Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set some data in session in functional test? #414

Closed
shubaivan opened this issue Apr 23, 2018 · 7 comments
Closed

How to set some data in session in functional test? #414

shubaivan opened this issue Apr 23, 2018 · 7 comments
Labels

Comments

@shubaivan
Copy link

I use "liip/functional-test-bundle": "~1.8", and "symfony/symfony": "~3.4", and for my functional test I want set some data in session,but after set data in action, with the same key, I had null. Mytest class extends from extends WebTestCase

this is how I set data in test method

    $session = $this->container->get('session');
    $session->set('test', 2);

    $this->getClient()->request(
        Request::METHOD_GET,
        $routing
    );

config_test.yml

framework:
test: ~
session:
    storage_id: session.storage.mock_file
    name: "MOCKSESSID"
profiler:
    collect: false

and in action

public function getEntityAction()
{
    $test = $this->get('session')->get('test');

variable $test equal null
How to set some data in session in functional test ?

@alexislefebvre
Copy link
Collaborator

Someone had the same question in #383.

I think that the test should follow the same path than the one used by visitors. So you'll test that the data is wrote in session and readen later.

@Jean85
Copy link
Contributor

Jean85 commented Apr 24, 2018

I think the main mistake here is forgetting that $this->container is NOT the same as $client->getContainer(): the Client spuns a completely separated, new container, and depending on how you use the session, you may not find the same stuff in both of them.

@alexislefebvre
Copy link
Collaborator

@Jean85 Do you remember if $this->getContainer() returns the container of the real application?

@alexislefebvre
Copy link
Collaborator

alexislefebvre commented May 3, 2018

@Jean85
Copy link
Contributor

Jean85 commented May 3, 2018

@alexislefebvre yes you get a real container, but it's not the same instance as $client->getContainer(); it's separated do avoid collisions or issues while testing through the client.

@alexislefebvre
Copy link
Collaborator

@shubaivan Did you try to use the client's container?

It may work with something like this:

$client = $this->makeClient();
$client->getContainer()->set('test', 2);
$crawler = $client->request('GET', '/contact');

@alexislefebvre
Copy link
Collaborator

I'm closing this issue for now. Feel free to reopen it if the problem is not solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants