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 :
- How to fix headers already sent
- Cannot send session cookie headers already sent
- Github discution 43950
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 "C:UserscimbaDocumentsprojectvendorphpunitphpunitsrcUtilPrinter.php" at line 104. (500 Internal Server Error) -->
C:UserscimbaDocumentsprojectvendorsymfonyframework-bundleTestBrowserKitAssertionsTrait.php:142
C:UserscimbaDocumentsprojectvendorsymfonyframework-bundleTestBrowserKitAssertionsTrait.php:33
C:UserscimbaDocumentsprojecttestsInternalLoginTest.php:51
C:UserscimbaDocumentsprojectbinphpunit:11
Caused by
ErrorException: Failed to start the session because headers have already been sent by "C:UserscimbaDocumentsprojectvendorphpunitphpunitsrcUtilPrinter.php" at line 104. in C:UserscimbaDocumentsprojectvendorsymfonyhttp-foundationSessionStorageNativeSessionStorage.php:135
Stack trace:
#0 C:UserscimbaDocumentsprojectvendorsymfonyframework-bundleTestBrowserKitAssertionsTrait.php(33): SymfonyBundleFrameworkBundleTestWebTestCase::assertThatForResponse(Object(SymfonyComponentHttpFoundationTestConstraintResponseIsSuccessful), '')
#1 C:UserscimbaDocumentsprojecttestsInternalLoginTest.php(51): SymfonyBundleFrameworkBundleTestWebTestCase::assertResponseIsSuccessful()
#2 C:UserscimbaDocumentsprojectvendorphpunitphpunitsrcFrameworkTestCase.php(1545): AppTestsInternalLoginTest->testLoginFailure()
#3 C:UserscimbaDocumentsprojectvendorphpunitphpunitsrcFrameworkTestCase.php(1151): PHPUnitFrameworkTestCase->runTest()
#4 C:UserscimbaDocumentsprojectvendorphpunitphpunitsrcFrameworkTestResult.php(726): PHPUnitFrameworkTestCase->runBare()
#5 C:UserscimbaDocumentsprojectvendorphpunitphpunitsrcFrameworkTestCase.php(903): PHPUnitFrameworkTestResult->run(Object(AppTestsInternalLoginTest))
#6 C:UserscimbaDocumentsprojectvendorphpunitphpunitsrcFrameworkTestSuite.php(670): PHPUnitFrameworkTestCase->run(Object(PHPUnitFrameworkTestResult))
#7 C:UserscimbaDocumentsprojectvendorphpunitphpunitsrcFrameworkTestSuite.php(670): PHPUnitFrameworkTestSuite->run(Object(PHPUnitFrameworkTestResult))
#8 C:UserscimbaDocumentsprojectvendorphpunitphpunitsrcTextUITestRunner.php(673): PHPUnitFrameworkTestSuite->run(Object(PHPUnitFrameworkTestResult))
#9 C:UserscimbaDocumentsprojectvendorphpunitphpunitsrcTextUICommand.php(143): PHPUnitTextUITestRunner->run(Object(PHPUnitFrameworkTestSuite), Array, Array, true)
#10 C:UserscimbaDocumentsprojectvendorphpunitphpunitsrcTextUICommand.php(96): PHPUnitTextUICommand->run(Array, true)
#11 C:UserscimbaDocumentsprojectbinphpunit(11): PHPUnitTextUICommand::main()
#12 C:UserscimbaAppDataLocalTempide-phpunit.php(224): require_once('C:\Users\cimba\...')
#13 C:UserscimbaAppDataLocalTempide-phpunit.php(173): IDE_PHPUnit_Loader::loadByAutoloader('C:\Users\cimba\...')
#14 C:UserscimbaAppDataLocalTempide-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
2
Answers
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 usephpunit
directly, thenpm
command, or whatever you’ve setup to run tests. It’sAPP_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.For test env
config/packages/test/framework.yaml