Using Sessions in PHPUnit Tests with CakePHP

Posted May 15th, 2021 • 1 min read

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?

More posts

May 15th, 2021 • 2 min read
In this post I will show you how to add GZip to your PHP application
May 15th, 2021 • 6 min read
Recently React Storybook came out. It's a tool to isolate your React Components to develop and design them outside of your app. I'll be walking through setting it up
May 15th, 2021 • 1 min read
How to use sessions within your unit tests using PHPUnit and CakePHP