Skip to content

Cannot set session ID after the session has started. #5161

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

Closed
danielec7 opened this issue Aug 3, 2012 · 29 comments
Closed

Cannot set session ID after the session has started. #5161

danielec7 opened this issue Aug 3, 2012 · 29 comments

Comments

@danielec7
Copy link
Contributor

I'm starting to receive this error after upgrading to 2.1 during tests. I've seen there are other tickets with this issue but they are old and closed.

I'm receiving the error for example with this code:

<?php
$client = static::createClient();

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

$this->assertTrue(200 === $client->getResponse()->getStatusCode());

// Fill in the form and submit it
$form = $crawler->selectButton('submit')->form(array(
'mp_contacttype[name]'  => 'Test',
'mp_contacttype[email]'  => 'test@test.com',
'mp_contacttype[phone]'  => '34534534',
'mp_contacttype[message]'  => 'Test',
));

$client->submit($form);

The last client response is 500 error Cannot set session ID after the session has started. This test worked before. Has something changed?

@ghost
Copy link

ghost commented Aug 3, 2012

This was something some users were experiencing before. It's specifically related to testing. Do you have a bundle I can work with to investigate further?

@danielec7
Copy link
Contributor Author

I'm a afraid not, my public bundles are just libraries without WebTestCases.
I'll try publishing an example.

By the way I get the same error in a simpler case:

<?php

$client = static::createClient();
$crawler = $client->request('GET', '/');
$this->assertTrue(200 === $client->getResponse()->getStatusCode());

$crawler = $client->request('GET', '/other-page/');
$this->assertTrue(200 === $client->getResponse()->getStatusCode());

Just requesting another page, it gives the error. Does it work with you?

@jakzal
Copy link
Contributor

jakzal commented Aug 3, 2012

I'm not getting this on a 2.1 project. Might be it's related to migration. Have you changed storage_id in the app/config/config_test.yml to session.storage.mock_file?

@danielec7
Copy link
Contributor Author

Yes I tried both session.storage.filesystem and session.storage.mock_file. Same effect. I'm also using LiipFunctionalTestBundle, I'll try to disable some bundles to locate the problem.

@danielec7
Copy link
Contributor Author

It appears that the problem is related to FOSFacebookBundle, disabling it, the tests work again. I'll try to report the issue there.

@maoueh
Copy link
Contributor

maoueh commented Aug 3, 2012

Check this PR #3741 and PR #79 on FOSFacebookBundle for more information. There is other issues related to this in the FOSFacebookBundle issues tracker.

@bamarni
Copy link
Contributor

bamarni commented Aug 30, 2012

I'm facing this issue even though I don't use FOSFacebookBundle, it occurs when I try to authenticate a user in a functional test, it looks related to the cookie I'm setting to fake a previous session, is there anything I'm doing wrong within this test?

https://gist.github.com/3424003

@bamarni
Copy link
Contributor

bamarni commented Sep 10, 2012

@Drak : any clue?

@gquemener
Copy link
Contributor

FriendsOfSymfony/FOSFacebookBundle#183 might address this issue. @iJanki @Drak @bamarni

@bamarni
Copy link
Contributor

bamarni commented Sep 10, 2012

@gquemener : apparently this can also occurs when not using FOSFacebookBundle.

I've added added my failing test in a symfony standard edition fork : https://github.com/bamarni/symfony-standard/blob/failing-test/src/Acme/DemoBundle/Tests/Controller/DemoControllerTest.php

Is there anything I'm doing wrong? cc @Drak

I've also commited my composer.lock so you can get the exact versions it fails with.

@havvg
Copy link
Contributor

havvg commented Oct 17, 2012

Any progress on this?

I'm facing the same issue, but when following redirects, at https://github.com/symfony/symfony/tree/ee7597576c4244d51d777001e570111ba59d9d72

framework:
    test: true
    profiler:
        only_exceptions: false
    session:
        storage_id: 'session.storage.mock_file'
        handler_id: false
<?php

namespace Ormigo\Bundle\OrmigoBundle\Tests\Controller;

use Ormigo\Tests\AbstractFunctionalTest;

use Ormigo\Model\Banking\BankQuery;

class BankControllerTest extends AbstractFunctionalTest
{
    // ..

    public function testByBicIsRedirectingToShow()
    {
        $bank = BankQuery::create()->findOneByBic('HYVEDEMM488');
        $client = static::createClient();

        $client->request('GET', sprintf('/bank/by-bic/%s.de_DE.json', $bank->getBic()));
        $this->assertRedirect($client->getResponse(), sprintf('/bank/%s.de_DE.json', $bank->getId()));

        $client->followRedirect();
        $this->assertSuccessfulJsonResponse($client->getResponse());  // 500 "Cannot set session ID .."
    }

    // ..
}

@danielec7
Copy link
Contributor Author

Nothing changed for me, I still get the original problems using the latest trees.

@Pajk
Copy link

