StoreTest.php 8.5 KB

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