StoreTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 Symfony\Component\HttpKernel\Tests\HttpCache;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\HttpCache\Store;
  14. class StoreTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected $request;
  17. protected $response;
  18. /**
  19. * @var Store
  20. */
  21. protected $store;
  22. protected function setUp()
  23. {
  24. $this->request = Request::create('/');
  25. $this->response = new Response('hello world', 200, array());
  26. HttpCacheTestCase::clearDirectory(sys_get_temp_dir().'/http_cache');
  27. $this->store = new Store(sys_get_temp_dir().'/http_cache');
  28. }
  29. protected function tearDown()
  30. {
  31. $this->store = null;
  32. $this->request = null;
  33. $this->response = null;
  34. HttpCacheTestCase::clearDirectory(sys_get_temp_dir().'/http_cache');
  35. }
  36. public function testReadsAnEmptyArrayWithReadWhenNothingCachedAtKey()
  37. {
  38. $this->assertEmpty($this->getStoreMetadata('/nothing'));
  39. }
  40. public function testUnlockFileThatDoesExist()
  41. {
  42. $cacheKey = $this->storeSimpleEntry();
  43. $this->store->lock($this->request);
  44. $this->assertTrue($this->store->unlock($this->request));
  45. }
  46. public function testUnlockFileThatDoesNotExist()
  47. {
  48. $this->assertFalse($this->store->unlock($this->request));
  49. }
  50. public function testRemovesEntriesForKeyWithPurge()
  51. {
  52. $request = Request::create('/foo');
  53. $this->store->write($request, new Response('foo'));
  54. $metadata = $this->getStoreMetadata($request);
  55. $this->assertNotEmpty($metadata);
  56. $this->assertTrue($this->store->purge('/foo'));
  57. $this->assertEmpty($this->getStoreMetadata($request));
  58. // cached content should be kept after purging
  59. $path = $this->store->getPath($metadata[0][1]['x-content-digest'][0]);
  60. $this->assertTrue(is_file($path));
  61. $this->assertFalse($this->store->purge('/bar'));
  62. }
  63. public function testStoresACacheEntry()
  64. {
  65. $cacheKey = $this->storeSimpleEntry();
  66. $this->assertNotEmpty($this->getStoreMetadata($cacheKey));
  67. }
  68. public function testSetsTheXContentDigestResponseHeaderBeforeStoring()
  69. {
  70. $cacheKey = $this->storeSimpleEntry();
  71. $entries = $this->getStoreMetadata($cacheKey);
  72. list($req, $res) = $entries[0];
  73. $this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
  74. }
  75. public function testFindsAStoredEntryWithLookup()
  76. {
  77. $this->storeSimpleEntry();
  78. $response = $this->store->lookup($this->request);
  79. $this->assertNotNull($response);
  80. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  81. }
  82. public function testDoesNotFindAnEntryWithLookupWhenNoneExists()
  83. {
  84. $request = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  85. $this->assertNull($this->store->lookup($request));
  86. }
  87. public function testCanonizesUrlsForCacheKeys()
  88. {
  89. $this->storeSimpleEntry($path = '/test?x=y&p=q');
  90. $hitsReq = Request::create($path);
  91. $missReq = Request::create('/test?p=x');
  92. $this->assertNotNull($this->store->lookup($hitsReq));
  93. $this->assertNull($this->store->lookup($missReq));
  94. }
  95. public function testDoesNotFindAnEntryWithLookupWhenTheBodyDoesNotExist()
  96. {
  97. $this->storeSimpleEntry();
  98. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  99. $path = $this->getStorePath($this->response->headers->get('X-Content-Digest'));
  100. @unlink($path);
  101. $this->assertNull($this->store->lookup($this->request));
  102. }
  103. public function testRestoresResponseHeadersProperlyWithLookup()
  104. {
  105. $this->storeSimpleEntry();
  106. $response = $this->store->lookup($this->request);
  107. $this->assertEquals($response->headers->all(), array_merge(array('content-length' => 4, 'x-body-file' => array($this->getStorePath($response->headers->get('X-Content-Digest')))), $this->response->headers->all()));
  108. }
  109. public function testRestoresResponseContentFromEntityStoreWithLookup()
  110. {
  111. $this->storeSimpleEntry();
  112. $response = $this->store->lookup($this->request);
  113. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test')), $response->getContent());
  114. }
  115. public function testInvalidatesMetaAndEntityStoreEntriesWithInvalidate()
  116. {
  117. $this->storeSimpleEntry();
  118. $this->store->invalidate($this->request);
  119. $response = $this->store->lookup($this->request);
  120. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  121. $this->assertFalse($response->isFresh());
  122. }
  123. public function testSucceedsQuietlyWhenInvalidateCalledWithNoMatchingEntries()
  124. {
  125. $req = Request::create('/test');
  126. $this->store->invalidate($req);
  127. $this->assertNull($this->store->lookup($this->request));
  128. }
  129. public function testDoesNotReturnEntriesThatVaryWithLookup()
  130. {
  131. $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  132. $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
  133. $res = new Response('test', 200, array('Vary' => 'Foo Bar'));
  134. $this->store->write($req1, $res);
  135. $this->assertNull($this->store->lookup($req2));
  136. }
  137. public function testDoesNotReturnEntriesThatSlightlyVaryWithLookup()
  138. {
  139. $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  140. $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bam'));
  141. $res = new Response('test', 200, array('Vary' => array('Foo', 'Bar')));
  142. $this->store->write($req1, $res);
  143. $this->assertNull($this->store->lookup($req2));
  144. }
  145. public function testStoresMultipleResponsesForEachVaryCombination()
  146. {
  147. $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  148. $res1 = new Response('test 1', 200, array('Vary' => 'Foo Bar'));
  149. $key = $this->store->write($req1, $res1);
  150. $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
  151. $res2 = new Response('test 2', 200, array('Vary' => 'Foo Bar'));
  152. $this->store->write($req2, $res2);
  153. $req3 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Baz', 'HTTP_BAR' => 'Boom'));
  154. $res3 = new Response('test 3', 200, array('Vary' => 'Foo Bar'));
  155. $this->store->write($req3, $res3);
  156. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());
  157. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());
  158. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());
  159. $this->assertCount(3, $this->getStoreMetadata($key));
  160. }
  161. public function testOverwritesNonVaryingResponseWithStore()
  162. {
  163. $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  164. $res1 = new Response('test 1', 200, array('Vary' => 'Foo Bar'));
  165. $key = $this->store->write($req1, $res1);
  166. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());
  167. $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
  168. $res2 = new Response('test 2', 200, array('Vary' => 'Foo Bar'));
  169. $this->store->write($req2, $res2);
  170. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());
  171. $req3 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  172. $res3 = new Response('test 3', 200, array('Vary' => 'Foo Bar'));
  173. $key = $this->store->write($req3, $res3);
  174. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());
  175. $this->assertCount(2, $this->getStoreMetadata($key));
  176. }
  177. public function testLocking()
  178. {
  179. $req = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  180. $this->assertTrue($this->store->lock($req));
  181. $path = $this->store->lock($req);
  182. $this->assertTrue($this->store->isLocked($req));
  183. $this->store->unlock($req);
  184. $this->assertFalse($this->store->isLocked($req));
  185. }
  186. protected function storeSimpleEntry($path = null, $headers = array())
  187. {
  188. if (null === $path) {
  189. $path = '/test';
  190. }
  191. $this->request = Request::create($path, 'get', array(), array(), array(), $headers);
  192. $this->response = new Response('test', 200, array('Cache-Control' => 'max-age=420'));
  193. return $this->store->write($this->request, $this->response);
  194. }
  195. protected function getStoreMetadata($key)
  196. {
  197. $r = new \ReflectionObject($this->store);
  198. $m = $r->getMethod('getMetadata');
  199. $m->setAccessible(true);
  200. if ($key instanceof Request) {
  201. $m1 = $r->getMethod('getCacheKey');
  202. $m1->setAccessible(true);
  203. $key = $m1->invoke($this->store, $key);
  204. }
  205. return $m->invoke($this->store, $key);
  206. }
  207. protected function getStorePath($key)
  208. {
  209. $r = new \ReflectionObject($this->store);
  210. $m = $r->getMethod('getPath');
  211. $m->setAccessible(true);
  212. return $m->invoke($this->store, $key);
  213. }
  214. }