I'm not familiar with the Sf2 Command thing, but the Sf2 docs have an example about testing it at http://symfony.com/doc/2.0/components/console.html#testing-commands
一般に、 fopen(php:// memory)
のような別のストリームリソースと置き換えることができるように、コンソールアプリケーションから STDIN
と STDOUT
/code> readline
の代わりに、
fwrite($outputStream, 'Prompt');
$line = stream_get_line($inputStream, 1024, PHP_EOL);
実際のコンソール環境を必要とせずにコンポーネントをテスト可能にすることです。この方法を使用すると、テスト中のいつでもストリームの内容を確認できます。 したがって、コンソールアプリケーションでCommand "foo"を実行し、出力が "bar"であることをテストする場合は、単に適切なリソースを巻き戻し、その内容を読む。代わりに、 SplTempFileObject
を使用することもできます。
class ConsoleApp
…
public function __construct($inputStream, $outputStream)
{
$this->inputStream = $inputStream;
$this->outputStream = $outputStream;
}
}
あなたの実際のシナリオでは、
$app = new ConsoleApp(STDIN, STDOUT);
しかし、あなたのテストではあなたが選んだストリームで ConsoleApp
を設定することができます:
public function setup()
{
$i = fopen('php://memory', 'w');
$o = fopen('php://memory', 'w');
$this->consoleApp = new ConsoleApp($i, $o);
}
このメソッドをアウトストリームに使用するUnitTestの例は、