Commit bd523d59 authored by Fabien Potencier's avatar Fabien Potencier

merged branch romainneutron/Sessions (PR #565)

This PR was squashed before being merged into the master branch (closes #565).

Commits
-------

55dce6b5 Test session service provider with routes that does not use session

Discussion
----------

Test session service provider with routes that does not use session

Hello,

Since symfony/Symfony@098b593591, a bug occurs with `SessionServiceProvider` :

When using `HttpKernel\Client`, setting `$app['session.test'] = true;` and querying the application multiple routes in a single unit test, a `RuntimeException` is thrown by `Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage`.

For instance :

```php
use Silex\WebTestCase;
use Silex\Application;
use Silex\Provider\SessionServiceProvider;

class unitTest extends WebTestCase
{
    public function testUn()
    {
        $client = $this->createClient();
        $client->request('GET', '/');
        $client->request('GET', '/');
    }

    public function createApplication()
    {
        $app = new Application();

        $app->register(new SessionServiceProvider(), array(
            'session.test' => true
        ));

        $app['debug'] = true;
        $app['exception_handler']->disable();

        $app->get('/', function(Application $app) {
            return 'BOOM';
        });

        return $app;
    }
}
```
results in
```
1) unitTest::testUn
RuntimeException: Trying to save a session that was not started yet or was already closed

/Users/romain/Documents/workspace/testSess/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php:101
/Users/romain/Documents/workspace/testSess/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Session/Session.php:184
/Users/romain/Documents/workspace/testSess/vendor/silex/silex/src/Silex/Provider/SessionServiceProvider.php:107
/Users/romain/Documents/workspace/testSess/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:164
/Users/romain/Documents/workspace/testSess/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php:53
/Users/romain/Documents/workspace/testSess/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:169
/Users/romain/Documents/workspace/testSess/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:151
/Users/romain/Documents/workspace/testSess/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:73
/Users/romain/Documents/workspace/testSess/vendor/silex/silex/src/Silex/Application.php:469
/Users/romain/Documents/workspace/testSess/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Client.php:61
/Users/romain/Documents/workspace/testSess/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:264
/Users/romain/Documents/workspace/testSess/bingo.php:13
```

Of course, this test currently passes as the bug happens with latest `2.1.x-dev` source whereas `composer.json` settings ask to install latest stable tag.

This PR adds a test that fails with latest symfony 2.1.x.

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

by romainneutron at 2012-12-17T22:19:24Z

It seems the fix I've added is good enough, so, finally this PR brings a solution to the problem :)
parents c2dff3ca 55dce6b5
...@@ -103,7 +103,8 @@ class SessionServiceProvider implements ServiceProviderInterface ...@@ -103,7 +103,8 @@ class SessionServiceProvider implements ServiceProviderInterface
return; return;
} }
if ($session = $event->getRequest()->getSession()) { $session = $event->getRequest()->getSession();
if ($session && $session->isStarted()) {
$session->save(); $session->save();
$params = session_get_cookie_params(); $params = session_get_cookie_params();
......
...@@ -14,8 +14,7 @@ namespace Silex\Tests\Provider; ...@@ -14,8 +14,7 @@ namespace Silex\Tests\Provider;
use Silex\Application; use Silex\Application;
use Silex\WebTestCase; use Silex\WebTestCase;
use Silex\Provider\SessionServiceProvider; use Silex\Provider\SessionServiceProvider;
use Symfony\Component\HttpKernel\Client;
use Symfony\Component\HttpFoundation\Request;
/** /**
* SessionProvider test cases. * SessionProvider test cases.
...@@ -77,4 +76,32 @@ class SessionServiceProviderTest extends WebTestCase ...@@ -77,4 +76,32 @@ class SessionServiceProviderTest extends WebTestCase
return $app; return $app;
} }
public function testWithRoutesThatDoesNotUseSession()
{
$app = new Application();
$app->register(new SessionServiceProvider(), array(
'session.test' => true,
));
$app->get('/', function () {
return 'A welcome page.';
});
$app->get('/robots.txt', function () {
return 'Informations for robots.';
});
$app['debug'] = true;
$app['exception_handler']->disable();
$client = new Client($app);
$client->request('get', '/');
$this->assertEquals('A welcome page.', $client->getResponse()->getContent());
$client->request('get', '/robots.txt');
$this->assertEquals('Informations for robots.', $client->getResponse()->getContent());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment