在phpunit

时间:2015-10-16 14:28:01

标签: php unit-testing testing netbeans phpunit

我试图将我在我的测试用例中创建的数组传递到我要测试的函数中。是否有可能在测试用例中创建一个可用的,并将其传递或模拟到我想要测试的类/函数?

可以使用这样的东西:

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

这是我的代码:

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);

    }

在函数testGetAllObjects()中我创建了一个数组,我想传递给我想要测试的函数

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;
}

我想传递给getAllObjects的变量是$ this-> objects

1 个答案:

答案 0 :(得分:4)

我认为你误解了模拟对象。模拟对象的目的是为 想要测试的任何其他类创建一个虚拟对象。模拟方法意味着,以防止另一个类调用其实际逻辑。相反,它没有执行,模拟只返回你给它的任何东西。

要测试您的实际类,您只需实例化它并调用其方法:

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);

    }
}

模拟示例:

让我们说你的类包含通过构造函数注入的类Service的其他一些对象:

class MyClass {

    protected $service;

    public function __construct(Service $service) {
        $this->service = $service;
    }

    public function myMethod($argument) {
        $return = $this->service->callService($argument);
        return $return;
    }

}

你的Service对象是这样的:

class Service{

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

}

现在您可以使用此方法测试MyClass

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仍会返回true,但如果Service$argument,则NULL通常会抛出异常。但是因为我们模拟了这个方法,它实际上从未被调用过,而模拟对象将返回我们在->will($this->returnValue())中提供的任何内容。