2

Versions :

  • PHP : 8.1
  • PHPUnit : 9.5.21
  • Symfony 6.1

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('bob@gmail.com');
    $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
Loicyeu
  • 115
  • 9
  • I notice the test shown is called `testLoginSuccess` but the error message refernces `App\Tests\InternalLoginTest->testLoginFailure()`. Are you sure you are editing the test which is giving the error? – IMSoP Jul 25 '22 at 14:02
  • Yes I'm sorry, it's the extact same function with different data – Loicyeu Jul 25 '22 at 14:08
  • Could you check if following code is in your phpunit.xml? – Annemieke Buijs Sep 28 '22 at 13:23

2 Answers2

3

For test env

config/packages/test/framework.yaml

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

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.

Joel Dare
  • 26
  • 3