2016-12-18 10 views

答えて

2

は、あなたがこれを行うことができます:$Y毎回返されます解決策以下

$o->expects($this->any()) 
     ->method('foo') 
     ->will($this->onConsecutiveCalls($X, $Y, $Y, $Y, $Y, $Y)); 

onConsecutiveCallsの代わりにPHPUnitのreturnCallback関数内の無名関数を使用します。しかし、これは各戻り値に対して単に$ Yを入力するよりもコード行が多いため、の場合はすべてとなります。最初の呼び出しで同じ値を何度も返す必要がある場合はdoSomethingを呼び出します。

class Example 
{ 
    function doSomething() { } 
} 

// within your test class 
public function testConsecutiveCallbacks() 
{ 
    $counter = 0; 
    $stub = $this->createMock(Example::class); 
    $stub 
     ->method('doSomething') 
     ->will($this->returnCallback(function() use (&$counter) { 
      $counter++; 
      if ($counter == 1) { 
       return 'value for x'; 
      } 
      return 'value for y'; 
     })); 
    $this->assertEquals('value for x', $stub->doSomething()); 
    $this->assertEquals('value for y', $stub->doSomething()); 
    $this->assertEquals('value for y', $stub->doSomething()); 
    $this->assertEquals('value for y', $stub->doSomething()); 
} 
関連する問題