symfonyのProcessBuilderを使用して2つのProcessオブジェクト(さらにAPI docsを参照)を作成するメソッドをテストしようとしています。異なるモックプロセスオブジェクトを返す。実際には、Mockeryがこれを行うことさえできるかどうかについて少し不明です。Mockery:異なる戻り値を持つチェーンメソッド呼び出しのテスト
オブジェクトのコールチェーン(引数を含む)に基づいてMockeryのandReturn()
の値を選択することはできますか?
<?php
$processBuilderMock
->shouldReceive('setPrefix("test")->add("-f")->getProcess')
->andReturn($testProcess);
全例:
が存在しない場合は、次のコードは、ファイルを作成します/tmp/dummy
は、理想的には私はこのような何かを探しています。それは2つのコマンドtest -f /tmp/dummy
とtouch /tmp/dummy
を使用しています(私はそれが愚かな例だとわかります)。
<?php
class Toggler
{
public function toggleFile(ProcessBuilder $builder)
{
$testProcess = $builder
->setPrefix('test')->setArguments(array('-f', '/tmp/dummy'))
->getProcess();
$testProcess->run();
if (!$testProcess->isSuccessful()) { // (a)
$touchProcess = $builder
->setPrefix('touch')->setArguments(array('/tmp/dummy'))
->getProcess();
$touchProcess->run();
return $touchProcess->isSuccessful(); // (b)
} else {
// ...
}
}
}
すべてのケースをテストするために、私は(ここでは:$testProcess
と$touchProcess
)対応するコマンドtest
とtouch
ためProcess
オブジェクトを模擬できるようにする必要があります。理想的には、このためのテストコードは次のようになります。しかし
<?php
public function testToggleFileFileDoesNotExist()
{
$testProcess = \Mockery::mock('\Symfony\Component\Process\Process');
$testProcess->shouldReceive('isSuccessful')->andReturn(false); // (a)
$testProcess->shouldReceive('run');
$touchProcess = \Mockery::mock('\Symfony\Component\Process\Process');
$touchProcess->shouldReceive('isSuccessful')->andReturn(false); // (b)
$touchProcess->shouldReceive('run');
$builder = \Mockery::mock('\Symfony\Component\Process\ProcessBuilder');
$builder->shouldReceive('setPrefix("test")->getProcess')->andReturn($testProcess); // (c) Doesn't work!
$builder->shouldReceive('setPrefix("touch")->getProcess')->andReturn($touchProcess); // (c) Doesn't work!
$toggler = new Toggler();
$this->assertTrue($toggler->toggleFile($builder)); // see (b)
}
を、嘲笑を呼び出しチェーンで引数を許可していないので、私はこのようなシナリオをテストする方法で失われたビットです。何か案は?