Loicyeu
Loicyeu

Reputation: 163

Symfony PHPUnit : Failed to start session because header have already been sent

Versions :

When the following test is run by PhpUnit, I get an error about the session not being able to be started. Does anyone have an idea of the problem and how to solve it?

The following topics do not answer my problem or the proposed solution doesn't work for me :

I tried the proposed solutions of the second link : @session_start(), @runInSeparateProcess but nothing worked. Maybe I just misunderstand my problem but I'm stuck for a week now.

protected function setUp(): void
{
  @session_start();
  parent::setUp();
}

/**
 * @runInSeparateProcess
 */
public function testLoginFailure(): void
{
    $client = static::createClient();
    $crawler = $client->request('GET', '/login');

    $form = $crawler->selectButton('Login')->form();

    $form['email']->setValue('[email protected]');
    $form['password']->setValue('123abcABC%');

    $crawler = $client->submit($form);
    $this->assertResponseIsSuccessful();
}
<!-- Failed to start the session because headers have already been sent by &quot;C:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\Util\Printer.php&quot; at line 104. (500 Internal Server Error) -->

C:\Users\cimba\Documents\project\vendor\symfony\framework-bundle\Test\BrowserKitAssertionsTrait.php:142
C:\Users\cimba\Documents\project\vendor\symfony\framework-bundle\Test\BrowserKitAssertionsTrait.php:33
C:\Users\cimba\Documents\project\tests\InternalLoginTest.php:51
C:\Users\cimba\Documents\project\bin\phpunit:11

Caused by
ErrorException: Failed to start the session because headers have already been sent by "C:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\Util\Printer.php" at line 104. in C:\Users\cimba\Documents\project\vendor\symfony\http-foundation\Session\Storage\NativeSessionStorage.php:135
Stack trace:
#0 C:\Users\cimba\Documents\project\vendor\symfony\framework-bundle\Test\BrowserKitAssertionsTrait.php(33): Symfony\Bundle\FrameworkBundle\Test\WebTestCase::assertThatForResponse(Object(Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsSuccessful), '')
#1 C:\Users\cimba\Documents\project\tests\InternalLoginTest.php(51): Symfony\Bundle\FrameworkBundle\Test\WebTestCase::assertResponseIsSuccessful()
#2 C:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\Framework\TestCase.php(1545): App\Tests\InternalLoginTest->testLoginFailure()
#3 C:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\Framework\TestCase.php(1151): PHPUnit\Framework\TestCase->runTest()
#4 C:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\Framework\TestResult.php(726): PHPUnit\Framework\TestCase->runBare()
#5 C:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\Framework\TestCase.php(903): PHPUnit\Framework\TestResult->run(Object(App\Tests\InternalLoginTest))
#6 C:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\Framework\TestSuite.php(670): PHPUnit\Framework\TestCase->run(Object(PHPUnit\Framework\TestResult))
#7 C:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\Framework\TestSuite.php(670): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#8 C:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\TextUI\TestRunner.php(673): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#9 C:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\TextUI\Command.php(143): PHPUnit\TextUI\TestRunner->run(Object(PHPUnit\Framework\TestSuite), Array, Array, true)
#10 C:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\TextUI\Command.php(96): PHPUnit\TextUI\Command->run(Array, true)
#11 C:\Users\cimba\Documents\project\bin\phpunit(11): PHPUnit\TextUI\Command::main()
#12 C:\Users\cimba\AppData\Local\Temp\ide-phpunit.php(224): require_once('C:\\Users\\cimba\\...')
#13 C:\Users\cimba\AppData\Local\Temp\ide-phpunit.php(173): IDE_PHPUnit_Loader::loadByAutoloader('C:\\Users\\cimba\\...')
#14 C:\Users\cimba\AppData\Local\Temp\ide-phpunit.php(228): IDE_PHPUnit_Loader::init()
#15 {main}

framework.yaml

framework:
    secret: '%env(APP_SECRET)%'
    http_method_override: false
    session:
        handler_id: null
        cookie_secure: auto
        cookie_samesite: lax
        storage_factory_id: session.storage.factory.native
    php_errors:
        log: true

when@test:
    framework:
        test: true
        session:
            storage_factory_id: session.storage.factory.mock_file

Upvotes: 6

Views: 4241

Answers (2)

Alex Rodionov
Alex Rodionov

Reputation: 131

For test env

config/packages/test/framework.yaml

framework:
    test: true
    session:
        storage_factory_id: session.storage.factory.mock_file

Upvotes: 13

Joel Dare
Joel Dare

Reputation: 26

If others find this I found that this was a result of me not running the correct APP_ENV. I solved it by running the following.

APP_ENV=test yarn phpunit

I have a run command called phpunit configured. You could also use phpunit directly, the npm command, or whatever you've setup to run tests. It's APP_ENV=test that was important for my use case. This, apparently, prevents the Symfony app from attempting to print to stdout thus suppressing the error.

Upvotes: 1

Related Questions