Pajk commented Oct 31, 2012

I have same problem. I am trying to use Behat with Mink but I think that is not the issue because I see this error when accessing app_test.php from browser too.

Tests are passing when I comment line #54 in TestSessionListener.php.

My composer.json:

{ "require" : {
        "symfony/symfony": "2.1.*",
        "doctrine/orm": ">=2.2.3,<2.4-dev",
        "doctrine/doctrine-bundle": "1.0.*",
        "twig/extensions": "1.0.*",
        "symfony/assetic-bundle": "2.1.*",
        "symfony/swiftmailer-bundle": "2.1.*",
        "symfony/monolog-bundle": "2.1.*",
        "sensio/distribution-bundle": "2.1.*",
        "sensio/framework-extra-bundle": "2.1.*",
        "sensio/generator-bundle": "2.1.*",
        "jms/security-extra-bundle": "1.2.*",
        "jms/di-extra-bundle": "1.1.*",
        "Beryllium/CacheBundle": "dev-master",
        "friendsofsymfony/jsrouting-bundle": "1.0.x-dev",
        "friendsofsymfony/facebook-bundle": "dev-master",
        "friendsofsymfony/user-bundle": "*",
        "stof/doctrine-extensions-bundle": "dev-master",
        "doctrine/doctrine-migrations-bundle": "dev-master",
        "jms/i18n-routing-bundle": "dev-master",
        "behat/behat":  "*",
        "behat/mink":   "*",
        "behat/symfony2-extension":      "*",
        "behat/mink-extension":          "*",
        "behat/mink-goutte-driver":      "*",
        "behat/mink-browserkit-driver":  "*",
        "behat/mink-selenium2-driver":   "*",
        "sanpi/behatch-contexts": "*"
}}```

@havvg
Copy link
Contributor

havvg commented Oct 31, 2012

@Pajk I believe you have the wrong session storage set.

framework:
    session:
        storage_id: 'session.storage.native'
        handler_id: 'session.handler.native_file'

This will work with Behat/Mink on an actual browser.

@Pajk
Copy link

Pajk commented Oct 31, 2012

Thanks for suggestion @havvg but it didn't help. There is a bug in Facebook bundle, [1] fixed it.

[1] FriendsOfSymfony/FOSFacebookBundle#183

@phidah
Copy link
Contributor

phidah commented Nov 28, 2012

Getting the same issue here - no FOSFacebookBundle. Tried both the session configurations mentioned above - didn't help.

Problem occurs when following redirect through the crawler after a login.

@Squazic
Copy link

Squazic commented Nov 29, 2012

I'm getting the same issue with LiipFunctionalTestBundle and the problem lies with this line:

$client->getCookieJar()->set(new Cookie($container->get('session')->getName(), true));

When I remove it in my tests, it works fine. I don't know why yet. Maybe someone else has more insight on this.

@Squazic
Copy link

Squazic commented Nov 29, 2012

An update: it seems that when I configure the session storage name to MOCKSESSID, the name symfony defaults to, it gives me the error. However, If I change the session storage name to anything else in config under framework: session: name:, the tests work again.

@binarious
Copy link

I'm getting the same issue with the test case of iJanki without using FOSFacebookBundle. Changing the session name didn't help.

@phidah
Copy link
Contributor

phidah commented Apr 3, 2013

I have this issue even without FOSFacebookBundle.

@jakzal
Copy link
Contributor

jakzal commented Apr 3, 2013

@phidah would it be possible to set up a simple standard edition project reproducing this issue?

@ghost
Copy link

ghost commented Apr 5, 2013

I'd be happy to try and investigate for you if you can setup a project I can just download and get the error with.

@bamarni
Copy link
Contributor

bamarni commented Apr 5, 2013

@ghost
Copy link

ghost commented Apr 5, 2013

@bamarni - Composer throws a wobbly when I try to get the deps. Maybe you can mail me at drak at zikula dot org so we dont pollute the ticket. I'm quite busy but if I can at least get the test env working, then I can get to it. I'm working on some session PRs atm so it's a good time.

@jakzal
Copy link
Contributor

jakzal commented Apr 5, 2013

@bamarni your test fails because there's no session ID sent with the cookie. Also, session needs to be saved before sending a request (in fact before creating a cookie too). see: bamarni/symfony-standard@317ce94#commitcomment-2953995

@ghost
Copy link

ghost commented Apr 5, 2013

@jakzal - do you think you could open a cookbook entry or something in the Symfony2 docs about this? If it's documented once, it might save quite a bit of bother.

@bamarni
Copy link
Contributor

bamarni commented Apr 5, 2013

@jakzal : thx for the explanations and the code snippet, I understand what was wrong now.

@jakzal
Copy link
Contributor

jakzal commented Apr 5, 2013

@Drak I will.

@fabpot
Copy link
Member

fabpot commented Apr 20, 2013

closing as symfony/symfony-docs#2468 has been merged now.

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

No branches or pull requests