Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Session][Test] MockFileSessionStorage is not persistent #22616

Closed
@mtricht

Description

@mtricht
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 2.1+

The following controller:

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction()
    {
        $this->get('session')->set('test', 'test');
        $this->get('session')->save();
        return new Response('All good');
    }

    /**
     * @Route("/test", name="test")
     */
    public function testAction()
    {
        if ($this->get('session')->get('test') !== 'test') {
            return new Response('Missing test');
        }
        return new Response('All good');
    }
}

Used in a test:

class DefaultControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();
        $client->request('GET', '/');
        $client->request('GET', '/test');
        $this->assertEquals('All good', $client->getResponse()->getContent());
    }
}

Expected: Test succeeds. Session is persisted as documented here.
Actual: Test fails. The session is not persisted through the two requests.

The following line causes this:

Removing this line fixes the problem.

Another 'solution' is adding the following line to the first action:

@@ -16,6 +16,7 @@ class DefaultController extends Controller
     {
         $this->get('session')->set('test', 'test');
         $this->get('session')->save();
+        $this->get('session')->get('test');
         return new Response('All good');
     }

Adding this get after the save sets the started variable back to true meaning the session is persisted, which is rather odd and made debugging this issue very frustrating.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions