22a2b71239874a3cc8d91b8cee088067951599b1
[yaffs-website] / vendor / symfony / http-kernel / Tests / EventListener / TestSessionListenerTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\EventListener;
13
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;
24
25 /**
26  * SessionListenerTest.
27  *
28  * Tests SessionListener.
29  *
30  * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
31  */
32 class TestSessionListenerTest extends TestCase
33 {
34     /**
35      * @var TestSessionListener
36      */
37     private $listener;
38
39     /**
40      * @var SessionInterface
41      */
42     private $session;
43
44     protected function setUp()
45     {
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));
51     }
52
53     public function testShouldSaveMasterRequestSession()
54     {
55         $this->sessionHasBeenStarted();
56         $this->sessionMustBeSaved();
57
58         $this->filterResponse(new Request());
59     }
60
61     public function testShouldNotSaveSubRequestSession()
62     {
63         $this->sessionMustNotBeSaved();
64
65         $this->filterResponse(new Request(), HttpKernelInterface::SUB_REQUEST);
66     }
67
68     public function testDoesNotDeleteCookieIfUsingSessionLifetime()
69     {
70         $this->sessionHasBeenStarted();
71
72         @ini_set('session.cookie_lifetime', 0);
73
74         $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST);
75         $cookies = $response->headers->getCookies();
76
77         $this->assertEquals(0, reset($cookies)->getExpiresTime());
78     }
79
80     /**
81      * @requires function \Symfony\Component\HttpFoundation\Session\Session::isEmpty
82      */
83     public function testEmptySessionDoesNotSendCookie()
84     {
85         $this->sessionHasBeenStarted();
86         $this->sessionIsEmpty();
87
88         $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST);
89
90         $this->assertSame(array(), $response->headers->getCookies());
91     }
92
93     public function testEmptySessionWithNewSessionIdDoesSendCookie()
94     {
95         $this->sessionHasBeenStarted();
96         $this->sessionIsEmpty();
97         $this->fixSessionId('456');
98
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);
103
104         $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST);
105
106         $this->assertNotEmpty($response->headers->getCookies());
107     }
108
109     public function testUnstartedSessionIsNotSave()
110     {
111         $this->sessionHasNotBeenStarted();
112         $this->sessionMustNotBeSaved();
113
114         $this->filterResponse(new Request());
115     }
116
117     public function testDoesNotImplementServiceSubscriberInterface()
118     {
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'));
124     }
125
126     private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
127     {
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);
132
133         $this->listener->onKernelResponse($event);
134
135         $this->assertSame($response, $event->getResponse());
136
137         return $response;
138     }
139
140     private function sessionMustNotBeSaved()
141     {
142         $this->session->expects($this->never())
143             ->method('save');
144     }
145
146     private function sessionMustBeSaved()
147     {
148         $this->session->expects($this->once())
149             ->method('save');
150     }
151
152     private function sessionHasBeenStarted()
153     {
154         $this->session->expects($this->once())
155             ->method('isStarted')
156             ->will($this->returnValue(true));
157     }
158
159     private function sessionHasNotBeenStarted()
160     {
161         $this->session->expects($this->once())
162             ->method('isStarted')
163             ->will($this->returnValue(false));
164     }
165
166     private function sessionIsEmpty()
167     {
168         $this->session->expects($this->once())
169             ->method('isEmpty')
170             ->will($this->returnValue(true));
171     }
172
173     private function fixSessionId($sessionId)
174     {
175         $this->session->expects($this->any())
176             ->method('getId')
177             ->will($this->returnValue($sessionId));
178     }
179
180     private function getSession()
181     {
182         $mock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
183             ->disableOriginalConstructor()
184             ->getMock();
185
186         // set return value for getName()
187         $mock->expects($this->any())->method('getName')->will($this->returnValue('MOCKSESSID'));
188
189         return $mock;
190     }
191 }