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

Skip to content

[Testing] $request->getSession() === null through $client in WebTestCase #4445

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
guilhermeblanco opened this issue May 28, 2012 · 1 comment
Milestone

Comments

@guilhermeblanco
Copy link
Contributor

Hi,

I'm experimenting an issue with WebTestCase.
When I try to create a Client through WebTestCase::createClient, it returns me an instance of FrameworkBundle\Client.
FrameworkBundle\Client inherits from HttpKernel\Client, which override its inherited method filterRequest from BrowserKit\Client to remap the BrowserKit\Request into a better supportable HttpKernel\Request, which allows to include Session.

Everything seems fine, until you trigger the $client->request.
If any code relies on Session through $request->getSession, it is always null because it never gets injected into this Request.

Even though it's a broken patch, I would like to point you something.
By manually updating the FrameworkBundle\Client in method filterRequest and injecting the session manually, this issue gets fixed.

Attached you have a stack trace that I came from:

Fatal error: Call to a member function has() on a non-object in /var/www/nde/vendor/instaclick/locale-bundle/Lunetics/LocaleBundle/EventListener/LocaleDetectorListener.php on line 101

Call Stack:
    0.0001     629480   1. {main}() /usr/bin/phpunit:0
    0.0033    1246232   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
    0.0033    1247288   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:130
    0.6581   16807576   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:192
    0.6625   17363776   5. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/TextUI/TestRunner.php:325
    0.6841   17370136   6. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:705
    0.6844   17372120   7. PHPUnit_Framework_TestSuite->runTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:745
    0.6844   17372120   8. PHPUnit_Framework_TestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:772
    0.6845   17372120   9. PHPUnit_Framework_TestResult->run() /usr/share/php/PHPUnit/Framework/TestCase.php:751
    0.6856   17473104  10. PHPUnit_Framework_TestCase->runBare() /usr/share/php/PHPUnit/Framework/TestResult.php:649
    1.1901   37934200  11. PHPUnit_Framework_TestCase->runTest() /usr/share/php/PHPUnit/Framework/TestCase.php:804
    1.1901   37936384  12. ReflectionMethod->invokeArgs() /usr/share/php/PHPUnit/Framework/TestCase.php:942
    1.1901   37936440  13. IC\Bundle\Site\SteveBundle\Tests\Controller\ExceptionControllerTest->testExceptionWhenAuthenticated() /var/www/nde/src/bundle-site-steve/IC/Bundle/Site/SteveBundle/Tests/Controller/ExceptionControllerTest.php:0
    1.9963   54288160  14. Symfony\Component\BrowserKit\Client->request() /var/www/nde/src/bundle-site-steve/IC/Bundle/Site/SteveBundle/Tests/Controller/ExceptionControllerTest.php:77
    1.9979   54311000  15. Symfony\Bundle\FrameworkBundle\Client->doRequest() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:264
    1.9979   54311080  16. Symfony\Component\HttpKernel\Kernel->handle() /var/www/nde/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:81
    1.9997   54364776  17. Symfony\Bundle\FrameworkBundle\HttpKernel->handle() /var/www/nde/app/bootstrap.php.cache:575
    1.9998   54366464  18. Symfony\Component\HttpKernel\HttpKernel->handle() /var/www/nde/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/HttpKernel.php:50
    2.0254   54820928  19. Symfony\Component\HttpKernel\HttpKernel->handleException() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:78
    2.0255   54822912  20. Symfony\Component\HttpKernel\Debug\ContainerAwareTraceableEventDispatcher->dispatch() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:185
    2.0256   54825208  21. Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php:78
    2.0263   54833152  22. Symfony\Component\EventDispatcher\EventDispatcher->dispatch() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php:165
    2.0266   54834672  23. Symfony\Component\HttpKernel\Debug\ContainerAwareTraceableEventDispatcher->doDispatch() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:53
    2.0304   54849776  24. call_user_func() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php:139
    2.0304   54849832  25. Symfony\Component\HttpKernel\EventListener\ExceptionListener->onKernelException() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php:0
    2.0365   55032696  26. Symfony\Bundle\FrameworkBundle\HttpKernel->handle() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php:82
    2.0368   55036056  27. Symfony\Component\HttpKernel\HttpKernel->handle() /var/www/nde/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/HttpKernel.php:50
    2.0368   55036056  28. Symfony\Component\HttpKernel\HttpKernel->handleRaw() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:72
    2.0368   55037832  29. Symfony\Component\HttpKernel\Debug\ContainerAwareTraceableEventDispatcher->dispatch() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:109
    2.0375   55046816  30. Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php:78
    2.0386   55049768  31. Symfony\Component\EventDispatcher\EventDispatcher->dispatch() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php:165
    2.0392   55050600  32. Symfony\Component\HttpKernel\Debug\ContainerAwareTraceableEventDispatcher->doDispatch() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:53
    2.0585   55125008  33. call_user_func() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php:139
    2.0585   55125064  34. Lunetics\LocaleBundle\EventListener\LocaleDetectorListener->onRequest() /var/www/nde/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php:0
@guilhermeblanco
Copy link
Contributor Author

This seems to be related to #3741

fabpot added a commit that referenced this issue May 30, 2012
Commits
-------

3ad01c0 Update src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php

Discussion
----------

Fix broken web test cases with session already started

This PR fixes the issues highlighted in PR #4445 and #3741 by not overriding the session id if the session is already started.

---------------------------------------------------------------------------

by travisbot at 2012-05-29T19:06:33Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1468970) (merged 3ad01c0 into adf07f1).

---------------------------------------------------------------------------

by stloyd at 2012-05-29T19:07:54Z

Look at #2040. It's quite old, but there is at least some "reason" =)

---------------------------------------------------------------------------

by guilhermeblanco at 2012-05-29T20:57:27Z

@stloyd it seems to me that doing a session_id() (and now as $session->setId()) is still wrong.
The right should be a regeneration of the id then, which would still make my code pass. The problem is that setId method checks for a possible regeneration/double attribution, which prevents an already started session (from a WebTestCase) to live peacefully with the TestSessionListener, because it will always try to set an id.
So either we allow to regenerate ids or this code is not only useless for 2.1, but actually broken.

---------------------------------------------------------------------------

by drak at 2012-05-30T02:42:47Z

@guilhermeblanco - When I was doing the session refactoring I wanted to make exactly the patch this PR now but I didnt because I wast sure if there were reasons I was unaware of. @fabpot - I would merge this PR if it fixes the related tickets.
@fabpot fabpot closed this as completed May 30, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants