1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hola\AmpToolboxBundle\Tests\Unit\EventSubscriber; |
4
|
|
|
|
5
|
|
|
use AmpProject\Optimizer\Error\UnknownError; |
6
|
|
|
use AmpProject\Optimizer\ErrorCollection; |
7
|
|
|
use AmpProject\Optimizer\TransformationEngine; |
8
|
|
|
use DG\BypassFinals; |
9
|
|
|
use Hola\AmpToolboxBundle\EventSubscriber\AmpOptimizerSubscriber; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Prophecy\Argument; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use ReflectionException; |
14
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
18
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
19
|
|
|
|
20
|
|
|
class AmpOptimizerSubscriberTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
BypassFinals::enable(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Test simple getSubscribedEvents function |
29
|
|
|
*/ |
30
|
|
|
public function testSubscribedEvents() |
31
|
|
|
{ |
32
|
|
|
$events = AmpOptimizerSubscriber::getSubscribedEvents(); |
33
|
|
|
$eventsExpected = [KernelEvents::RESPONSE => ['onKernelResponse', -10]]; |
34
|
|
|
$this->assertEquals($events, $eventsExpected); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Test not master request |
39
|
|
|
*/ |
40
|
|
|
public function testNotMasterRequest() |
41
|
|
|
{ |
42
|
|
|
$instance = $this->getInstanceNotMasterRequest(); |
43
|
|
|
$event = $this->getEventNotMasterRequestMocked(); |
44
|
|
|
$instance->onKernelResponse($event); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Provide instance to test with not master request and test calls |
49
|
|
|
* @return AmpOptimizerSubscriber |
50
|
|
|
*/ |
51
|
|
|
private function getInstanceNotMasterRequest(): AmpOptimizerSubscriber |
52
|
|
|
{ |
53
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
54
|
|
|
|
55
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
56
|
|
|
$transformationEngine->optimizeHtml(Argument::type('string'), Argument::type(ErrorCollection::class)) |
57
|
|
|
->shouldNotBeCalled(); |
58
|
|
|
|
59
|
|
|
return new AmpOptimizerSubscriber( |
60
|
|
|
$logger->reveal(), |
61
|
|
|
$transformationEngine->reveal(), |
62
|
|
|
['transform_enabled' => true] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Provide response event to test with not master request and test calls |
68
|
|
|
* @return ResponseEvent |
69
|
|
|
*/ |
70
|
|
|
private function getEventNotMasterRequestMocked(): ResponseEvent |
71
|
|
|
{ |
72
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
73
|
|
|
$event->isMasterRequest()->shouldBeCalled()->willReturn(false); |
74
|
|
|
$event->getResponse()->shouldNotBeCalled(); |
75
|
|
|
return $event->reveal(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test request without html amp format or content type |
80
|
|
|
*/ |
81
|
|
|
public function testNotAmpRequest() |
82
|
|
|
{ |
83
|
|
|
$instance = $this->getInstanceNotAmpRequest(); |
84
|
|
|
|
85
|
|
|
$event = $this->getEventNotAmpRequestMocked('text/html', '', '/segment/image.webp'); |
86
|
|
|
$instance->onKernelResponse($event); |
87
|
|
|
|
88
|
|
|
$event = $this->getEventNotAmpRequestMocked('image/jpeg'); |
89
|
|
|
$instance->onKernelResponse($event); |
90
|
|
|
|
91
|
|
|
$event = $this->getEventNotAmpRequestMocked('text/html', '<html></html>'); |
92
|
|
|
$instance->onKernelResponse($event); |
93
|
|
|
|
94
|
|
|
$event = $this->getEventNotAmpRequestMocked('text/html', '<body></body>'); |
95
|
|
|
$instance->onKernelResponse($event); |
96
|
|
|
|
97
|
|
|
$event = $this->getEventNotAmpRequestMocked('text/html', ''); |
98
|
|
|
$instance->onKernelResponse($event); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Provide instance to test with not html amp request and test calls |
103
|
|
|
* @return AmpOptimizerSubscriber |
104
|
|
|
*/ |
105
|
|
|
private function getInstanceNotAmpRequest(): AmpOptimizerSubscriber |
106
|
|
|
{ |
107
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
108
|
|
|
|
109
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
110
|
|
|
$transformationEngine->optimizeHtml(Argument::type('string'), Argument::type(ErrorCollection::class)) |
111
|
|
|
->shouldNotBeCalled(); |
112
|
|
|
|
113
|
|
|
return new AmpOptimizerSubscriber( |
114
|
|
|
$logger->reveal(), |
115
|
|
|
$transformationEngine->reveal(), |
116
|
|
|
['transform_enabled' => true] |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Provide response event to test with not html amp request and test calls |
122
|
|
|
* @param string $contentType |
123
|
|
|
* @param string $content |
124
|
|
|
* @param string $uri |
125
|
|
|
* @return ResponseEvent |
126
|
|
|
*/ |
127
|
|
|
private function getEventNotAmpRequestMocked( |
128
|
|
|
$contentType = 'text/html', |
129
|
|
|
$content = '<html ⚡></html>', |
130
|
|
|
$uri = '/segment/doc' |
131
|
|
|
): ResponseEvent { |
132
|
|
|
$headers = $this->prophesize(ParameterBag::class); |
133
|
|
|
$headers->get(Argument::exact('Content-type'))->willReturn($contentType); |
134
|
|
|
|
135
|
|
|
$response = $this->prophesize(Response::class); |
136
|
|
|
if ($contentType === 'text/html' && $uri === '/segment/doc') { |
137
|
|
|
$response->getContent()->shouldBeCalled()->willReturn($content); |
138
|
|
|
} |
139
|
|
|
if ($contentType === 'image/jpeg') { |
140
|
|
|
$response->getContent()->shouldNotBeCalled(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$response->headers = $headers; |
144
|
|
|
$response = $response->reveal(); |
145
|
|
|
|
146
|
|
|
$request = $this->prophesize(Request::class); |
147
|
|
|
$request->getUri()->willReturn($uri); |
148
|
|
|
$request = $request->reveal(); |
149
|
|
|
|
150
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
151
|
|
|
$event->isMasterRequest()->shouldBeCalled()->willReturn(true); |
152
|
|
|
$event->getResponse()->shouldBeCalled()->willReturn($response); |
153
|
|
|
$event->getRequest()->shouldBeCalled()->willReturn($request); |
154
|
|
|
return $event->reveal(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Test normal operation |
159
|
|
|
*/ |
160
|
|
|
public function testTransformRequest() |
161
|
|
|
{ |
162
|
|
|
$instance = $this->getInstance(); |
163
|
|
|
$event = $this->getEventMasterRequestMocked(); |
164
|
|
|
$instance->onKernelResponse($event); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Provide instance to test normal operation and test calls |
169
|
|
|
* @return AmpOptimizerSubscriber |
170
|
|
|
*/ |
171
|
|
|
private function getInstance(): AmpOptimizerSubscriber |
172
|
|
|
{ |
173
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
174
|
|
|
|
175
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
176
|
|
|
$transformationEngine->optimizeHtml(Argument::type('string'), Argument::type(ErrorCollection::class)) |
177
|
|
|
->shouldBeCalled(); |
178
|
|
|
|
179
|
|
|
return new AmpOptimizerSubscriber( |
180
|
|
|
$logger->reveal(), |
181
|
|
|
$transformationEngine->reveal(), |
182
|
|
|
['transform_enabled' => true] |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Test disable transform from config |
189
|
|
|
*/ |
190
|
|
|
public function testNotConfigDisableRequest() |
191
|
|
|
{ |
192
|
|
|
$instance = $this->getInstanceConfigDisabledRequest(); |
193
|
|
|
$event = $this->getEventConfigDisabledMocked(); |
194
|
|
|
$instance->onKernelResponse($event); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Provide instance to test disable transform from config and test calls |
199
|
|
|
* @return AmpOptimizerSubscriber |
200
|
|
|
*/ |
201
|
|
|
private function getInstanceConfigDisabledRequest(): AmpOptimizerSubscriber |
202
|
|
|
{ |
203
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
204
|
|
|
|
205
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
206
|
|
|
$transformationEngine->optimizeHtml(Argument::type('string'), Argument::type(ErrorCollection::class)) |
207
|
|
|
->shouldNotBeCalled(); |
208
|
|
|
|
209
|
|
|
return new AmpOptimizerSubscriber( |
210
|
|
|
$logger->reveal(), |
211
|
|
|
$transformationEngine->reveal(), |
212
|
|
|
[] |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Provide response event to test disable transform from config and test calls |
218
|
|
|
* @return ResponseEvent |
219
|
|
|
*/ |
220
|
|
|
private function getEventConfigDisabledMocked(): ResponseEvent |
221
|
|
|
{ |
222
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
223
|
|
|
$event->isMasterRequest()->shouldNotBeCalled()->willReturn(false); |
224
|
|
|
$event->getResponse()->shouldNotBeCalled(); |
225
|
|
|
return $event->reveal(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Test normal operation with error log |
231
|
|
|
*/ |
232
|
|
|
public function testTransformRequestWithErrorLog() |
233
|
|
|
{ |
234
|
|
|
$instance = $this->getInstanceWithErrorLog(); |
235
|
|
|
$event = $this->getEventMasterRequestMocked(); |
236
|
|
|
$instance->onKernelResponse($event); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Provide instance to test normal operation with error loga nd test calls |
241
|
|
|
* @return AmpOptimizerSubscriber |
242
|
|
|
* @throws ReflectionException |
243
|
|
|
*/ |
244
|
|
|
private function getInstanceWithErrorLog(): AmpOptimizerSubscriber |
245
|
|
|
{ |
246
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
247
|
|
|
$logger->error(Argument::any())->shouldBeCalled(); |
248
|
|
|
|
249
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
250
|
|
|
$transformationEngine->optimizeHtml(Argument::type('string'), Argument::type(ErrorCollection::class)) |
251
|
|
|
->shouldBeCalled(); |
252
|
|
|
|
253
|
|
|
$instance = new AmpOptimizerSubscriber( |
254
|
|
|
$logger->reveal(), |
255
|
|
|
$transformationEngine->reveal(), |
256
|
|
|
['transform_enabled' => true] |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
$reflection = new \ReflectionClass($instance); |
260
|
|
|
$property = $reflection->getProperty('errorCollection'); |
261
|
|
|
$property->setAccessible(true); |
262
|
|
|
$errorCollection = new ErrorCollection(); |
263
|
|
|
$errorCollection->add(new UnknownError('example error message')); |
264
|
|
|
$property->setValue($instance, $errorCollection); |
265
|
|
|
|
266
|
|
|
return $instance; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Provide response event to test normal operation log and test calls |
271
|
|
|
* @param string $uri |
272
|
|
|
* @return ResponseEvent |
273
|
|
|
*/ |
274
|
|
|
private function getEventMasterRequestMocked($uri = '/segment/doc'): ResponseEvent |
275
|
|
|
{ |
276
|
|
|
$headers = $this->prophesize(ParameterBag::class); |
277
|
|
|
$headers->get(Argument::exact('Content-type'))->willReturn('text/html'); |
278
|
|
|
|
279
|
|
|
$response = $this->prophesize(Response::class); |
280
|
|
|
$response->getContent()->shouldBeCalled()->willReturn('<html ⚡></html>'); |
281
|
|
|
$response->setContent(null)->shouldBeCalled(); |
282
|
|
|
$response->headers = $headers; |
283
|
|
|
$response = $response->reveal(); |
284
|
|
|
|
285
|
|
|
$request = $this->prophesize(Request::class); |
286
|
|
|
$request->getUri()->willReturn($uri); |
287
|
|
|
$request = $request->reveal(); |
288
|
|
|
|
289
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
290
|
|
|
$event->isMasterRequest()->shouldBeCalled()->willReturn(true); |
291
|
|
|
$event->getResponse()->shouldBeCalled()->willReturn($response); |
292
|
|
|
$event->getRequest()->shouldBeCalled()->willReturn($request); |
293
|
|
|
return $event->reveal(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Test disabled by property |
298
|
|
|
*/ |
299
|
|
|
public function testDisabledByProperty() |
300
|
|
|
{ |
301
|
|
|
$instance = $this->getInstanceDisabledByProperty(); |
302
|
|
|
$instance->setEnabled(false); |
303
|
|
|
$event = $this->getEventDisabledByPropertyMocked(); |
304
|
|
|
$instance->onKernelResponse($event); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Test isEnabled property |
309
|
|
|
*/ |
310
|
|
|
public function testIsEnabledProperty() |
311
|
|
|
{ |
312
|
|
|
$instance = $this->getInstanceDisabledByProperty(); |
313
|
|
|
$instance->setEnabled(false); |
314
|
|
|
$this->assertFalse($instance->isEnabled()); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Provide instance to test with disabled by property and test calls |
319
|
|
|
* @return AmpOptimizerSubscriber |
320
|
|
|
*/ |
321
|
|
|
private function getInstanceDisabledByProperty(): AmpOptimizerSubscriber |
322
|
|
|
{ |
323
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
324
|
|
|
|
325
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
326
|
|
|
$transformationEngine->optimizeHtml(Argument::type('string'), Argument::type(ErrorCollection::class)) |
327
|
|
|
->shouldNotBeCalled(); |
328
|
|
|
|
329
|
|
|
return new AmpOptimizerSubscriber( |
330
|
|
|
$logger->reveal(), |
331
|
|
|
$transformationEngine->reveal(), |
332
|
|
|
['transform_enabled' => true] |
333
|
|
|
); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Provide response event to test with disabled by property and test calls |
338
|
|
|
* @return ResponseEvent |
339
|
|
|
*/ |
340
|
|
|
private function getEventDisabledByPropertyMocked(): ResponseEvent |
341
|
|
|
{ |
342
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
343
|
|
|
$event->isMasterRequest()->shouldNotBeCalled(); |
344
|
|
|
$event->getResponse()->shouldNotBeCalled(); |
345
|
|
|
return $event->reveal(); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|