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:\Users\cimba\Documents\project\vendor\phpunit\phpunit\src\Util\Printer.php" 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
testLoginSuccess
but the error message referncesApp\Tests\InternalLoginTest->testLoginFailure()
. Are you sure you are editing the test which is giving the error? – Preinstruct