1

While doing a controller test method, and running dispatch method I find it echos the output,

How can I capture that output instead of printing it to the commandline?

Omar
  • 8,374
  • 8
  • 39
  • 50

1 Answers1

2

You can use built-in support in PHPUnit: http://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.output


Edited from the old answer mentioning output buffering.

Josef Cech
  • 2,115
  • 16
  • 17
  • I was looking for something to do with the zend view renderer not to output the rendered output instead capture it – Omar Nov 01 '12 at 11:15
  • PHPUnit now supports testing output too: http://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.output – Josef Cech Jan 21 '14 at 11:48
  • @Thibault PHPUnit itself captures content with ob_start so this should still work: public function testFail() { ob_start(); echo 'foo'; $renderedContent = ob_get_contents(); ob_end_clean(); $renderedContentAfterClean = ob_get_contents(); $this->assertSame($renderedContent, 'foo'); $this->assertSame($renderedContentAfterClean, 'bar'); } – Josef Cech Jan 21 '14 at 11:55
  • @Josef Thank you for the link. Although apparently, PHPUnit already does a `ob_start` in the `setUp`, so you can't capture output. See http://stackoverflow.com/questions/9127252/phpunit-test-for-expected-headers – Thibault Jan 21 '14 at 12:00
  • @Thibault It can be nested - so it implies you have to catch exceptions and call ob_end_clean if you do not want some debugging nightmares. :) (Of course - just using built-in solution would be much better.) header() function ignores ob_* functions so you cannot catch headers even outside PHPUnit. – Josef Cech Jan 21 '14 at 12:18
  • @Josef Thanks for these details – Thibault Jan 21 '14 at 12:53
  • @Thibault You're welcome. .) I've edited the old answer. – Josef Cech Jan 21 '14 at 13:20