From 6b9ee87b6239e8d27ee297aae8a1071966da5e48 Mon Sep 17 00:00:00 2001 From: Baldur Rensch Date: Fri, 14 Dec 2012 11:26:41 -0800 Subject: [PATCH] [Session] Fixed a bug with the TestListener When the session is not started, the test listener would still save the session causing the session data to be emptied. --- .../EventListener/TestSessionListener.php | 4 +++- .../EventListener/TestSessionListenerTest.php | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php b/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php index 1969f71e9e..95592f7505 100644 --- a/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php +++ b/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php @@ -68,7 +68,9 @@ class TestSessionListener implements EventSubscriberInterface } if ($session = $event->getRequest()->getSession()) { - $session->save(); + if ($session->isStarted()) { + $session->save(); + } $params = session_get_cookie_params(); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TestSessionListenerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TestSessionListenerTest.php index 88fadf2551..cfaf8e80a9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TestSessionListenerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TestSessionListenerTest.php @@ -43,6 +43,7 @@ class TestSessionListenerTest extends \PHPUnit_Framework_TestCase public function testShouldSaveMasterRequestSession() { + $this->sessionHasBeenStarted(); $this->sessionMustBeSaved(); $this->filterResponse(new Request()); @@ -66,6 +67,14 @@ class TestSessionListenerTest extends \PHPUnit_Framework_TestCase $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); @@ -92,6 +101,20 @@ class TestSessionListenerTest extends \PHPUnit_Framework_TestCase ->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')