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\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;
22 * SessionListenerTest.
24 * Tests SessionListener.
26 * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
28 class TestSessionListenerTest extends TestCase
31 * @var TestSessionListener
36 * @var SessionInterface
40 protected function setUp()
42 $this->listener = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\EventListener\TestSessionListener');
43 $this->session = $this->getSession();
46 public function testShouldSaveMasterRequestSession()
48 $this->sessionHasBeenStarted();
49 $this->sessionMustBeSaved();
51 $this->filterResponse(new Request());
54 public function testShouldNotSaveSubRequestSession()
56 $this->sessionMustNotBeSaved();
58 $this->filterResponse(new Request(), HttpKernelInterface::SUB_REQUEST);
61 public function testDoesNotDeleteCookieIfUsingSessionLifetime()
63 $this->sessionHasBeenStarted();
65 $params = session_get_cookie_params();
66 session_set_cookie_params(0, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
68 $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST);
69 $cookies = $response->headers->getCookies();
71 $this->assertEquals(0, reset($cookies)->getExpiresTime());
74 public function testUnstartedSessionIsNotSave()
76 $this->sessionHasNotBeenStarted();
77 $this->sessionMustNotBeSaved();
79 $this->filterResponse(new Request());
82 private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
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);
89 $this->listener->onKernelResponse($event);
91 $this->assertSame($response, $event->getResponse());
96 private function sessionMustNotBeSaved()
98 $this->session->expects($this->never())
102 private function sessionMustBeSaved()
104 $this->session->expects($this->once())
108 private function sessionHasBeenStarted()
110 $this->session->expects($this->once())
111 ->method('isStarted')
112 ->will($this->returnValue(true));
115 private function sessionHasNotBeenStarted()
117 $this->session->expects($this->once())
118 ->method('isStarted')
119 ->will($this->returnValue(false));
122 private function getSession()
124 $mock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
125 ->disableOriginalConstructor()
128 // set return value for getName()
129 $mock->expects($this->any())->method('getName')->will($this->returnValue('MOCKSESSID'));