I have a console app, (written as a Symfony2 command) that is reading input from user via STDIN and with help of readline, user input is then passed to eval()
The whole thing is just for having “debug shell” (something like a php -a) but within project env and Dependency Injection container access.
I would like to write unit-tests for this command but I’m hitting wall, on how (and is it possible) write PHPUnit tests for this behavior?
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
In general, you could decouple
STDINandSTDOUTfrom your console app so you can replace it with another stream resource, likefopen(php://memory). Instead ofreadline, you useThe idea is to make your component testable without requiring the real console environment. Using this approach allows you to check the contents of the Stream at any time in your test. So if you run Command “foo” in your console app and want to test that the output is “bar” you simply rewind the appropriate resource and read it’s content. An alternative would be to use
SplTempFileObject.In your real world scenario you’d create the Console App with
But in your test you can setup the
ConsoleAppwith a stream of your choice:An example of a UnitTest using this method for the outstream would be