3

I'm trying to play a little bit with the functional test in Symfony and i'm currently facing a problem with my sessions. I execute a piece of code, which seems to be working, but nothing is stored inside the session of my container.

I have a form where you set datas. When you submit it, it checks the values and store it inside sessions. Then it redirects to another page where these values stored in session are needed.

The purpose of my test is to check the session.

<?php

namespace Acme\TestBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;

class FrontControllerTest extends WebTestCase
{
    public function testForm()
    {
        $client = static::createClient();
        $session = $client->getContainer()->get('session');

        $crawler = $client->request('GET', '/setParameters');

        $form = $crawler->selectButton('Submit')->form();
        $form['myForm[firstname]']->setValue('Ben');
        $form['myForm[lastname]']->setValue('H');

        $client->submit($form);

        //I tested and my controller is fully going through the submit handler
        //which check the values and save it into the session
        //Things are 100% sure there. Then it redirects to another page which check those values.

        $values = $client->getContainer()->get('session')->get('parameters'); //NULL
        $this->assertEquals($values['firstname'], 'Ben'); //false
        $this->assertEquals($values['lastname'], 'H'); //false
    }
}

Actually it's not working at all, it seems like i can't store anything in the session and retrieve it.

Can someone help me with it ? Thank's.

1
  • Before check in the session I suggest you to check the succesfully of the operation with $this->assertEquals(302, $client->getResponse()->getStatusCode(), "correct response code"); Commented Sep 23, 2014 at 12:41

1 Answer 1

2

The container you use in your test is not the container that is used by the requests you trigger. Symfony creates new containers for each request. So you can not access the session directly via the container.

See also http://alexandre-salome.fr/blog/Symfony2-Isolation-Of-Tests

A possible solution would be to extract the session ID from your cookies and then read the session from your session storage.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank's ! as a newbie on functionnal test, i'd also like to have your advice on "does that make sense to do what i'm doing" :-).
I mean you could extract the logic that persists the session data into a separate class and test this class with unit tests. But if anything around the session handling, form handling or whatever changes and the code in your separate class is not called anymore you will only recognize this with a functional test. So from my point of view a functional test is perfectly suitable for this case.
Please add the relevant code from the link in a blockquote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.