StoreTest.php 8.6 KB

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