title: Unit testing with Symfony's flashBag sessions
body: <p>I'm creating an application in Silex with unit tests.</p>
  
  <p>Running unit tests works fine against the regular session handler:</p>
  
  <pre><code>$app-&gt;register(new Silex\Provider\SessionServiceProvider(), array(
      'session.storage.options' =&gt; array(
          'cookie_lifetime' =&gt; 1209600, // 2 weeks
      ),
  ));
  </code></pre>
  
  <p>and setting this flag in my unit tests:</p>
  
  <pre><code>$this-&gt;app['session.test'] = true;
  </code></pre>
  
  <p>If I don't set that session.test flag, my unit tests throw a headers already sent error and all fail. With it on, my tests run well.</p>
  
  <p>The issue is I am attempting to use the flashBag feature (session info that lasts only until first request then get removed):</p>
  
  <pre><code>$foo = $app['session']-&gt;getFlashBag()-&gt;all();
  </code></pre>
  
  <p>The flashBag does not seem to respect the session.test flag, and attempts to send headers, which cause all my unit tests to fail:</p>
  
  <blockquote>
    <p>24)
    Yumilicious\UnitTests\Validator\PersonAccountTest::setConstraintsPassesWithMinimumAttributes
    RuntimeException: Failed to start the session because headers have
    already been sent.</p>
    
    <p>/webroot/yumilicious/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php:142
    /webroot/yumilicious/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/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/vendor/EHER/PHPUnit/bin/phpunit:5</p>
  </blockquote>
  
  <p>I've narrowed it down to this bit of code: <a href="https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php#L259" rel="nofollow">https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php#L259</a></p>
  
  <p>Specifically, line 262. Commenting out that single line allows my tests to work properly and all pass green.</p>
  
  <p>I've searched quite a bit to get this to work, but am not having any luck. I think it's 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 being deprecated.</p>
  
  <p>Any suggestions on getting my unit tests to work would be awesome.</p>