Using Sessions in PHPUnit Tests with CakePHP

By Stef van den Ham — February 24 2012

I was breaking my head over failing unit tests when trying to test a simple Controller that was saving a new record.

Turned out it was failing only on the command line, while the webversion was passing all the tests.

Luckily, the solution was simple ..

The PHPUnit command line suite apparently outputs contents early, before the session gets initiated.

Solution 1

Add --stderr to the command line:

$ cake testsuite app Controller/YourFancyController --stderr

This will pass the tests again, as it doesn’t output contents early to STDOUT. The only “problem” is you won’t see the awesome green and red colors in the CLI output anymore.

Solution 2

This is the one I use, as it brings the colors back.

As I’m using my self-contained PHPUnit Install, as found on Github @ PHPUnit-Cake2 I am able to edit the following file: Vendor/PHPUnit/Autoload.php At the very top of this file, initialize the session early:


This will pass the tests again, and even better, bring back the colors!

Maybe the session initialization can be done early by putting it in a file by CakePHP itself, but haven’t found the right place yet. Any suggestions?

Tags: phpunit cakephp


As always, if you have any questions or comments, you can find me on Twitter.

Be notified of new posts. Subscribe to the RSS Feed.