|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Request; |
| 16 | +use Symfony\Component\HttpFoundation\Response; |
| 17 | +use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 18 | +use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
| 19 | +use Symfony\Component\HttpKernel\EventListener\SaveSessionListener; |
| 20 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 21 | + |
| 22 | +class SaveSessionListenerTest extends TestCase |
| 23 | +{ |
| 24 | + public function testOnlyTriggeredOnMasterRequest() |
| 25 | + { |
| 26 | + $listener = new SaveSessionListener(); |
| 27 | + $event = $this->getMockBuilder(FilterResponseEvent::class)->disableOriginalConstructor()->getMock(); |
| 28 | + $event->expects($this->once())->method('isMasterRequest')->willReturn(false); |
| 29 | + $event->expects($this->never())->method('getRequest'); |
| 30 | + |
| 31 | + // sub request |
| 32 | + $listener->onKernelResponse($event); |
| 33 | + } |
| 34 | + |
| 35 | + public function testSessionSavedAndResponsePrivate() |
| 36 | + { |
| 37 | + $listener = new SaveSessionListener(); |
| 38 | + $kernel = $this->getMockBuilder(HttpKernelInterface::class)->disableOriginalConstructor()->getMock(); |
| 39 | + |
| 40 | + $session = $this->getMockBuilder(SessionInterface::class)->disableOriginalConstructor()->getMock(); |
| 41 | + $session->expects($this->once())->method('isStarted')->willReturn(true); |
| 42 | + $session->expects($this->once())->method('save'); |
| 43 | + |
| 44 | + $request = new Request(); |
| 45 | + $request->setSession($session); |
| 46 | + $response = new Response(); |
| 47 | + $listener->onKernelResponse(new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response)); |
| 48 | + |
| 49 | + $this->assertTrue($response->headers->hasCacheControlDirective('private')); |
| 50 | + $this->assertTrue($response->headers->hasCacheControlDirective('must-revalidate')); |
| 51 | + $this->assertSame('0', $response->headers->getCacheControlDirective('max-age')); |
| 52 | + } |
| 53 | +} |
0 commit comments