Fabrizio Fabrizio - 1 year ago 223
PHP Question

Cannot send session cookie - headers already sent PHPUnit / Laravel

I have this strange problem when i call the

on my TestCase class for unit test a class
when i run phpunit it throw me this error:

1) MatchRequestRepositoryTest::test_find_requests_by_match_id
ErrorException: session_start(): Cannot send session cookie - headers
already sent by (output started at

What can be the problem? Thanks for any help.

Answer Source

The problem is that you have some code, perhaps deep in the framework you use, that calls session_start(). That in turn wants to send a cookie. But PHPUnit has already started writing output to stdout.

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

function setUp(){