Group Testing with Symfony flashBag Sessions

I am building an application in Silex with unit tests.

Running unit tests works fine with a regular session handler:

$app->register(new Silex\Provider\SessionServiceProvider(), array( 'session.storage.options' => array( 'cookie_lifetime' => 1209600, // 2 weeks ), )); 

and setting this flag in my unit tests:

 $this->app['session.test'] = true; 

If I do not set this session.test flag, my unit tests throw headers, errors already posted, and all fail. However, my tests pass well.

The problem is that I am trying to use the flashBag function (session information that lasts only until the first request is deleted):

 $foo = $app['session']->getFlashBag()->all(); 

FlashBag does not seem to respect the session.test flag and tries to send headers that cause all of my unit tests to fail:

24) Yumilicious \ UnitTests \ Validator \ PersonAccountTest :: setConstraintsPassesWithMinimumAttributes RuntimeException: The session could not be started because the headers have already been sent.

/webroot/yumilicious/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php:142 / webroot / yumilicious / vendor / symfony / http -foundation / Symfony / Component / HttpF /NativeSessionStorage.php:262 / webroot / yumilicious / vendor / symfony / http -foundation / Symfony / Component / HttpFoundation / Session / Session.php: 240 / webroot / yumilicious / vendor / symfony / http -foundation / Symfony / Component / HttpFoundation /Session/Session.php:250 /webroot/yumilicious/src/app.php:38 /webroot/yumilicious/tests/Yumilicious/UnitTests/Base.php:13 / webroot / yumilicious / vendor / silex / silex / src / Silex /WebTestCase.php:34 /webroot/yumilicious/vendor/EHER/PHPUnit/src/phpunit/phpunit.php:46 / Webroot / yumilicious / seller / Eher / PHPUnit / bin / PHPUnit: 5

I narrowed it down to this piece of code: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php#L259

In particular, line 262. Commenting that one line allows my tests to work correctly and everyone skips green.

I was looking quite a bit to get this to work, but I was out of luck. I think this is because the flashBag stuff is new (https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Session/Session.php#L305) and the old methods are deprecated.

Any suggestions that my unit tests work would be great.

+4
source share
3 answers

For testing, you need to replace the session.storage service with an instance of MockArraySessionStorage :

 use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; $app['session.storage'] = new MockArraySessionStorage(); 

This is because native is trying to send a cookie via header , which of course does not work in a test environment.

EDIT: There is now a session.test parameter that you must set to true. This will automatically force the session to use the repository layout.

+4
source

I also had this, if I'm not mistaken, I fixed it when my unittests run through another environment that has

 framework: test: ~ session: storage_id: session.storage.mock_file 

set in config_test.yml

+3
source

Today I ran into a similar problem, and the temp fix was to comment out a block of code in

 \Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage 

in start() method

  /* if (ini_get('session.use_cookies') && headers_sent()) { throw new \RuntimeException('Failed to start the session because headers have already been sent.'); } */ 

This solution keeps the tests "green" and from the appearance of its application session functionality as it is.

0
source

Source: https://habr.com/ru/post/1436851/


All Articles