PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/sensio/framework-extra-bundle/Tests/EventListener/HttpCacheListenerTest.php

https://gitlab.com/pr0055/symfonypizza
PHP | 259 lines | 196 code | 55 blank | 8 comment | 0 complexity | d2543b11b4ccb196ce18dc04363227f7 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sensio\Bundle\FrameworkExtraBundle\Tests\EventListener;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  14. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  16. use Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener;
  17. class HttpCacheListenerTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function setUp()
  20. {
  21. $this->listener = new HttpCacheListener();
  22. $this->response = new Response();
  23. $this->cache = new Cache(array());
  24. $this->request = $this->createRequest($this->cache);
  25. $this->event = $this->createEventMock($this->request, $this->response);
  26. }
  27. public function testWontReassignResponseWhenResponseIsUnsuccessful()
  28. {
  29. $this->event
  30. ->expects($this->never())
  31. ->method('setResponse')
  32. ;
  33. $this->response->setStatusCode(500);
  34. $this->assertInternalType('null', $this->listener->onKernelResponse($this->event));
  35. }
  36. public function testWontReassignResponseWhenNoConfigurationIsPresent()
  37. {
  38. $this->event
  39. ->expects($this->never())
  40. ->method('setResponse')
  41. ;
  42. $this->request->attributes->remove('_cache');
  43. $this->assertInternalType('null', $this->listener->onKernelResponse($this->event));
  44. }
  45. public function testResponseIsPublicIfConfigurationIsPublicTrue()
  46. {
  47. $request = $this->createRequest(new Cache(array(
  48. 'public' => true,
  49. )));
  50. $this->listener->onKernelResponse($this->createEventMock($request, $this->response));
  51. $this->assertTrue($this->response->headers->hasCacheControlDirective('public'));
  52. $this->assertFalse($this->response->headers->hasCacheControlDirective('private'));
  53. }
  54. public function testResponseIsPrivateIfConfigurationIsPublicFalse()
  55. {
  56. $request = $this->createRequest(new Cache(array(
  57. 'public' => false,
  58. )));
  59. $this->listener->onKernelResponse($this->createEventMock($request, $this->response));
  60. $this->assertFalse($this->response->headers->hasCacheControlDirective('public'));
  61. $this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
  62. }
  63. public function testResponseVary()
  64. {
  65. $vary = array('foobar');
  66. $request = $this->createRequest(new Cache(array('vary' => $vary)));
  67. $this->listener->onKernelResponse($this->createEventMock($request, $this->response));
  68. $this->assertTrue($this->response->hasVary());
  69. $result = $this->response->getVary();
  70. $this->assertEquals($vary, $result);
  71. }
  72. public function testResponseVaryWhenVaryNotSet()
  73. {
  74. $request = $this->createRequest(new Cache(array()));
  75. $vary = array('foobar');
  76. $this->response->setVary($vary);
  77. $this->listener->onKernelResponse($this->createEventMock($request, $this->response));
  78. $this->assertTrue($this->response->hasVary());
  79. $result = $this->response->getVary();
  80. $this->assertFalse(empty($result), 'Existing vary headers should not be removed');
  81. $this->assertEquals($vary, $result, 'Vary header should not be changed');
  82. }
  83. public function testResponseIsNeitherPrivateNorPublicIfConfigurationIsPublicNotSet()
  84. {
  85. $request = $this->createRequest(new Cache(array(
  86. )));
  87. $this->listener->onKernelResponse($this->createEventMock($request, $this->response));
  88. $this->assertFalse($this->response->headers->hasCacheControlDirective('public'));
  89. $this->assertFalse($this->response->headers->hasCacheControlDirective('private'));
  90. }
  91. public function testConfigurationAttributesAreSetOnResponse()
  92. {
  93. $this->assertInternalType('null', $this->response->getMaxAge());
  94. $this->assertInternalType('null', $this->response->getExpires());
  95. $this->assertFalse($this->response->headers->hasCacheControlDirective('s-maxage'));
  96. $this->request->attributes->set('_cache', new Cache(array(
  97. 'expires' => 'tomorrow',
  98. 'smaxage' => '15',
  99. 'maxage' => '15',
  100. )));
  101. $this->listener->onKernelResponse($this->event);
  102. $this->assertEquals('15', $this->response->getMaxAge());
  103. $this->assertEquals('15', $this->response->headers->getCacheControlDirective('s-maxage'));
  104. $this->assertInstanceOf('DateTime', $this->response->getExpires());
  105. }
  106. public function testCacheMaxAgeSupportsStrtotimeFormat()
  107. {
  108. $this->request->attributes->set('_cache', new Cache(array(
  109. 'smaxage' => '1 day',
  110. 'maxage' => '1 day',
  111. )));
  112. $this->listener->onKernelResponse($this->event);
  113. $this->assertEquals(60 * 60 * 24, $this->response->headers->getCacheControlDirective('s-maxage'));
  114. $this->assertEquals(60 * 60 * 24, $this->response->getMaxAge());
  115. }
  116. public function testLastModifiedNotModifiedResponse()
  117. {
  118. $request = $this->createRequest(new Cache(array('lastModified' => 'test.getDate()')));
  119. $request->attributes->set('test', new TestEntity());
  120. $request->headers->add(array('If-Modified-Since' => 'Fri, 23 Aug 2013 00:00:00 GMT'));
  121. $listener = new HttpCacheListener();
  122. $controllerEvent = new FilterControllerEvent($this->getKernel(), function () { return new Response(500); }, $request, null);
  123. $listener->onKernelController($controllerEvent);
  124. $response = call_user_func($controllerEvent->getController());
  125. $this->assertEquals(304, $response->getStatusCode());
  126. }
  127. public function testLastModifiedHeader()
  128. {
  129. $request = $this->createRequest(new Cache(array('lastModified' => 'test.getDate()')));
  130. $request->attributes->set('test', new TestEntity());
  131. $response = new Response();
  132. $listener = new HttpCacheListener();
  133. $controllerEvent = new FilterControllerEvent($this->getKernel(), function () { return new Response(); }, $request, null);
  134. $listener->onKernelController($controllerEvent);
  135. $responseEvent = new FilterResponseEvent($this->getKernel(), $request, null, call_user_func($controllerEvent->getController()));
  136. $listener->onKernelResponse($responseEvent);
  137. $response = $responseEvent->getResponse();
  138. $this->assertEquals(200, $response->getStatusCode());
  139. $this->assertTrue($response->headers->has('Last-Modified'));
  140. $this->assertEquals('Fri, 23 Aug 2013 00:00:00 GMT', $response->headers->get('Last-Modified'));
  141. }
  142. public function testETagNotModifiedResponse()
  143. {
  144. $request = $this->createRequest(new Cache(array('etag' => 'test.getId()')));
  145. $request->attributes->set('test', $entity = new TestEntity());
  146. $request->headers->add(array('If-None-Match' => sprintf('"%s"', hash('sha256', $entity->getId()))));
  147. $listener = new HttpCacheListener();
  148. $controllerEvent = new FilterControllerEvent($this->getKernel(), function () { return new Response(500); }, $request, null);
  149. $listener->onKernelController($controllerEvent);
  150. $response = call_user_func($controllerEvent->getController());
  151. $this->assertEquals(304, $response->getStatusCode());
  152. }
  153. public function testETagHeader()
  154. {
  155. $request = $this->createRequest(new Cache(array('ETag' => 'test.getId()')));
  156. $request->attributes->set('test', $entity = new TestEntity());
  157. $response = new Response();
  158. $listener = new HttpCacheListener();
  159. $controllerEvent = new FilterControllerEvent($this->getKernel(), function () { return new Response(); }, $request, null);
  160. $listener->onKernelController($controllerEvent);
  161. $responseEvent = new FilterResponseEvent($this->getKernel(), $request, null, call_user_func($controllerEvent->getController()));
  162. $listener->onKernelResponse($responseEvent);
  163. $response = $responseEvent->getResponse();
  164. $this->assertEquals(200, $response->getStatusCode());
  165. $this->assertTrue($response->headers->has('ETag'));
  166. $this->assertContains(hash('sha256', $entity->getId()), $response->headers->get('ETag'));
  167. }
  168. private function createRequest(Cache $cache = null)
  169. {
  170. return new Request(array(), array(), array(
  171. '_cache' => $cache,
  172. ));
  173. }
  174. private function createEventMock(Request $request, Response $response)
  175. {
  176. $event = $this->getMock('Symfony\Component\HttpKernel\Event\FilterResponseEvent', array(), array(), '', null);
  177. $event
  178. ->expects($this->any())
  179. ->method('getRequest')
  180. ->will($this->returnValue($request))
  181. ;
  182. $event
  183. ->expects($this->any())
  184. ->method('getResponse')
  185. ->will($this->returnValue($response))
  186. ;
  187. return $event;
  188. }
  189. private function getKernel()
  190. {
  191. return $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
  192. }
  193. }
  194. class TestEntity
  195. {
  196. public function getDate()
  197. {
  198. return new \DateTime('Fri, 23 Aug 2013 00:00:00 GMT');
  199. }
  200. public function getId()
  201. {
  202. return '12345';
  203. }
  204. }