4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\EventListener;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpFoundation\Request;
18 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19 use Symfony\Component\HttpKernel\HttpKernelInterface;
20 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
21 use Symfony\Component\HttpKernel\EventListener\SessionListener;
22 use Symfony\Component\HttpKernel\EventListener\TestSessionListener;
23 use Symfony\Component\HttpFoundation\Session\SessionInterface;
26 * SessionListenerTest.
28 * Tests SessionListener.
30 * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
32 class TestSessionListenerTest extends TestCase
35 * @var TestSessionListener
40 * @var SessionInterface
44 protected function setUp()
46 $this->listener = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\EventListener\AbstractTestSessionListener');
47 $this->session = $this->getSession();
48 $this->listener->expects($this->any())
49 ->method('getSession')
50 ->will($this->returnValue($this->session));
53 public function testShouldSaveMasterRequestSession()
55 $this->sessionHasBeenStarted();
56 $this->sessionMustBeSaved();
58 $this->filterResponse(new Request());
61 public function testShouldNotSaveSubRequestSession()
63 $this->sessionMustNotBeSaved();
65 $this->filterResponse(new Request(), HttpKernelInterface::SUB_REQUEST);
68 public function testDoesNotDeleteCookieIfUsingSessionLifetime()
70 $this->sessionHasBeenStarted();
72 @ini_set('session.cookie_lifetime', 0);
74 $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST);
75 $cookies = $response->headers->getCookies();
77 $this->assertEquals(0, reset($cookies)->getExpiresTime());
81 * @requires function \Symfony\Component\HttpFoundation\Session\Session::isEmpty
83 public function testEmptySessionDoesNotSendCookie()
85 $this->sessionHasBeenStarted();
86 $this->sessionIsEmpty();
88 $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST);
90 $this->assertSame(array(), $response->headers->getCookies());
93 public function testEmptySessionWithNewSessionIdDoesSendCookie()
95 $this->sessionHasBeenStarted();
96 $this->sessionIsEmpty();
97 $this->fixSessionId('456');
99 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
100 $request = Request::create('/', 'GET', array(), array('MOCKSESSID' => '123'));
101 $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
102 $this->listener->onKernelRequest($event);
104 $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST);
106 $this->assertNotEmpty($response->headers->getCookies());
109 public function testUnstartedSessionIsNotSave()
111 $this->sessionHasNotBeenStarted();
112 $this->sessionMustNotBeSaved();
114 $this->filterResponse(new Request());
117 public function testDoesNotImplementServiceSubscriberInterface()
119 $this->assertTrue(interface_exists(ServiceSubscriberInterface::class));
120 $this->assertTrue(class_exists(SessionListener::class));
121 $this->assertTrue(class_exists(TestSessionListener::class));
122 $this->assertFalse(is_subclass_of(SessionListener::class, ServiceSubscriberInterface::class), 'Implementing ServiceSubscriberInterface would create a dep on the DI component, which eg Silex cannot afford');
123 $this->assertFalse(is_subclass_of(TestSessionListener::class, ServiceSubscriberInterface::class, 'Implementing ServiceSubscriberInterface would create a dep on the DI component, which eg Silex cannot afford'));
126 private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
128 $request->setSession($this->session);
129 $response = new Response();
130 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
131 $event = new FilterResponseEvent($kernel, $request, $type, $response);
133 $this->listener->onKernelResponse($event);
135 $this->assertSame($response, $event->getResponse());
140 private function sessionMustNotBeSaved()
142 $this->session->expects($this->never())
146 private function sessionMustBeSaved()
148 $this->session->expects($this->once())
152 private function sessionHasBeenStarted()
154 $this->session->expects($this->once())
155 ->method('isStarted')
156 ->will($this->returnValue(true));
159 private function sessionHasNotBeenStarted()
161 $this->session->expects($this->once())
162 ->method('isStarted')
163 ->will($this->returnValue(false));
166 private function sessionIsEmpty()
168 $this->session->expects($this->once())
170 ->will($this->returnValue(true));
173 private function fixSessionId($sessionId)
175 $this->session->expects($this->any())
177 ->will($this->returnValue($sessionId));
180 private function getSession()
182 $mock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
183 ->disableOriginalConstructor()
186 // set return value for getName()
187 $mock->expects($this->any())->method('getName')->will($this->returnValue('MOCKSESSID'));