1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\HttpCache\Test; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\SymfonyCache\CacheInvalidationInterface; |
15
|
|
|
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache; |
16
|
|
|
use FOS\HttpCache\SymfonyCache\CacheEvent; |
17
|
|
|
use FOS\HttpCache\SymfonyCache\Events; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This test ensures that the EventDispatchingHttpCache trait is correctly used. |
25
|
|
|
*/ |
26
|
|
|
abstract class EventDispatchingHttpCacheTestCase extends \PHPUnit_Framework_TestCase |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Specify the CacheInvalidationInterface HttpCache class to test. |
30
|
|
|
* |
31
|
|
|
* @return string Fully qualified class name of the AppCache |
32
|
|
|
*/ |
33
|
|
|
abstract protected function getCacheClass(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a partial mock of the HttpCache to only test some methods. |
37
|
|
|
* |
38
|
|
|
* @param array $mockedMethods List of methods to mock. |
39
|
|
|
* |
40
|
|
|
* @return CacheInvalidationInterface|EventDispatchingHttpCache|\PHPUnit_Framework_MockObject_MockObject |
41
|
|
|
*/ |
42
|
8 |
|
protected function getHttpCachePartialMock(array $mockedMethods = null) |
43
|
|
|
{ |
44
|
8 |
|
$mock = $this |
45
|
8 |
|
->getMockBuilder($this->getCacheClass()) |
46
|
8 |
|
->setMethods( $mockedMethods ) |
47
|
8 |
|
->disableOriginalConstructor() |
48
|
8 |
|
->getMock() |
49
|
8 |
|
; |
50
|
|
|
|
51
|
8 |
|
$this->assertInstanceOf('FOS\HttpCache\SymfonyCache\CacheInvalidationInterface', $mock); |
52
|
|
|
|
53
|
|
|
// Force setting options property since we can't use original constructor. |
54
|
|
|
$options = [ |
55
|
8 |
|
'debug' => false, |
56
|
8 |
|
'default_ttl' => 0, |
57
|
8 |
|
'private_headers' => [ 'Authorization', 'Cookie' ], |
58
|
8 |
|
'allow_reload' => false, |
59
|
8 |
|
'allow_revalidate' => false, |
60
|
8 |
|
'stale_while_revalidate' => 2, |
61
|
8 |
|
'stale_if_error' => 60, |
62
|
8 |
|
]; |
63
|
|
|
|
64
|
8 |
|
$refHttpCache = new \ReflectionClass('Symfony\Component\HttpKernel\HttpCache\HttpCache'); |
65
|
|
|
// Workaround for Symfony 2.3 where $options property is not defined. |
66
|
8 |
|
if (!$refHttpCache->hasProperty('options')) { |
67
|
|
|
$mock->options = $options; |
68
|
|
|
} else { |
69
|
8 |
|
$refOptions = $refHttpCache->getProperty('options'); |
70
|
8 |
|
$refOptions->setAccessible(true); |
71
|
8 |
|
$refOptions->setValue($mock, $options ); |
72
|
|
|
} |
73
|
|
|
|
74
|
8 |
|
return $mock; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the store property on a HttpCache to a StoreInterface expecting one write with request and response. |
79
|
|
|
* |
80
|
|
|
* @param CacheInvalidationInterface $httpCache |
81
|
|
|
* @param Request $request |
82
|
|
|
* @param Response $response |
83
|
|
|
*/ |
84
|
2 |
|
protected function setStoreMock(CacheInvalidationInterface $httpCache, Request $request, Response $response) |
85
|
|
|
{ |
86
|
2 |
|
$store = $this->getMock('Symfony\Component\HttpKernel\HttpCache\StoreInterface'); |
87
|
|
|
$store |
88
|
2 |
|
->expects($this->once()) |
89
|
2 |
|
->method('write') |
90
|
2 |
|
->with($request, $response) |
91
|
|
|
; |
92
|
2 |
|
$refHttpCache = new \ReflectionClass('Symfony\Component\HttpKernel\HttpCache\HttpCache'); |
93
|
2 |
|
$refStore = $refHttpCache->getProperty('store'); |
94
|
2 |
|
$refStore->setAccessible(true); |
95
|
2 |
|
$refStore->setValue($httpCache, $store); |
96
|
2 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Assert that preHandle and postHandle are called. |
100
|
|
|
*/ |
101
|
1 |
|
public function testHandleCalled() |
102
|
|
|
{ |
103
|
1 |
|
$catch = true; |
104
|
1 |
|
$request = Request::create('/foo', 'GET'); |
105
|
1 |
|
$response = new Response(); |
106
|
|
|
|
107
|
1 |
|
$httpCache = $this->getHttpCachePartialMock(['lookup']); |
108
|
1 |
|
$subscriber = new TestSubscriber($this, $httpCache, $request); |
|
|
|
|
109
|
1 |
|
$httpCache->addSubscriber($subscriber); |
|
|
|
|
110
|
|
|
$httpCache |
111
|
1 |
|
->expects($this->any()) |
112
|
1 |
|
->method('lookup') |
113
|
1 |
|
->with($request) |
114
|
1 |
|
->will($this->returnValue($response)) |
115
|
|
|
; |
116
|
|
|
|
117
|
1 |
|
$this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); |
118
|
1 |
|
$this->assertEquals(1, $subscriber->preHandleCalls); |
119
|
1 |
|
$this->assertEquals(1, $subscriber->postHandleCalls); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Assert that when preHandle returns a response, that response is used and the normal kernel flow stopped. |
124
|
|
|
* |
125
|
|
|
* @depends testHandleCalled |
126
|
|
|
*/ |
127
|
1 |
|
public function testPreHandleReturnEarly() |
128
|
|
|
{ |
129
|
1 |
|
$catch = true; |
130
|
1 |
|
$request = Request::create('/foo', 'GET'); |
131
|
1 |
|
$response = new Response(); |
132
|
|
|
|
133
|
1 |
|
$httpCache = $this->getHttpCachePartialMock(['lookup']); |
134
|
1 |
|
$subscriber = new TestSubscriber($this, $httpCache, $request); |
|
|
|
|
135
|
1 |
|
$subscriber->preHandleResponse = $response; |
136
|
1 |
|
$httpCache->addSubscriber($subscriber); |
|
|
|
|
137
|
|
|
$httpCache |
138
|
1 |
|
->expects($this->never()) |
139
|
1 |
|
->method('lookup') |
140
|
|
|
; |
141
|
|
|
|
142
|
1 |
|
$this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); |
143
|
1 |
|
$this->assertEquals(1, $subscriber->preHandleCalls); |
144
|
1 |
|
$this->assertEquals(1, $subscriber->postHandleCalls); |
145
|
1 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Assert that postHandle can update the response. |
149
|
|
|
* |
150
|
|
|
* @depends testHandleCalled |
151
|
|
|
*/ |
152
|
1 |
|
public function testPostHandleReturn() |
153
|
|
|
{ |
154
|
1 |
|
$catch = true; |
155
|
1 |
|
$request = Request::create('/foo', 'GET'); |
156
|
1 |
|
$regularResponse = new Response(); |
157
|
1 |
|
$postResponse = new Response(); |
158
|
|
|
|
159
|
1 |
|
$httpCache = $this->getHttpCachePartialMock(['lookup']); |
160
|
1 |
|
$subscriber = new TestSubscriber($this, $httpCache, $request); |
|
|
|
|
161
|
1 |
|
$subscriber->postHandleResponse = $postResponse; |
162
|
1 |
|
$httpCache->addSubscriber($subscriber); |
|
|
|
|
163
|
|
|
$httpCache |
164
|
1 |
|
->expects($this->any()) |
165
|
1 |
|
->method('lookup') |
166
|
1 |
|
->with($request) |
167
|
1 |
|
->will($this->returnValue($regularResponse)) |
168
|
|
|
; |
169
|
|
|
|
170
|
1 |
|
$this->assertSame($postResponse, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); |
171
|
1 |
|
$this->assertEquals(1, $subscriber->preHandleCalls); |
172
|
1 |
|
$this->assertEquals(1, $subscriber->postHandleCalls); |
173
|
1 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Assert that postHandle is called and the response can be updated even when preHandle returned a response. |
177
|
|
|
* |
178
|
|
|
* @depends testHandleCalled |
179
|
|
|
*/ |
180
|
1 |
|
public function testPostHandleAfterPreHandle() |
181
|
|
|
{ |
182
|
1 |
|
$catch = true; |
183
|
1 |
|
$request = Request::create('/foo', 'GET'); |
184
|
1 |
|
$preResponse = new Response(); |
185
|
1 |
|
$postResponse = new Response(); |
186
|
|
|
|
187
|
1 |
|
$httpCache = $this->getHttpCachePartialMock(['lookup']); |
188
|
1 |
|
$subscriber = new TestSubscriber($this, $httpCache, $request); |
|
|
|
|
189
|
1 |
|
$subscriber->preHandleResponse = $preResponse; |
190
|
1 |
|
$subscriber->postHandleResponse = $postResponse; |
191
|
1 |
|
$httpCache->addSubscriber($subscriber); |
|
|
|
|
192
|
|
|
$httpCache |
193
|
1 |
|
->expects($this->never()) |
194
|
1 |
|
->method('lookup') |
195
|
|
|
; |
196
|
|
|
|
197
|
1 |
|
$this->assertSame($postResponse, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); |
198
|
1 |
|
$this->assertEquals(1, $subscriber->preHandleCalls); |
199
|
1 |
|
$this->assertEquals(1, $subscriber->postHandleCalls); |
200
|
1 |
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Assert that preStore is called. |
204
|
|
|
*/ |
205
|
1 |
|
public function testPreStoreCalled() |
206
|
|
|
{ |
207
|
1 |
|
$request = Request::create('/foo', 'GET'); |
208
|
1 |
|
$response = new Response(); |
209
|
|
|
|
210
|
1 |
|
$httpCache = $this->getHttpCachePartialMock(); |
211
|
1 |
|
$subscriber = new TestSubscriber($this, $httpCache, $request); |
|
|
|
|
212
|
1 |
|
$httpCache->addSubscriber($subscriber); |
|
|
|
|
213
|
|
|
|
214
|
1 |
|
$this->setStoreMock($httpCache, $request, $response); |
215
|
|
|
|
216
|
1 |
|
$refHttpCache = new \ReflectionObject($httpCache); |
217
|
1 |
|
$method = $refHttpCache->getMethod('store'); |
218
|
1 |
|
$method->setAccessible(true); |
219
|
1 |
|
$method->invokeArgs($httpCache, [$request, $response]); |
220
|
1 |
|
$this->assertEquals(1, $subscriber->preStoreCalls); |
221
|
1 |
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Assert that preStore response is used when provided. |
225
|
|
|
*/ |
226
|
1 |
|
public function testPreStoreResponse() |
227
|
|
|
{ |
228
|
1 |
|
$request = Request::create('/foo', 'GET'); |
229
|
1 |
|
$regularResponse = new Response(); |
230
|
1 |
|
$preStoreResponse = new Response(); |
231
|
|
|
|
232
|
1 |
|
$httpCache = $this->getHttpCachePartialMock(); |
233
|
1 |
|
$subscriber = new TestSubscriber($this, $httpCache, $request); |
|
|
|
|
234
|
1 |
|
$subscriber->preStoreResponse = $preStoreResponse; |
235
|
1 |
|
$httpCache->addSubscriber($subscriber); |
|
|
|
|
236
|
|
|
|
237
|
1 |
|
$this->setStoreMock($httpCache, $request, $preStoreResponse); |
238
|
|
|
|
239
|
1 |
|
$refHttpCache = new \ReflectionObject($httpCache); |
240
|
1 |
|
$method = $refHttpCache->getMethod('store'); |
241
|
1 |
|
$method->setAccessible(true); |
242
|
1 |
|
$method->invokeArgs($httpCache, [$request, $regularResponse]); |
243
|
1 |
|
$this->assertEquals(1, $subscriber->preStoreCalls); |
244
|
1 |
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Assert that preInvalidate is called. |
248
|
|
|
*/ |
249
|
1 |
|
public function testPreInvalidateCalled() |
250
|
|
|
{ |
251
|
1 |
|
$catch = true; |
252
|
1 |
|
$request = Request::create('/foo', 'GET'); |
253
|
1 |
|
$response = new Response('', 500); |
254
|
|
|
|
255
|
1 |
|
$httpCache = $this->getHttpCachePartialMock(['pass']); |
256
|
1 |
|
$subscriber = new TestSubscriber($this, $httpCache, $request); |
|
|
|
|
257
|
1 |
|
$httpCache->addSubscriber($subscriber); |
|
|
|
|
258
|
|
|
$httpCache |
259
|
1 |
|
->expects($this->any()) |
260
|
1 |
|
->method('pass') |
261
|
1 |
|
->with($request) |
262
|
1 |
|
->will($this->returnValue($response)) |
263
|
|
|
; |
264
|
1 |
|
$refHttpCache = new \ReflectionObject($httpCache); |
265
|
1 |
|
$method = $refHttpCache->getMethod('invalidate'); |
266
|
1 |
|
$method->setAccessible(true); |
267
|
|
|
|
268
|
1 |
|
$this->assertSame($response, $method->invokeArgs($httpCache, [$request, $catch])); |
269
|
1 |
|
$this->assertEquals(1, $subscriber->preInvalidateCalls); |
270
|
1 |
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Assert that when preInvalidate returns a response, that response is used and the normal kernel flow stopped. |
274
|
|
|
* |
275
|
|
|
* @depends testPreInvalidateCalled |
276
|
|
|
*/ |
277
|
1 |
|
public function testPreInvalidateReturnEarly() |
278
|
|
|
{ |
279
|
1 |
|
$catch = true; |
280
|
1 |
|
$request = Request::create('/foo', 'GET'); |
281
|
1 |
|
$response = new Response('', 400); |
282
|
|
|
|
283
|
1 |
|
$httpCache = $this->getHttpCachePartialMock(['pass']); |
284
|
1 |
|
$subscriber = new TestSubscriber($this, $httpCache, $request); |
|
|
|
|
285
|
1 |
|
$subscriber->preInvalidateResponse = $response; |
286
|
1 |
|
$httpCache->addSubscriber($subscriber); |
|
|
|
|
287
|
|
|
$httpCache |
288
|
1 |
|
->expects($this->never()) |
289
|
1 |
|
->method('pass') |
290
|
|
|
; |
291
|
1 |
|
$refHttpCache = new \ReflectionObject($httpCache); |
292
|
1 |
|
$method = $refHttpCache->getMethod('invalidate'); |
293
|
1 |
|
$method->setAccessible(true); |
294
|
|
|
|
295
|
1 |
|
$this->assertSame($response, $method->invokeArgs($httpCache, [$request, $catch])); |
296
|
1 |
|
$this->assertEquals(1, $subscriber->preInvalidateCalls); |
297
|
1 |
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
class TestSubscriber implements EventSubscriberInterface |
|
|
|
|
301
|
|
|
{ |
302
|
|
|
/** |
303
|
|
|
* @var int Count how many times preHandle has been called. |
304
|
|
|
*/ |
305
|
|
|
public $preHandleCalls = 0; |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @var int Count how many times postHandle has been called. |
309
|
|
|
*/ |
310
|
|
|
public $postHandleCalls = 0; |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @var int Count how many times preStore has been called. |
314
|
|
|
*/ |
315
|
|
|
public $preStoreCalls = 0; |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @var int Count how many times preInvalidate has been called. |
319
|
|
|
*/ |
320
|
|
|
public $preInvalidateCalls = 0; |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @var Response A response to set during the preHandle |
324
|
|
|
*/ |
325
|
|
|
public $preHandleResponse; |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @var Response A response to set during the postHandle |
329
|
|
|
*/ |
330
|
|
|
public $postHandleResponse; |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @var Response A response to set during the preStore |
334
|
|
|
*/ |
335
|
|
|
public $preStoreResponse; |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @var Response A response to set during the preInvalidate |
339
|
|
|
*/ |
340
|
|
|
public $preInvalidateResponse; |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @var EventDispatchingHttpCacheTestCase To do assertions |
344
|
|
|
*/ |
345
|
|
|
private $test; |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @var CacheInvalidationInterface The kernel to ensure the event carries the correct kernel |
349
|
|
|
*/ |
350
|
|
|
private $kernel; |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @var Request The request to ensure the event carries the correct request |
354
|
|
|
*/ |
355
|
|
|
private $request; |
356
|
|
|
|
357
|
8 |
|
public function __construct( |
358
|
|
|
EventDispatchingHttpCacheTestCase $test, |
359
|
|
|
CacheInvalidationInterface $kernel, |
360
|
|
|
Request $request |
|
|
|
|
361
|
|
|
) { |
362
|
8 |
|
$this->test = $test; |
363
|
8 |
|
$this->kernel = $kernel; |
364
|
8 |
|
$this->request = $request; |
365
|
8 |
|
} |
366
|
|
|
|
367
|
8 |
|
public static function getSubscribedEvents() |
368
|
|
|
{ |
369
|
|
|
return [ |
370
|
8 |
|
Events::PRE_HANDLE => 'preHandle', |
371
|
8 |
|
Events::POST_HANDLE => 'postHandle', |
372
|
8 |
|
Events::PRE_STORE => 'preStore', |
373
|
8 |
|
Events::PRE_INVALIDATE => 'preInvalidate', |
374
|
8 |
|
]; |
375
|
|
|
} |
376
|
|
|
|
377
|
4 |
View Code Duplication |
public function preHandle(CacheEvent $event) |
|
|
|
|
378
|
|
|
{ |
379
|
4 |
|
$this->test->assertSame($this->kernel, $event->getKernel()); |
380
|
4 |
|
$this->test->assertSame($this->request, $event->getRequest()); |
381
|
4 |
|
if ($this->preHandleResponse) { |
382
|
2 |
|
$event->setResponse($this->preHandleResponse); |
383
|
2 |
|
} |
384
|
4 |
|
$this->preHandleCalls++; |
385
|
4 |
|
} |
386
|
|
|
|
387
|
4 |
View Code Duplication |
public function postHandle(CacheEvent $event) |
|
|
|
|
388
|
|
|
{ |
389
|
4 |
|
$this->test->assertSame($this->kernel, $event->getKernel()); |
390
|
4 |
|
$this->test->assertSame($this->request, $event->getRequest()); |
391
|
4 |
|
if ($this->postHandleResponse) { |
392
|
2 |
|
$event->setResponse($this->postHandleResponse); |
393
|
2 |
|
} |
394
|
4 |
|
$this->postHandleCalls++; |
395
|
4 |
|
} |
396
|
|
|
|
397
|
2 |
View Code Duplication |
public function preStore(CacheEvent $event) |
|
|
|
|
398
|
|
|
{ |
399
|
2 |
|
$this->test->assertSame($this->kernel, $event->getKernel()); |
400
|
2 |
|
$this->test->assertSame($this->request, $event->getRequest()); |
401
|
2 |
|
if ($this->preStoreResponse) { |
402
|
1 |
|
$event->setResponse($this->preStoreResponse); |
403
|
1 |
|
} |
404
|
2 |
|
$this->preStoreCalls++; |
405
|
2 |
|
} |
406
|
|
|
|
407
|
2 |
View Code Duplication |
public function preInvalidate(CacheEvent $event) |
|
|
|
|
408
|
|
|
{ |
409
|
2 |
|
$this->test->assertSame($this->kernel, $event->getKernel()); |
410
|
2 |
|
$this->test->assertSame($this->request, $event->getRequest()); |
411
|
2 |
|
if ($this->preInvalidateResponse) { |
412
|
1 |
|
$event->setResponse($this->preInvalidateResponse); |
413
|
1 |
|
} |
414
|
2 |
|
$this->preInvalidateCalls++; |
415
|
2 |
|
} |
416
|
|
|
} |
417
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.