Cannot send cookie session - PHPUnit / Laravel headers already sent

I have this strange problem, when I call parent::setUp() in my TestCase class for the unit test class, when I ran phpunit, it threw me this error:

1) MatchRequestRepositoryTest :: test_find_requests_by_match_id ErrorException: session_start (): unable to send session cookies - headers have already been sent (the output started with /var/www/project.dev/vendor/phpunit/phpunit/PHPUnit/TextUI/Test.ner/testunnerprefunner )

What could be the problem? Thanks for any help.

+7
php unit-testing phpunit laravel-4
source share
2 answers

The problem is that you have the code, perhaps deep in the structure you are using, which calls session_start() . This, in turn, wants to send a cookie. But PHPUnit has already started writing output to stdout.

The key to understanding here is that it is just a unit test, no one cares about the header. So just suppress the error message. And the way you do this, without changing the system test, is to call session_start () in your own unit test (before parent::setUp() or inside this setUp function). And use the @ prefix to suppress errors. eg.

 function setUp(){ @session_start(); parent::setUp(); ... } 
+19
source share

The best way to do this in PHPUnit is to send the output to stderr instead of stdout, as shown by this answer .

 phpunit --stderr 
+11
source share

All Articles