Codeception / module-symfony

Codeception module for testing apps using Symfony framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ini_set(): A session is active. You cannot change the session module's ini settings at this time

ThomasLandauer opened this issue · comments

I'm getting this PHPUnit\Framework\Exception:

ini_set(): A session is active. You cannot change the session module's ini settings at this time

In my config/packages/test/framework.php, I have:

return static function (ContainerConfigurator $containerConfigurator): void {
    $containerConfigurator->extension('framework', [
        'test' => true,
        'csrf_protection' => false,
        'session' => [
            'handler_id' => 'session.handler.native_file',
        ],
    ]);
};

https://github.com/Codeception/Codeception/issues/6025 is similar.

When I set the handler_id to null, this error disappears, but there is another (which I don't know if it's related): #43

commented

I don't know if it's the cause or not, but if I remove this line:
echo "Testing $suiteName\n";

from this file:
/vendor/symfony/phpunit-bridge/Legacy/SymfonyTestsListenerTrait.php

the error disappears.

Here is this line: https://github.com/symfony/symfony/blob/5.x/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php#L135

But I don't have this file in my vendor directory (symfony/phpunit-bridge not in composer show), so this can't be the reason in my case.

After some debugging, I found out:

  • The exception gets thrown by Symfony's NativeFileSessionHandler::__construct()
  • During my test, this function is called multiple times. Doing session_status() in there, gives PHP_SESSION_NONE first, but PHP_SESSION_ACTIVE then.
  • The stack trace in Codeception's saved _output HTML file shows that the last Codeception call is Symfony::doRequest(). Then, after around 15 Symfony internal calls, it ultimately ends up at the NativeFileSessionHandler (see above).

So the question is: Why is sometimes the previous session not getting closed before trying to start this new one?

The error is gone now, and I think it was due to this change in config/packages/test/framework.php:

'storage_id' => 'session.storage.mock_file',

See https://symfony.com/doc/current/reference/configuration/framework.html#storage-id

So I'm closing this.

@TavoNiievez Is it a given fact that Symfony module is only compatible with session.storage.mock_file? If yes, we should document this on https://codeception.com/docs/modules/Symfony
(And I'm pretty sure it did work with session.handler.native_file too - so this must have changed in the previous months/years.)