2015-10-16 147 views
1

我試圖將我在我的測試用例中創建的數組傳遞到我想測試的函數中。是否有可能在測試用例中創建一個可放大的文件並將其傳遞或模擬到我想測試的類/函數?在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

回答

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 o bject是這樣的:

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通常會拋出一個異常,如果$argumentNULL。但是因爲我們嘲笑了這個方法,所以它永遠都不會被調用,模擬對象會返回我們在->will($this->returnValue())提供的任何東西。