I am using Symfony\Component\Console\Output\ConsoleOutput
to write to the console.
Explicitly, I am writing to php://stdout
.
In my unit tests, I would like to be able to check the output to the console.
Using the PHPUnit method expectOutputString()
, I can check for output:
// Passes, as expected
public function testOutputBufferingEcho()
{
$this->expectOutputString('Hello');
echo 'Hello';
}
This works with output to php://output
too:
// Passes, as expected
public function testOutputBufferingOutput()
{
$this->expectOutputString('Hello');
$out = fopen('php://output', 'w');
fputs ($out, 'Hello');
fclose($out);
}
However, it does not work with output to php://stdout
(the one ConsoleOutput
uses as default):
// Failed asserting that two strings are equal.
// --- Expected
// +++ Actual
// @@ @@
// -'Hello'
// +''
public function testOutputBufferingStdOut()
{
$this->expectOutputString('Hello');
$out = fopen('php://stdout', 'w');
fputs ($out, 'Hello');
fclose($out);
}
Additionally, it appears it is not possible to use the ob_*
functions to capture output directly to php://stdout
.
Is there anyway to test output to php://stdout
with PHPUnit?
Or is there any other way to capture the output to php://stdout
into a string (and so test in PHPUnit)?
The above tests ran in PHPUnit 5.5.5.
Thank you in advance.