1 | <?php |
||
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 | 8 | $mock->options = $options; |
|
68 | 8 | } else { |
|
69 | $refOptions = $refHttpCache->getProperty('options'); |
||
70 | $refOptions->setAccessible(true); |
||
71 | $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() |
|
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() |
|
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() |
|
298 | } |
||
299 | |||
417 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.