From e8f0972951a439d5d6c4277be78040bb7d9bd9d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 27 Mar 2017 01:47:14 +0200 Subject: [PATCH 1/2] Revert SessionListener rename for BC SessionListener and TestSessionListener are extended outside of Symfony in Silex. These classes cannot be renamed. --- .../EventListener/SessionListener.php | 8 +- .../EventListener/TestSessionListener.php | 22 +---- .../Resources/config/session.xml | 2 +- .../FrameworkBundle/Resources/config/test.xml | 2 +- .../EventListener/AbstractSessionListener.php | 54 ------------ .../AbstractTestSessionListener.php | 84 ------------------- .../ContainerAwareSessionListener.php | 52 ++++++++++++ .../ContainerAwareTestSessionListener.php | 52 ++++++++++++ .../EventListener/SessionListener.php | 42 +++++----- .../EventListener/TestSessionListener.php | 68 +++++++++++---- .../EventListener/TestSessionListenerTest.php | 3 +- 11 files changed, 187 insertions(+), 202 deletions(-) delete mode 100644 src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php delete mode 100644 src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php create mode 100644 src/Symfony/Component/HttpKernel/EventListener/ContainerAwareSessionListener.php create mode 100644 src/Symfony/Component/HttpKernel/EventListener/ContainerAwareTestSessionListener.php diff --git a/src/Symfony/Bundle/FrameworkBundle/EventListener/SessionListener.php b/src/Symfony/Bundle/FrameworkBundle/EventListener/SessionListener.php index 07bfe7496d111..85bc2d49cc01d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/EventListener/SessionListener.php +++ b/src/Symfony/Bundle/FrameworkBundle/EventListener/SessionListener.php @@ -11,17 +11,17 @@ namespace Symfony\Bundle\FrameworkBundle\EventListener; -use Symfony\Component\HttpKernel\EventListener\SessionListener as BaseSessionListener; +use Symfony\Component\HttpKernel\EventListener\ContainerAwareSessionListener; -@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use %s instead.', SessionListener::class, BaseSessionListener::class), E_USER_DEPRECATED); +@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use %s instead.', ContainerAwareSessionListener::class, BaseSessionListener::class), E_USER_DEPRECATED); /** * Sets the session in the request. * * @author Fabien Potencier * - * @deprecated since version 3.3, to be removed in 4.0. Use {@link BaseSessionListener} instead + * @deprecated since version 3.3, to be removed in 4.0. Use {@link ContainerAwareSessionListener} instead */ -class SessionListener extends BaseSessionListener +class SessionListener extends ContainerAwareSessionListener { } diff --git a/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php b/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php index 703be8ff3beda..11a81492e6f90 100644 --- a/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php +++ b/src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php @@ -13,31 +13,15 @@ @trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use Symfony\Component\HttpKernel\EventListener\TestSessionListener instead.', TestSessionListener::class), E_USER_DEPRECATED); -use Symfony\Component\HttpKernel\EventListener\AbstractTestSessionListener; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpKernel\EventListener\ContainerAwareTestSessionListener; /** * TestSessionListener. * * @author Fabien Potencier * - * @deprecated since version 3.3, to be removed in 4.0. + * @deprecated since version 3.3, to be removed in 4.0. Use {@link ContainerAwareTestSessionListener} instead */ -class TestSessionListener extends AbstractTestSessionListener +class TestSessionListener extends ContainerAwareTestSessionListener { - protected $container; - - public function __construct(ContainerInterface $container) - { - $this->container = $container; - } - - protected function getSession() - { - if (!$this->container->has('session')) { - return; - } - - return $this->container->get('session'); - } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml index a4e4afdba5683..90e93af697e51 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml @@ -51,7 +51,7 @@ - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml index 943c56310b93c..aa1aad3b31cdc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml @@ -20,7 +20,7 @@ - + diff --git a/src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php b/src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php deleted file mode 100644 index 7a6c20734bf90..0000000000000 --- a/src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php +++ /dev/null @@ -1,54 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpKernel\EventListener; - -use Symfony\Component\HttpFoundation\Session\SessionInterface; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; -use Symfony\Component\HttpKernel\KernelEvents; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; - -/** - * Sets the session in the request. - * - * @author Johannes M. Schmitt - */ -abstract class AbstractSessionListener implements EventSubscriberInterface -{ - public function onKernelRequest(GetResponseEvent $event) - { - if (!$event->isMasterRequest()) { - return; - } - - $request = $event->getRequest(); - $session = $this->getSession(); - if (null === $session || $request->hasSession()) { - return; - } - - $request->setSession($session); - } - - public static function getSubscribedEvents() - { - return array( - KernelEvents::REQUEST => array('onKernelRequest', 128), - ); - } - - /** - * Gets the session object. - * - * @return SessionInterface|null A SessionInterface instance or null if no session is available - */ - abstract protected function getSession(); -} diff --git a/src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php b/src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php deleted file mode 100644 index eb6e66c0eb6ed..0000000000000 --- a/src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpKernel\EventListener; - -use Symfony\Component\HttpFoundation\Cookie; -use Symfony\Component\HttpFoundation\Session\SessionInterface; -use Symfony\Component\HttpKernel\KernelEvents; -use Symfony\Component\HttpKernel\Event\FilterResponseEvent; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; - -/** - * TestSessionListener. - * - * Saves session in test environment. - * - * @author Bulat Shakirzyanov - * @author Fabien Potencier - */ -abstract class AbstractTestSessionListener implements EventSubscriberInterface -{ - public function onKernelRequest(GetResponseEvent $event) - { - if (!$event->isMasterRequest()) { - return; - } - - // bootstrap the session - $session = $this->getSession(); - if (!$session) { - return; - } - - $cookies = $event->getRequest()->cookies; - - if ($cookies->has($session->getName())) { - $session->setId($cookies->get($session->getName())); - } - } - - /** - * Checks if session was initialized and saves if current request is master - * Runs on 'kernel.response' in test environment. - * - * @param FilterResponseEvent $event - */ - public function onKernelResponse(FilterResponseEvent $event) - { - if (!$event->isMasterRequest()) { - return; - } - - $session = $event->getRequest()->getSession(); - if ($session && $session->isStarted()) { - $session->save(); - $params = session_get_cookie_params(); - $event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly'])); - } - } - - public static function getSubscribedEvents() - { - return array( - KernelEvents::REQUEST => array('onKernelRequest', 192), - KernelEvents::RESPONSE => array('onKernelResponse', -128), - ); - } - - /** - * Gets the session object. - * - * @return SessionInterface|null A SessionInterface instance or null if no session is available - */ - abstract protected function getSession(); -} diff --git a/src/Symfony/Component/HttpKernel/EventListener/ContainerAwareSessionListener.php b/src/Symfony/Component/HttpKernel/EventListener/ContainerAwareSessionListener.php new file mode 100644 index 0000000000000..4fce1dea3fa13 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/EventListener/ContainerAwareSessionListener.php @@ -0,0 +1,52 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\EventListener; + +use Psr\Container\ContainerInterface; +use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; +use Symfony\Component\HttpFoundation\Session\SessionInterface; + +/** + * Sets the session in the request. + * + * @author Fabien Potencier + * + * @final since version 3.3 + */ +class ContainerAwareSessionListener extends SessionListener implements ServiceSubscriberInterface +{ + private $container; + + public function __construct(ContainerInterface $container) + { + $this->container = $container; + } + + protected function getSession() + { + if (!$this->container->has('session')) { + return; + } + + return $this->container->get('session'); + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedServices() + { + return array( + 'session' => '?'.SessionInterface::class, + ); + } +} diff --git a/src/Symfony/Component/HttpKernel/EventListener/ContainerAwareTestSessionListener.php b/src/Symfony/Component/HttpKernel/EventListener/ContainerAwareTestSessionListener.php new file mode 100644 index 0000000000000..3e9b8d873bdce --- /dev/null +++ b/src/Symfony/Component/HttpKernel/EventListener/ContainerAwareTestSessionListener.php @@ -0,0 +1,52 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\EventListener; + +use Psr\Container\ContainerInterface; +use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; +use Symfony\Component\HttpFoundation\Session\SessionInterface; + +/** + * Sets the session in the request. + * + * @author Fabien Potencier + * + * @final since version 3.3 + */ +class ContainerAwareTestSessionListener extends TestSessionListener implements ServiceSubscriberInterface +{ + private $container; + + public function __construct(ContainerInterface $container) + { + $this->container = $container; + } + + protected function getSession() + { + if (!$this->container->has('session')) { + return; + } + + return $this->container->get('session'); + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedServices() + { + return array( + 'session' => '?'.SessionInterface::class, + ); + } +} diff --git a/src/Symfony/Component/HttpKernel/EventListener/SessionListener.php b/src/Symfony/Component/HttpKernel/EventListener/SessionListener.php index a6a7de7550a51..2d6adfa0122d2 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/SessionListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/SessionListener.php @@ -11,42 +11,44 @@ namespace Symfony\Component\HttpKernel\EventListener; -use Psr\Container\ContainerInterface; -use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Sets the session in the request. * - * @author Fabien Potencier - * - * @final since version 3.3 + * @author Johannes M. Schmitt */ -class SessionListener extends AbstractSessionListener implements ServiceSubscriberInterface +abstract class SessionListener implements EventSubscriberInterface { - private $container; - - public function __construct(ContainerInterface $container) + public function onKernelRequest(GetResponseEvent $event) { - $this->container = $container; - } + if (!$event->isMasterRequest()) { + return; + } - protected function getSession() - { - if (!$this->container->has('session')) { + $request = $event->getRequest(); + $session = $this->getSession(); + if (null === $session || $request->hasSession()) { return; } - return $this->container->get('session'); + $request->setSession($session); } - /** - * {@inheritdoc} - */ - public static function getSubscribedServices() + public static function getSubscribedEvents() { return array( - 'session' => '?'.SessionInterface::class, + KernelEvents::REQUEST => array('onKernelRequest', 128), ); } + + /** + * Gets the session object. + * + * @return SessionInterface|null A SessionInterface instance or null if no session is available + */ + abstract protected function getSession(); } diff --git a/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php b/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php index 769eb3b6d033c..19a61f169bdf9 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php @@ -11,42 +11,74 @@ namespace Symfony\Component\HttpKernel\EventListener; -use Psr\Container\ContainerInterface; -use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; +use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\Session\SessionInterface; +use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\HttpKernel\Event\FilterResponseEvent; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** - * Sets the session in the request. + * TestSessionListener. * - * @author Fabien Potencier + * Saves session in test environment. * - * @final since version 3.3 + * @author Bulat Shakirzyanov + * @author Fabien Potencier */ -class TestSessionListener extends AbstractTestSessionListener implements ServiceSubscriberInterface +abstract class TestSessionListener implements EventSubscriberInterface { - private $container; - - public function __construct(ContainerInterface $container) + public function onKernelRequest(GetResponseEvent $event) { - $this->container = $container; - } + if (!$event->isMasterRequest()) { + return; + } - protected function getSession() - { - if (!$this->container->has('session')) { + // bootstrap the session + $session = $this->getSession(); + if (!$session) { return; } - return $this->container->get('session'); + $cookies = $event->getRequest()->cookies; + + if ($cookies->has($session->getName())) { + $session->setId($cookies->get($session->getName())); + } } /** - * {@inheritdoc} + * Checks if session was initialized and saves if current request is master + * Runs on 'kernel.response' in test environment. + * + * @param FilterResponseEvent $event */ - public static function getSubscribedServices() + public function onKernelResponse(FilterResponseEvent $event) + { + if (!$event->isMasterRequest()) { + return; + } + + $session = $event->getRequest()->getSession(); + if ($session && $session->isStarted()) { + $session->save(); + $params = session_get_cookie_params(); + $event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly'])); + } + } + + public static function getSubscribedEvents() { return array( - 'session' => '?'.SessionInterface::class, + KernelEvents::REQUEST => array('onKernelRequest', 192), + KernelEvents::RESPONSE => array('onKernelResponse', -128), ); } + + /** + * Gets the session object. + * + * @return SessionInterface|null A SessionInterface instance or null if no session is available + */ + abstract protected function getSession(); } diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php index 2e51d42d150f3..84474594aeba4 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\EventListener\TestSessionListener; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpFoundation\Session\SessionInterface; @@ -39,7 +40,7 @@ class TestSessionListenerTest extends TestCase protected function setUp() { - $this->listener = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\EventListener\AbstractTestSessionListener'); + $this->listener = $this->getMockForAbstractClass(TestSessionListener::class); $this->session = $this->getSession(); } From 7b0ce40cd8b1bace1d2b2ca7efc074083863ea7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 27 Mar 2017 01:49:11 +0200 Subject: [PATCH 2/2] Add test on *SessionListener* classes --- .../ContainerAwareSessionListenerTest.php | 95 ++++++++++++ .../ContainerAwareTestSessionListenerTest.php | 95 ++++++++++++ .../EventListener/SessionListenerTest.php | 139 ++++++++++++++++++ 3 files changed, 329 insertions(+) create mode 100644 src/Symfony/Component/HttpKernel/Tests/EventListener/ContainerAwareSessionListenerTest.php create mode 100644 src/Symfony/Component/HttpKernel/Tests/EventListener/ContainerAwareTestSessionListenerTest.php create mode 100644 src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/ContainerAwareSessionListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/ContainerAwareSessionListenerTest.php new file mode 100644 index 0000000000000..4b7a3479cd3cf --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/ContainerAwareSessionListenerTest.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\EventListener; + +use PHPUnit\Framework\TestCase; +use Psr\Container\ContainerInterface; +use Symfony\Component\HttpKernel\EventListener\ContainerAwareSessionListener; +use Symfony\Component\HttpFoundation\Session\SessionInterface; + +class ContainerAwareSessionListenerTest extends TestCase +{ + /** + * @var ContainerInterface + */ + private $container; + + /** + * @var SessionInterface + */ + private $session; + + /** + * @var ContainerAwareSessionListener + */ + private $listener; + + protected function setUp() + { + $this->container = $this->getMockBuilder(ContainerInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $this->session = $this->getMockBuilder(SessionInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->listener = new ContainerAwareSessionListener($this->container); + } + + public function testShouldGetSessionService() + { + $this->containerHavingSession(); + + $this->assertSame($this->session, $this->getSession()); + } + + public function testShouldGetSessionNullWhenServiceIsNotDefined() + { + $this->containerNotHavingSession(); + + $this->assertNull($this->getSession()); + } + + private function getSession() + { + $method = (new \ReflectionClass($this->listener)) + ->getMethod('getSession'); + $method->setAccessible(true); + + return $method->invoke($this->listener); + } + + private function containerHavingSession() + { + $this->container->expects($this->any()) + ->method('has') + ->with($this->equalTo('session')) + ->will($this->returnValue(true)); + + $this->container->expects($this->any()) + ->method('get') + ->with($this->equalTo('session')) + ->will($this->returnValue($this->session)); + } + + private function containerNotHavingSession() + { + $this->container->expects($this->any()) + ->method('has') + ->with($this->equalTo('session')) + ->will($this->returnValue(false)); + + $this->container->expects($this->never()) + ->method('get') + ->with($this->equalTo('session')); + } +} diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/ContainerAwareTestSessionListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/ContainerAwareTestSessionListenerTest.php new file mode 100644 index 0000000000000..0c3182c8bcd84 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/ContainerAwareTestSessionListenerTest.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\EventListener; + +use PHPUnit\Framework\TestCase; +use Psr\Container\ContainerInterface; +use Symfony\Component\HttpKernel\EventListener\ContainerAwareTestSessionListener; +use Symfony\Component\HttpFoundation\Session\SessionInterface; + +class ContainerAwareTestSessionListenerTest extends TestCase +{ + /** + * @var ContainerInterface + */ + private $container; + + /** + * @var SessionInterface + */ + private $session; + + /** + * @var ContainerAwareTestSessionListener + */ + private $listener; + + protected function setUp() + { + $this->container = $this->getMockBuilder(ContainerInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $this->session = $this->getMockBuilder(SessionInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->listener = new ContainerAwareTestSessionListener($this->container); + } + + public function testShouldGetSessionService() + { + $this->containerHavingSession(); + + $this->assertSame($this->session, $this->getSession()); + } + + public function testShouldGetSessionNullWhenServiceIsNotDefined() + { + $this->containerNotHavingSession(); + + $this->assertNull($this->getSession()); + } + + private function getSession() + { + $method = (new \ReflectionClass($this->listener)) + ->getMethod('getSession'); + $method->setAccessible(true); + + return $method->invoke($this->listener); + } + + private function containerHavingSession() + { + $this->container->expects($this->any()) + ->method('has') + ->with($this->equalTo('session')) + ->will($this->returnValue(true)); + + $this->container->expects($this->any()) + ->method('get') + ->with($this->equalTo('session')) + ->will($this->returnValue($this->session)); + } + + private function containerNotHavingSession() + { + $this->container->expects($this->any()) + ->method('has') + ->with($this->equalTo('session')) + ->will($this->returnValue(false)); + + $this->container->expects($this->never()) + ->method('get') + ->with($this->equalTo('session')); + } +} diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php new file mode 100644 index 0000000000000..2cf49380a706f --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php @@ -0,0 +1,139 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\EventListener; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\EventListener\SessionListener; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpFoundation\Session\SessionInterface; + +/** + * SessionListenerTest. + * + * Tests SessionListener. + */ +class SessionListenerTest extends TestCase +{ + /** + * @var Request + */ + private $request; + + /** + * @var SessionListener + */ + private $listener; + + /** + * @var SessionInterface + */ + private $session; + + protected function setUp() + { + $this->request = $this->getMockBuilder(Request::class) + ->disableOriginalConstructor() + ->getMock(); + $this->listener = $this->getMockForAbstractClass(SessionListener::class); + $this->session = $this->getSession(); + } + + public function testShouldSetSessionOnMasterRequest() + { + $this->sessionIsDefined(); + $this->sessionMustBeSet(); + + $this->kernelRequest($this->request); + } + + public function testShouldNotSetSessionOnSubRequest() + { + $this->sessionIsDefined(); + $this->sessionMustNotBeSet(); + + $this->kernelRequest(new Request(), HttpKernelInterface::SUB_REQUEST); + } + + public function testShouldNotSetNullSession() + { + $this->sessionIsNull(); + $this->sessionMustNotBeSet(); + + $this->kernelRequest(new Request()); + } + + public function testShouldNotReplaceSession() + { + $this->sessionIsDefined(); + $this->sessionMustNotBeSet(); + + $this->kernelRequest(new Request()); + } + + private function kernelRequest(Request $request, $type = HttpKernelInterface::MASTER_REQUEST) + { + $response = new Response(); + $kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock(); + $event = new GetResponseEvent($kernel, $request, $type); + $event->setResponse($response); + + $this->listener->onKernelRequest($event); + + $this->assertSame($response, $event->getResponse()); + } + + private function sessionIsDefined() + { + $this->listener->expects($this->any()) + ->method('getSession') + ->will($this->returnValue($this->session)); + } + + private function sessionIsNull() + { + $this->listener->expects($this->any()) + ->method('getSession') + ->will($this->returnValue(null)); + } + + private function sessionAlreadySet() + { + $this->request->expects($this->any()) + ->method('getSession') + ->will($this->returnValue(clone $this->session)); + } + + private function sessionMustBeSet() + { + $this->request->expects($this->once()) + ->method('setSession') + ->with($this->identicalTo($this->session)); + } + + private function sessionMustNotBeSet() + { + $this->request->expects($this->never()) + ->method('setSession'); + } + + private function getSession() + { + $mock = $this->getMockBuilder(SessionInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + return $mock; + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy