Yaffs site version 1.1
[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\HttpFoundation\Response;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpKernel\HttpKernelInterface;
18 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
19 use Symfony\Component\HttpFoundation\Session\SessionInterface;
20
21 /**
22  * SessionListenerTest.
23  *
24  * Tests SessionListener.
25  *
26  * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
27  */
28 class TestSessionListenerTest extends TestCase
29 {
30     /**
31      * @var TestSessionListener
32      */
33     private $listener;
34
35     /**
36      * @var SessionInterface
37      */
38     private $session;
39
40     protected function setUp()
41     {
42         $this->listener = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\EventListener\TestSessionListener');
43         $this->session = $this->getSession();
44     }
45
46     public function testShouldSaveMasterRequestSession()
47     {
48         $this->sessionHasBeenStarted();
49         $this->sessionMustBeSaved();
50
51         $this->filterResponse(new Request());
52     }
53
54     public function testShouldNotSaveSubRequestSession()
55     {
56         $this->sessionMustNotBeSaved();
57
58         $this->filterResponse(new Request(), HttpKernelInterface::SUB_REQUEST);
59     }
60
61     public function testDoesNotDeleteCookieIfUsingSessionLifetime()
62     {
63         $this->sessionHasBeenStarted();
64
65         $params = session_get_cookie_params();
66         session_set_cookie_params(0, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
67
68         $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST);
69         $cookies = $response->headers->getCookies();
70
71         $this->assertEquals(0, reset($cookies)->getExpiresTime());
72     }
73
74     public function testUnstartedSessionIsNotSave()
75     {
76         $this->sessionHasNotBeenStarted();
77         $this->sessionMustNotBeSaved();
78
79         $this->filterResponse(new Request());
80     }
81
82     private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
83     {
84         $request->setSession($this->session);
85         $response = new Response();
86         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
87         $event = new FilterResponseEvent($kernel, $request, $type, $response);
88
89         $this->listener->onKernelResponse($event);
90
91         $this->assertSame($response, $event->getResponse());
92
93         return $response;
94     }
95
96     private function sessionMustNotBeSaved()
97     {
98         $this->session->expects($this->never())
99             ->method('save');
100     }
101
102     private function sessionMustBeSaved()
103     {
104         $this->session->expects($this->once())
105             ->method('save');
106     }
107
108     private function sessionHasBeenStarted()
109     {
110         $this->session->expects($this->once())
111             ->method('isStarted')
112             ->will($this->returnValue(true));
113     }
114
115     private function sessionHasNotBeenStarted()
116     {
117         $this->session->expects($this->once())
118             ->method('isStarted')
119             ->will($this->returnValue(false));
120     }
121
122     private function getSession()
123     {
124         $mock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
125             ->disableOriginalConstructor()
126             ->getMock();
127
128         // set return value for getName()
129         $mock->expects($this->any())->method('getName')->will($this->returnValue('MOCKSESSID'));
130
131         return $mock;
132     }
133 }