2017-05-05 73 views
1

我刚刚开始使用Mockery代替PHPUnits自己的嘲笑功能。PHPUnits的嘲笑当量,returnValueMap

Mockery是否有等价的PHPUnits returnValueMap,它可以根据提供给方法调用的特定参数值返回特定值?

这是怎么用PHPUnit完成的。

<?php 

$stub = $this->createMock(SomeClass::class); 

$map = [ 
    ['a', 'b', 'c', 'd'], 
    ['e', 'f', 'g', 'h'] 
]; 

$stub->method('doSomething') 
    ->will($this->returnValueMap($map)); 

$this->assertEquals('d', $stub->doSomething('a', 'b', 'c')); 
$this->assertEquals('h', $stub->doSomething('e', 'f', 'g')); 

回答

0

我的问题是有点“咄”的时刻为自己。这是嘲弄相当于PHPUnit中使用returnValueMap()的:

<?php 

$stub = \Mockery::mock(SomeClass::class); 

$stub->shouldReceive('doSomething')->with('a', 'b', 'c')->andReturn('d'); 
$stub->shouldReceive('doSomething')->with('e', 'f', 'g')->andReturn('h'); 

$this->assertEquals('d', $stub->doSomething('a', 'b', 'c')); 
$this->assertEquals('h', $stub->doSomething('e', 'f', 'g')); 

...咄。