Mockery对象参数验证问题

tho*_*dic 4 php phpunit mockery

考虑一下示例类(对于它如此复杂而道歉,但它尽可能地模糊):

class RecordLookup
{
    private $records = [
        13 => 'foo',
        42 => 'bar',
    ];

    function __construct($id)
    {
        $this->record = $this->records[$id];
    }

    public function getRecord()
    {
        return $this->record;
    }
}

class RecordPage
{
    public function run(RecordLookup $id)
    {
        return "Record is " . $id->getRecord();
    }
}

class App
{
    function __construct(RecordPage $page, $id)
    {
        $this->page = $page;
        $this->record_lookup = new RecordLookup($id);
    }

    public function runPage()
    {
        return $this->page->run($this->record_lookup);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在模拟RecordPage时测试App:

class AppTest extends \PHPUnit_Framework_TestCase
{
    function testAppRunPage()
    {
        $mock_page = \Mockery::mock('RecordPage');

        $mock_page
            ->shouldReceive('run')
            ->with(new RecordLookup(42))
            ->andReturn('bar');

        $app = new App($mock_page, 42);

        $this->assertEquals('Record is bar', $app->runPage());
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:预期的对象参数->with(new RecordLookup(42)).

我希望这会通过,但是Mockery会返回投掷No matching handler found for Mockery_0_RecordPage::run(object(RecordLookup)). Either the method was unexpected or its arguments matched no expected argument list for this method.

我假设这是因为严格的比较用于期望的参数with()并且new RecordLookup(42) === new RecordLookup(42)评估为false.注意new RecordLookup(42) == new RecordLookup(42)评估为真,所以如果有人放松比较,它将解决我的问题.

有没有一种正确的方法来处理Mockery中的预期实例参数?也许我使用不正确?

gon*_*lez 9

您可以告诉嘲笑应该接收RecordLookup实例(任何):

$mock_page
        ->shouldReceive('run')
        ->with(\Mockery::type('RecordLookup'))
        ->andReturn('bar');
Run Code Online (Sandbox Code Playgroud)

但这将匹配RecordLookup的任何实例.如果您需要挖掘对象内部并检查它的值是否为42,那么您可以使用自定义验证器:

$mock_page
        ->shouldReceive('run')
        ->with(\Mockery::on(function($argument) {
            return 
                 $argument instanceof RecordLookup && 
                 'bar' == $argument->getRecord()
            ;
        }))
        ->andReturn('bar');
Run Code Online (Sandbox Code Playgroud)

还有更多选项,在文档中有很好的解释.