douangzhao4108 2015-10-16 14:28
浏览 49
已采纳

在phpunit中传递变量

I'm trying to pass an array i created in my testcase into my function i want to test. Is it possible to create a vairable in the testcase and pass or mock it to the class/function i want to test?

is it possible to use something like this:

$this->object = array(//array code here);
$this->testclass->attachVar->getAllObjects($this->objects);

Here is my code:

class myClassTest extends \PHPUnit_Framework_TestCase {

protected function setUp(){
    $this->testclass = new \stdClass();
    $this->testclass = $this->getMockBuilder('library\ixPlanta\plantChange', $this->object)
                 ->disableOriginalConstructor()                     
                 ->getMock();
}
public function testGetAllObjects() {

   $this->object = array(
                'testdb'        => array(
                    'testdb_michel' => array(
                        'dbname'        => 'testdb',
                        'projectName'   => 'testdb',
                        'projectID'     => 'bd993d2b9478582f6d3b73cda00bd2a',
                        'mainProject'   => 'test',
                        'search'        => false,
                        'webgroup'      => array(),
                        'locked'        => false
                    )
                )
            );


    $this->testclass->expects($this->once())
            ->method('GetAllObjects')
            ->with('testdb', false, "CHECKED")
            ->injectTo('object', $this->object)
            ->will();


  $result = $this->testclass->getAllObjects('testdb', false, "CHECKED");
  $this->assertTrue($result);

    }

In the function testGetAllObjects() i created an array that i want to pass to the function i want to test

public function getAllObjects($company,$selected=false,$selectText='CHECKED'){
    $objList = array();
    $i = 0;
    foreach($this->Objects[$company] as $key => $value){
        $objList[$i] = array('value'=> $key,'name' => $value['projectName'], 'objectID' => $value['projectID']);
        $objList[$i]['checked'] = '';
        if($selected !== false && !is_array($selected) && $selected === $value['dbname']){
            $objList[$i]['checked'] = $selectText;
        }elseif($selected !== false && is_array($selected) && in_array($value['dbname'], $selected)){
            $objList[$i]['checked'] = $selectText;
        }
        ++$i;
    }
    return $objList;
}

The variable i want to pass to getAllObjects is $this->objects

  • 写回答

1条回答 默认 最新

  • dongtun2572 2015-10-16 14:53
    关注

    I think you misunderstood mock objects. The purpose of mock objects is, to create a dummy object for any other class you don't want to test. Mocking a method means, to prevent another class from calling its actual logic. Instead, it is not executed and the mock just returns whatever you gave it.

    To test your actual class you just instantiate it and call its method:

    class myClassTest extends \PHPUnit_Framework_TestCase
    {
    
        protected function setUp()
        {
            $this->testclass = new MyClass();
        }
    
        public function testGetAllObjects()
        {
    
            $this->testclass->object = array(
                'testdb' => array(
                    'testdb_michel' => array(
                        'dbname'      => 'testdb',
                        'projectName' => 'testdb',
                        'projectID'   => 'bd993d2b9478582f6d3b73cda00bd2a',
                        'mainProject' => 'test',
                        'search'      => false,
                        'webgroup'    => array(),
                        'locked'      => false
                    )
                )
            );
    
            $result = $this->testclass->getAllObjects('testdb', false, "CHECKED");
            $this->assertTrue($result);
    
        }
    }
    

    Example of a mock:

    Let's say your class contains some other object of the class Service which is injected through the constructor:

    class MyClass {
    
        protected $service;
    
        public function __construct(Service $service) {
            $this->service = $service;
        }
    
        public function myMethod($argument) {
            $return = $this->service->callService($argument);
            return $return;
        }
    
    }
    

    And your Service object is something like this:

    class Service{
    
        public function callService($argument) {
            if($argument === NULL) {
                throw new \Exception("argument can't be NULL");
            }
            return true;
        }
    
    }
    

    Now you could test MyClass with this method:

    public function testMyClassMethod() {
        $serviceMock = $this->getMockBuilder("Service")->getMock();
        $serviceMock->expects($this->any())
            ->method("callService")
            ->will($this->returnValue(true));
    
        $myClass = new MyClass($serviceMock);
    
        $this->assertTrue($myClass->myMethod(NULL));
    }
    

    myMethod will still return true, although Service would normally throw an Exception if $argument is NULL. But because we mocked the method, it is never actually called and the mock object will return whatever we provided in ->will($this->returnValue()).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面