Mockery应该接受类型 - >得到收到的对象

时间:2015-08-28 08:44:39

标签: php phpunit mockery

我对嘲弄和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对象。我想要的是对象。

我想断言等于EFPresentation->标题,但是从给定参数的给定对象开始。现在我正在使用在add_presentation中创建的EFPresentation->标题。

1 个答案:

答案 0 :(得分: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"];
      })
  )
;

有一点需要注意的是,当测试没有通过时,除非你发出一些回声或使用调试器,否则你不会得到任何有关原因的详细信息。 Mockery会告知关闭返回错误。

编辑:编辑了缺失的括号