cef019167a4efa88ab8339a4dcf6f02b947a1f35
[yaffs-website] / vendor / symfony / http-kernel / Tests / HttpCache / StoreTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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\HttpCache;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpKernel\HttpCache\Store;
18
19 class StoreTest extends TestCase
20 {
21     protected $request;
22     protected $response;
23
24     /**
25      * @var Store
26      */
27     protected $store;
28
29     protected function setUp()
30     {
31         $this->request = Request::create('/');
32         $this->response = new Response('hello world', 200, array());
33
34         HttpCacheTestCase::clearDirectory(sys_get_temp_dir().'/http_cache');
35
36         $this->store = new Store(sys_get_temp_dir().'/http_cache');
37     }
38
39     protected function tearDown()
40     {
41         $this->store = null;
42         $this->request = null;
43         $this->response = null;
44
45         HttpCacheTestCase::clearDirectory(sys_get_temp_dir().'/http_cache');
46     }
47
48     public function testReadsAnEmptyArrayWithReadWhenNothingCachedAtKey()
49     {
50         $this->assertEmpty($this->getStoreMetadata('/nothing'));
51     }
52
53     public function testUnlockFileThatDoesExist()
54     {
55         $cacheKey = $this->storeSimpleEntry();
56         $this->store->lock($this->request);
57
58         $this->assertTrue($this->store->unlock($this->request));
59     }
60
61     public function testUnlockFileThatDoesNotExist()
62     {
63         $this->assertFalse($this->store->unlock($this->request));
64     }
65
66     public function testRemovesEntriesForKeyWithPurge()
67     {
68         $request = Request::create('/foo');
69         $this->store->write($request, new Response('foo'));
70
71         $metadata = $this->getStoreMetadata($request);
72         $this->assertNotEmpty($metadata);
73
74         $this->assertTrue($this->store->purge('/foo'));
75         $this->assertEmpty($this->getStoreMetadata($request));
76
77         // cached content should be kept after purging
78         $path = $this->store->getPath($metadata[0][1]['x-content-digest'][0]);
79         $this->assertTrue(is_file($path));
80
81         $this->assertFalse($this->store->purge('/bar'));
82     }
83
84     public function testStoresACacheEntry()
85     {
86         $cacheKey = $this->storeSimpleEntry();
87
88         $this->assertNotEmpty($this->getStoreMetadata($cacheKey));
89     }
90
91     public function testSetsTheXContentDigestResponseHeaderBeforeStoring()
92     {
93         $cacheKey = $this->storeSimpleEntry();
94         $entries = $this->getStoreMetadata($cacheKey);
95         list($req, $res) = $entries[0];
96
97         $this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
98     }
99
100     public function testFindsAStoredEntryWithLookup()
101     {
102         $this->storeSimpleEntry();
103         $response = $this->store->lookup($this->request);
104
105         $this->assertNotNull($response);
106         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
107     }
108
109     public function testDoesNotFindAnEntryWithLookupWhenNoneExists()
110     {
111         $request = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
112
113         $this->assertNull($this->store->lookup($request));
114     }
115
116     public function testCanonizesUrlsForCacheKeys()
117     {
118         $this->storeSimpleEntry($path = '/test?x=y&p=q');
119         $hitsReq = Request::create($path);
120         $missReq = Request::create('/test?p=x');
121
122         $this->assertNotNull($this->store->lookup($hitsReq));
123         $this->assertNull($this->store->lookup($missReq));
124     }
125
126     public function testDoesNotFindAnEntryWithLookupWhenTheBodyDoesNotExist()
127     {
128         $this->storeSimpleEntry();
129         $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
130         $path = $this->getStorePath($this->response->headers->get('X-Content-Digest'));
131         @unlink($path);
132         $this->assertNull($this->store->lookup($this->request));
133     }
134
135     public function testRestoresResponseHeadersProperlyWithLookup()
136     {
137         $this->storeSimpleEntry();
138         $response = $this->store->lookup($this->request);
139
140         $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()));
141     }
142
143     public function testRestoresResponseContentFromEntityStoreWithLookup()
144     {
145         $this->storeSimpleEntry();
146         $response = $this->store->lookup($this->request);
147         $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test')), $response->getContent());
148     }
149
150     public function testInvalidatesMetaAndEntityStoreEntriesWithInvalidate()
151     {
152         $this->storeSimpleEntry();
153         $this->store->invalidate($this->request);
154         $response = $this->store->lookup($this->request);
155         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
156         $this->assertFalse($response->isFresh());
157     }
158
159     public function testSucceedsQuietlyWhenInvalidateCalledWithNoMatchingEntries()
160     {
161         $req = Request::create('/test');
162         $this->store->invalidate($req);
163         $this->assertNull($this->store->lookup($this->request));
164     }
165
166     public function testDoesNotReturnEntriesThatVaryWithLookup()
167     {
168         $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
169         $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
170         $res = new Response('test', 200, array('Vary' => 'Foo Bar'));
171         $this->store->write($req1, $res);
172
173         $this->assertNull($this->store->lookup($req2));
174     }
175
176     public function testDoesNotReturnEntriesThatSlightlyVaryWithLookup()
177     {
178         $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
179         $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bam'));
180         $res = new Response('test', 200, array('Vary' => array('Foo', 'Bar')));
181         $this->store->write($req1, $res);
182
183         $this->assertNull($this->store->lookup($req2));
184     }
185
186     public function testStoresMultipleResponsesForEachVaryCombination()
187     {
188         $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
189         $res1 = new Response('test 1', 200, array('Vary' => 'Foo Bar'));
190         $key = $this->store->write($req1, $res1);
191
192         $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
193         $res2 = new Response('test 2', 200, array('Vary' => 'Foo Bar'));
194         $this->store->write($req2, $res2);
195
196         $req3 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Baz', 'HTTP_BAR' => 'Boom'));
197         $res3 = new Response('test 3', 200, array('Vary' => 'Foo Bar'));
198         $this->store->write($req3, $res3);
199
200         $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());
201         $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());
202         $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());
203
204         $this->assertCount(3, $this->getStoreMetadata($key));
205     }
206
207     public function testOverwritesNonVaryingResponseWithStore()
208     {
209         $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
210         $res1 = new Response('test 1', 200, array('Vary' => 'Foo Bar'));
211         $key = $this->store->write($req1, $res1);
212         $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());
213
214         $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
215         $res2 = new Response('test 2', 200, array('Vary' => 'Foo Bar'));
216         $this->store->write($req2, $res2);
217         $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());
218
219         $req3 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
220         $res3 = new Response('test 3', 200, array('Vary' => 'Foo Bar'));
221         $key = $this->store->write($req3, $res3);
222         $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());
223
224         $this->assertCount(2, $this->getStoreMetadata($key));
225     }
226
227     public function testLocking()
228     {
229         $req = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
230         $this->assertTrue($this->store->lock($req));
231
232         $path = $this->store->lock($req);
233         $this->assertTrue($this->store->isLocked($req));
234
235         $this->store->unlock($req);
236         $this->assertFalse($this->store->isLocked($req));
237     }
238
239     public function testPurgeHttps()
240     {
241         $request = Request::create('https://example.com/foo');
242         $this->store->write($request, new Response('foo'));
243
244         $this->assertNotEmpty($this->getStoreMetadata($request));
245
246         $this->assertTrue($this->store->purge('https://example.com/foo'));
247         $this->assertEmpty($this->getStoreMetadata($request));
248     }
249
250     public function testPurgeHttpAndHttps()
251     {
252         $requestHttp = Request::create('https://example.com/foo');
253         $this->store->write($requestHttp, new Response('foo'));
254
255         $requestHttps = Request::create('http://example.com/foo');
256         $this->store->write($requestHttps, new Response('foo'));
257
258         $this->assertNotEmpty($this->getStoreMetadata($requestHttp));
259         $this->assertNotEmpty($this->getStoreMetadata($requestHttps));
260
261         $this->assertTrue($this->store->purge('http://example.com/foo'));
262         $this->assertEmpty($this->getStoreMetadata($requestHttp));
263         $this->assertEmpty($this->getStoreMetadata($requestHttps));
264     }
265
266     protected function storeSimpleEntry($path = null, $headers = array())
267     {
268         if (null === $path) {
269             $path = '/test';
270         }
271
272         $this->request = Request::create($path, 'get', array(), array(), array(), $headers);
273         $this->response = new Response('test', 200, array('Cache-Control' => 'max-age=420'));
274
275         return $this->store->write($this->request, $this->response);
276     }
277
278     protected function getStoreMetadata($key)
279     {
280         $r = new \ReflectionObject($this->store);
281         $m = $r->getMethod('getMetadata');
282         $m->setAccessible(true);
283
284         if ($key instanceof Request) {
285             $m1 = $r->getMethod('getCacheKey');
286             $m1->setAccessible(true);
287             $key = $m1->invoke($this->store, $key);
288         }
289
290         return $m->invoke($this->store, $key);
291     }
292
293     protected function getStorePath($key)
294     {
295         $r = new \ReflectionObject($this->store);
296         $m = $r->getMethod('getPath');
297         $m->setAccessible(true);
298
299         return $m->invoke($this->store, $key);
300     }
301 }