Skip to content

[Session] Fixed bug with TestListener #6362

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

Merged
merged 2 commits into from
Dec 15, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public function onKernelResponse(FilterResponseEvent $event)
}

if ($session = $event->getRequest()->getSession()) {
$session->save();
if ($session->isStarted()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be part of the condition above because if the session has not started, surely there is nothing more to do here at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, already merged this PR. It probably makes sense to move the check, but the tests must be fixed as well then as they won't pass anymore.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix it now.

$session->save();
}

$params = session_get_cookie_params();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected function tearDown()

public function testShouldSaveMasterRequestSession()
{
$this->sessionHasBeenStarted();
$this->sessionMustBeSaved();

$this->filterResponse(new Request());
Expand All @@ -66,6 +67,14 @@ public function testDoesNotDeleteCookieIfUsingSessionLifetime()
$this->assertEquals(0, reset($cookies)->getExpiresTime());
}

public function testUnstartedSessionIsNotSave()
{
$this->sessionHasNotBeenStarted();
$this->sessionMustNotBeSaved();

$this->filterResponse(new Request());
}

private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
{
$request->setSession($this->session);
Expand All @@ -92,6 +101,20 @@ private function sessionMustBeSaved()
->method('save');
}

private function sessionHasBeenStarted()
{
$this->session->expects($this->once())
->method('isStarted')
->will($this->returnValue(true));
}

private function sessionHasNotBeenStarted()
{
$this->session->expects($this->once())
->method('isStarted')
->will($this->returnValue(false));
}

private function getSession()
{
$mock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ public function setName($name)
*/
public function save()
{
if (!$this->started || $this->closed) {
throw new \RuntimeException("Trying to save a session that was not started yet or was already closed");
}
// nothing to do since we don't persist the session data
$this->closed = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public function regenerate($destroy = false, $lifetime = null)
*/
public function save()
{
if (!$this->started) {
throw new \RuntimeException("Trying to save a session that was not started yet or was already closed");
}

file_put_contents($this->getFilePath(), serialize($this->data));

// this is needed for Silex, where the session object is re-used across requests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public function regenerate($destroy = false, $lifetime = null);
* used for a storage object design for unit or functional testing where
* a real PHP session would interfere with testing, in which case it
* it should actually persist the session data if required.
*
* @throws \RuntimeException If the session is saved without being started, or if the session
* is already closed.
*/
public function save();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public function testMigrateDestroy()

public function testSave()
{
$this->session->start();
$this->session->save();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ public function testGetId()
$this->storage->start();
$this->assertNotEquals('', $this->storage->getId());
}

/**
* @expectedException RuntimeException
*/
public function testUnstartedSave()
{
$this->storage->save();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ public function testMultipleInstances()
$this->assertEquals('bar', $storage2->getBag('attributes')->get('foo'), 'values persist between instances');
}

/**
* @expectedException RuntimeException
*/
public function testSaveWithoutStart()
{
$storage1 = $this->getStorage();
$storage1->save();
}

private function getStorage()
{
$storage = new MockFileSessionStorage($this->sessionDir);
Expand Down