2015-08-28 20 views
0

我對嘲笑和phpunit測試很新穎。嘲笑應該接收類型 - >獲取對象

我創建了一個測試來檢查是否有東西寫入數據庫。我使用教義和我創建了我的doctrine_connection和我的doctrine_manager模擬對象。

一切正常,但我想得到給定的參數來檢查它與assertEqual。

現在即時通訊執行以下操作:

require_once "AbstractEFlyerPhpUnitTestCase.php"; 
class test2 extends AbstractEFlyerPhpUnitTestCase { 

public function getCodeUnderTest() { 
    return "../php/ajax/presentations/add_presentation.php"; 
} 

public function testingPresentationObject() 
{ 
    // prepare 
    $_REQUEST["caption"] = "Testpräsentation"; 
    $_SESSION["currentUserId"] = 1337; 

    $this->mockedUnitOfWork->shouldReceive('saveGraph')->with(\Mockery::type('EFPresentation')); 
    $this->mockedUnitOfWork->shouldReceive('saveGraph')->with(\Mockery::type('EFSharedPresentation')); 
    $this->mockedDoctrineConnection->shouldReceive('commit'); 

    //run 
    $this->runCodeUnderTest(); 
    global $newPresentation; 
    global $newSharedPresentation; 
    // verify 
    $this -> assertEquals($newPresentation->caption,$_REQUEST["caption"]); 
    $this -> assertEquals($newSharedPresentation->userId,$_SESSION["currentUserId"]); 
} 
} 

saveGraph越來越的EFPresentation對象。我想要的是對象。

我想assertEqual EFPresentation->標題,但從給定的對象給參數。現在我使用在add_presentation中創建的EFPresentation->標題。

回答

1

您可以使用\ Mockery :: on(closure)來檢查參數。這個方法接收一個將被調用傳遞實際參數的函數。在裏面你可以檢查你需要的任何東西,如果檢查成功,你必須返回true。

$this 
    ->mockedUnitOfWork 
    ->shouldReceive('saveGraph') 
    ->with(
     \Mockery::on(function($newPresentation) { 
      // here you can check what you need... 
      return $newPresentation->caption === $_REQUEST["caption"]; 
     }) 
) 
; 

一個需要注意的是,當測試沒有通過,你不會得到爲什麼,除非你把一些回聲或使用調試程序的任何詳細信息。嘲笑會通知關閉返回false。

編輯:編輯丟失的支架

+0

謝謝。有效 :) – kovogel