2015-02-23

Unit testing headers in PHP

There's plenty of questions about unit testing code that sets headers using PHPUnit. PHPUnit officially assumes that neither the test code nor the tested code emit output or send headers:

(from https://github.com/sebastianbergmann/phpunit/issues/720#issuecomment-10399612)

And rightfully so. Headers are essentially global state, which is hard to unit test. However, if you're willing to install the PECL extension Runkit, you can actually unit test headers if you really want to.

I use the setupBeforeClass hook to only run the code once before the entire class runs. If you only have one method that you're testing it can all get stuck in the single test. I check if the runkit extension is loaded to avoid breaking the unit test suite if someone tries to run it that doesn't have runkit installed. The test that actually looks at the headers is marked with a requires annotation, which will mark that test as skipped if runkit isn't loaded. If every test in your test class tests headers, you can move that up to the class level if you'd like. My particular class had some tests that didn't need runkit and I wanted those to still run for people that didn't have runkit loaded.

1 comment:

  1. I've had a similar issue in the past. If I recall the "official" answer from a PHPUnit bug ticket was to batch up your headers to send in an array/property/Response object, and then test the contents of that. It still relies on PHP knowing how to send a header correctly, but you don't open up the can of runkit worms. There are a few more helpful comments here too:

    http://www.brianfenton.us/2011/01/unit-testing-with-phpunit.html

    ReplyDelete