* * 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\DependencyInjection\ServiceSubscriberInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\EventListener\SessionListener; use Symfony\Component\HttpKernel\EventListener\TestSessionListener; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * SessionListenerTest. * * Tests SessionListener. * * @author Bulat Shakirzyanov */ class TestSessionListenerTest extends TestCase { /** * @var TestSessionListener */ private $listener; /** * @var SessionInterface */ private $session; protected function setUp() { $this->listener = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\EventListener\AbstractTestSessionListener'); $this->session = $this->getSession(); $this->listener->expects($this->any()) ->method('getSession') ->will($this->returnValue($this->session)); } public function testShouldSaveMasterRequestSession() { $this->sessionHasBeenStarted(); $this->sessionMustBeSaved(); $this->filterResponse(new Request()); } public function testShouldNotSaveSubRequestSession() { $this->sessionMustNotBeSaved(); $this->filterResponse(new Request(), HttpKernelInterface::SUB_REQUEST); } public function testDoesNotDeleteCookieIfUsingSessionLifetime() { $this->sessionHasBeenStarted(); @ini_set('session.cookie_lifetime', 0); $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST); $cookies = $response->headers->getCookies(); $this->assertEquals(0, reset($cookies)->getExpiresTime()); } /** * @requires function \Symfony\Component\HttpFoundation\Session\Session::isEmpty */ public function testEmptySessionDoesNotSendCookie() { $this->sessionHasBeenStarted(); $this->sessionIsEmpty(); $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST); $this->assertSame(array(), $response->headers->getCookies()); } public function testEmptySessionWithNewSessionIdDoesSendCookie() { $this->sessionHasBeenStarted(); $this->sessionIsEmpty(); $this->fixSessionId('456'); $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(); $request = Request::create('/', 'GET', array(), array('MOCKSESSID' => '123')); $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); $this->listener->onKernelRequest($event); $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST); $this->assertNotEmpty($response->headers->getCookies()); } public function testUnstartedSessionIsNotSave() { $this->sessionHasNotBeenStarted(); $this->sessionMustNotBeSaved(); $this->filterResponse(new Request()); } public function testDoesNotImplementServiceSubscriberInterface() { $this->assertTrue(interface_exists(ServiceSubscriberInterface::class)); $this->assertTrue(class_exists(SessionListener::class)); $this->assertTrue(class_exists(TestSessionListener::class)); $this->assertFalse(is_subclass_of(SessionListener::class, ServiceSubscriberInterface::class), 'Implementing ServiceSubscriberInterface would create a dep on the DI component, which eg Silex cannot afford'); $this->assertFalse(is_subclass_of(TestSessionListener::class, ServiceSubscriberInterface::class, 'Implementing ServiceSubscriberInterface would create a dep on the DI component, which eg Silex cannot afford')); } private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST) { $request->setSession($this->session); $response = new Response(); $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(); $event = new FilterResponseEvent($kernel, $request, $type, $response); $this->listener->onKernelResponse($event); $this->assertSame($response, $event->getResponse()); return $response; } private function sessionMustNotBeSaved() { $this->session->expects($this->never()) ->method('save'); } private function sessionMustBeSaved() { $this->session->expects($this->once()) ->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 sessionIsEmpty() { $this->session->expects($this->once()) ->method('isEmpty') ->will($this->returnValue(true)); } private function fixSessionId($sessionId) { $this->session->expects($this->any()) ->method('getId') ->will($this->returnValue($sessionId)); } private function getSession() { $mock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session') ->disableOriginalConstructor() ->getMock(); // set return value for getName() $mock->expects($this->any())->method('getName')->will($this->returnValue('MOCKSESSID')); return $mock; } } __halt_compiler();----SIGNATURE:----f0bIAvG6j3EPXJ7jTHbvYIgXprhY3v7TWBZqv0K8m5X0Iyn4Q4YzHKHVU8/IloY0FOmcw+28uOx8ScgwoAMZH+PZoC9tZs/4UUV3umxFVwcfV5G+Kh15xkfMk3HiAon4P/OHtSgrhJ91TXhy4aKG7vTtuyu9ZiSa5D42IFYdlwHlv5mgusKjB5GWn+fjEHwS8NKw7hKpxBxKIIt+JCMTZailK4j796LbtMAL2E0cA9b1GbRyd5WwQKTJjLRG17NwaRJgbIEPG7hK1U3VwNml9MZrXAZz1WgBbLIvMG1oV05EdlE4us+hv/xZb2If9ttLSNZ4UW0W1vbc8ezq9UNpAtvFMASpyQtjj1bSKYHndZ2cQpDFd8Btf2OG0RiuTRh7L6JwvJEfctnhm0A1jihHPz9I9xl/p1zr7aBF90kmhvycHv+kluaFY26B7rPHbq9EWYP9eVXmCcEnTq/8nIcy5UjIff3HEQFHwmK9Z5YbcNw3ufefJTvT8z1FFzOSty76q3Gk6z0m9QuUM4whNb3pmeVPtOmZPwKs4C44sslXJEKYKPnk9r1p6u6zA+EAnDCkoI6xg+wBiK1s3QLkZ0m9aLk3RyvRk0s2wOzj94BfDxqfOQOCmgYFyMWU+SyOjMY02li8ziQ4mUfjJNwz+467mjKuDKSeJZXN4U27l5D9ulM=----ATTACHMENT:----NTE1NTAwMTM2Mzk2NDc4OCA5MDk5MjkzMjQxMjA1NjQgNDk3OTU0MjI5MjYyNzE4MA==