antonioribeiro / support

Support Classes
BSD 3-Clause "New" or "Revised" License
59 stars 49 forks source link

PhpSession messing up with tests? #12

Open bjmrey opened 7 years ago

bjmrey commented 7 years ago

After upgrading to Laravel 5.4 running phpunit outputs the error below.

There was 1 error:

1) Tests\Feature\ExampleTest::testBasicTest ErrorException: session_start(): Cannot send session cookie - headers already sent by (output started at /project/vendor/phpunit/phpunit/src/Util/Printer.php:114)

/project/vendor/pragmarx/support/src/PhpSession.php:23 /project/vendor/pragmarx/support/src/PhpSession.php:14 /project/vendor/pragmarx/tracker/src/Vendor/Laravel/ServiceProvider.php:268 ...

I've solved it temporarily by adding @ before session_start() in src/PhpSession.php, but I'm not quite sure what's going on and whether the issue comes from this package or from my setup.

private function startSession()
{
    if ( ! $this->isStarted())
    {
        @session_start();
    }
}
hktang commented 7 years ago

Thank you. I am having the same issue with Laravel 5.3.

Your temporary solution helps, at least the tests can run as usual.

antonioribeiro commented 7 years ago

Could you please tell me what happens if you do a

private function startSession()
{
        dd(session_status());

        if ( ! $this->isStarted())
    {
        @session_start();
    }
}

?

hktang commented 7 years ago

Hi @antonioribeiro, just run your code and get a return value 1.

antonioribeiro commented 7 years ago

Sorry, I think we shoul also test:

private function startSession()
{

        if ( ! $this->isStarted())
    {
        dd(session_status());

        @session_start();
    }
}
antonioribeiro commented 7 years ago

Those are the values we are looking for

image

hktang commented 7 years ago

Oops it seems I messed up with the tracker in both dev and production environment so I reverted the code to a previous commit. Will update you when I get back to the same state again..

antonioribeiro commented 7 years ago

Did you try to run it (locally) with dd()inside that if() ?

javisr commented 6 years ago

Hi @antonioribeiro, I'm having the same issue.

I have a fresh 5.6 Laravel installation with your tracker package and the default example tests[1] and I get the same error when I run phpunit.

I've include dd(session_status()) inside the if() and the return is 1 in the browser and inside the phpunit test.

I hope it could help :)

[1]

public function testExample()
{
    $response = $this->get('/');
     $response->assertStatus(200);
}