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\Response; |
16
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
17
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
18
|
|
|
|
19
|
|
|
class AmpOptimizerSubscriberTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function setUp(): void |
22
|
|
|
{ |
23
|
|
|
BypassFinals::enable(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Test simple getSubscribedEvents function |
28
|
|
|
*/ |
29
|
|
|
public function testSubscribedEvents() |
30
|
|
|
{ |
31
|
|
|
$events = AmpOptimizerSubscriber::getSubscribedEvents(); |
32
|
|
|
$eventsExpected = [KernelEvents::RESPONSE => ['onKernelResponse', -10]]; |
33
|
|
|
$this->assertEquals($events, $eventsExpected); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Test not master request |
38
|
|
|
*/ |
39
|
|
|
public function testNotMasterRequest() |
40
|
|
|
{ |
41
|
|
|
$instance = $this->getInstanceNotMasterRequest(); |
42
|
|
|
$event = $this->getEventNotMasterRequestMocked(); |
43
|
|
|
$instance->onKernelResponse($event); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Provide instance to test with not master request and test calls |
48
|
|
|
* @return AmpOptimizerSubscriber |
49
|
|
|
*/ |
50
|
|
|
private function getInstanceNotMasterRequest(): AmpOptimizerSubscriber |
51
|
|
|
{ |
52
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
53
|
|
|
|
54
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
55
|
|
|
$transformationEngine->optimizeHtml(Argument::type('string'), Argument::type(ErrorCollection::class)) |
56
|
|
|
->shouldNotBeCalled(); |
57
|
|
|
|
58
|
|
|
return new AmpOptimizerSubscriber( |
59
|
|
|
$logger->reveal(), |
60
|
|
|
$transformationEngine->reveal(), |
61
|
|
|
['transform_enabled' => true] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Provide response event to test with not master request and test calls |
67
|
|
|
* @return ResponseEvent |
68
|
|
|
*/ |
69
|
|
|
private function getEventNotMasterRequestMocked(): ResponseEvent |
70
|
|
|
{ |
71
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
72
|
|
|
$event->isMasterRequest()->shouldBeCalled()->willReturn(false); |
73
|
|
|
$event->getResponse()->shouldNotBeCalled(); |
74
|
|
|
return $event->reveal(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Test request without html amp format or content type |
79
|
|
|
*/ |
80
|
|
|
public function testNotAmpRequest() |
81
|
|
|
{ |
82
|
|
|
$instance = $this->getInstanceNotAmpRequest(); |
83
|
|
|
$event = $this->getEventNotAmpRequestMocked('image/jpeg'); |
84
|
|
|
$instance->onKernelResponse($event); |
85
|
|
|
|
86
|
|
|
$event = $this->getEventNotAmpRequestMocked('text/html', '<html></html>'); |
87
|
|
|
$instance->onKernelResponse($event); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Provide instance to test with not html amp request and test calls |
92
|
|
|
* @return AmpOptimizerSubscriber |
93
|
|
|
*/ |
94
|
|
|
private function getInstanceNotAmpRequest(): AmpOptimizerSubscriber |
95
|
|
|
{ |
96
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
97
|
|
|
|
98
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
99
|
|
|
$transformationEngine->optimizeHtml(Argument::type('string'), Argument::type(ErrorCollection::class)) |
100
|
|
|
->shouldNotBeCalled(); |
101
|
|
|
|
102
|
|
|
return new AmpOptimizerSubscriber( |
103
|
|
|
$logger->reveal(), |
104
|
|
|
$transformationEngine->reveal(), |
105
|
|
|
['transform_enabled' => true] |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Provide response event to test with not html amp request and test calls |
111
|
|
|
* @param string $contentType |
112
|
|
|
* @param string $content |
113
|
|
|
* @return ResponseEvent |
114
|
|
|
*/ |
115
|
|
|
private function getEventNotAmpRequestMocked($contentType = 'text/html', $content = '<html ⚡></html>'): ResponseEvent |
116
|
|
|
{ |
117
|
|
|
$headers = $this->prophesize(ParameterBag::class); |
118
|
|
|
$headers->get(Argument::exact('Content-type'))->willReturn($contentType); |
119
|
|
|
|
120
|
|
|
$response = $this->prophesize(Response::class); |
121
|
|
|
if ($contentType === 'text/html') { |
122
|
|
|
$response->getContent()->shouldBeCalled()->willReturn($content); |
123
|
|
|
} |
124
|
|
|
if ($contentType === 'image/jpeg') { |
125
|
|
|
$response->getContent()->shouldNotBeCalled(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$response->headers = $headers; |
129
|
|
|
$response = $response->reveal(); |
130
|
|
|
|
131
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
132
|
|
|
$event->isMasterRequest()->shouldBeCalled()->willReturn(true); |
133
|
|
|
$event->getResponse()->shouldBeCalled()->willReturn($response); |
134
|
|
|
return $event->reveal(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Test normal operation |
139
|
|
|
*/ |
140
|
|
|
public function testTransformRequest() |
141
|
|
|
{ |
142
|
|
|
$instance = $this->getInstance(); |
143
|
|
|
$event = $this->getEventMasterRequestMocked(); |
144
|
|
|
$instance->onKernelResponse($event); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Provide instance to test normal operation and test calls |
149
|
|
|
* @return AmpOptimizerSubscriber |
150
|
|
|
*/ |
151
|
|
|
private function getInstance(): AmpOptimizerSubscriber |
152
|
|
|
{ |
153
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
154
|
|
|
|
155
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
156
|
|
|
$transformationEngine->optimizeHtml(Argument::type('string'), Argument::type(ErrorCollection::class)) |
157
|
|
|
->shouldBeCalled(); |
158
|
|
|
|
159
|
|
|
return new AmpOptimizerSubscriber( |
160
|
|
|
$logger->reveal(), |
161
|
|
|
$transformationEngine->reveal(), |
162
|
|
|
['transform_enabled' => true] |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Test disable transform from config |
169
|
|
|
*/ |
170
|
|
|
public function testNotConfigDisableRequest() |
171
|
|
|
{ |
172
|
|
|
$instance = $this->getInstanceConfigDisabledRequest(); |
173
|
|
|
$event = $this->getEventConfigDisabledMocked(); |
174
|
|
|
$instance->onKernelResponse($event); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Provide instance to test disable transform from config and test calls |
179
|
|
|
* @return AmpOptimizerSubscriber |
180
|
|
|
*/ |
181
|
|
|
private function getInstanceConfigDisabledRequest(): AmpOptimizerSubscriber |
182
|
|
|
{ |
183
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
184
|
|
|
|
185
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
186
|
|
|
$transformationEngine->optimizeHtml(Argument::type('string'), Argument::type(ErrorCollection::class)) |
187
|
|
|
->shouldNotBeCalled(); |
188
|
|
|
|
189
|
|
|
return new AmpOptimizerSubscriber( |
190
|
|
|
$logger->reveal(), |
191
|
|
|
$transformationEngine->reveal(), |
192
|
|
|
[] |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Provide response event to test disable transform from config and test calls |
198
|
|
|
* @return ResponseEvent |
199
|
|
|
*/ |
200
|
|
|
private function getEventConfigDisabledMocked(): ResponseEvent |
201
|
|
|
{ |
202
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
203
|
|
|
$event->isMasterRequest()->shouldNotBeCalled()->willReturn(false); |
204
|
|
|
$event->getResponse()->shouldNotBeCalled(); |
205
|
|
|
return $event->reveal(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Test normal operation with error log |
211
|
|
|
*/ |
212
|
|
|
public function testTransformRequestWithErrorLog() |
213
|
|
|
{ |
214
|
|
|
$instance = $this->getInstanceWithErrorLog(); |
215
|
|
|
$event = $this->getEventMasterRequestMocked(); |
216
|
|
|
$instance->onKernelResponse($event); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Provide instance to test normal operation with error loga nd test calls |
221
|
|
|
* @return AmpOptimizerSubscriber |
222
|
|
|
* @throws ReflectionException |
223
|
|
|
*/ |
224
|
|
|
private function getInstanceWithErrorLog(): AmpOptimizerSubscriber |
225
|
|
|
{ |
226
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
227
|
|
|
$logger->error(Argument::any())->shouldBeCalled(); |
228
|
|
|
|
229
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
230
|
|
|
$transformationEngine->optimizeHtml(Argument::type('string'), Argument::type(ErrorCollection::class)) |
231
|
|
|
->shouldBeCalled(); |
232
|
|
|
|
233
|
|
|
$instance = new AmpOptimizerSubscriber( |
234
|
|
|
$logger->reveal(), |
235
|
|
|
$transformationEngine->reveal(), |
236
|
|
|
['transform_enabled' => true] |
237
|
|
|
); |
238
|
|
|
|
239
|
|
|
$reflection = new \ReflectionClass($instance); |
240
|
|
|
$property = $reflection->getProperty('errorCollection'); |
241
|
|
|
$property->setAccessible(true); |
242
|
|
|
$errorCollection = new ErrorCollection(); |
243
|
|
|
$errorCollection->add(new UnknownError('example error message')); |
244
|
|
|
$property->setValue($instance, $errorCollection); |
245
|
|
|
|
246
|
|
|
return $instance; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Provide response event to test normal operation log and test calls |
251
|
|
|
* @return ResponseEvent |
252
|
|
|
*/ |
253
|
|
|
private function getEventMasterRequestMocked(): ResponseEvent |
254
|
|
|
{ |
255
|
|
|
$headers = $this->prophesize(ParameterBag::class); |
256
|
|
|
$headers->get(Argument::exact('Content-type'))->willReturn('text/html'); |
257
|
|
|
|
258
|
|
|
$response = $this->prophesize(Response::class); |
259
|
|
|
$response->getContent()->shouldBeCalled()->willReturn('<html ⚡></html>'); |
260
|
|
|
$response->setContent(null)->shouldBeCalled(); |
261
|
|
|
$response->headers = $headers; |
262
|
|
|
$response = $response->reveal(); |
263
|
|
|
|
264
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
265
|
|
|
$event->isMasterRequest()->shouldBeCalled()->willReturn(true); |
266
|
|
|
$event->getResponse()->shouldBeCalled()->willReturn($response); |
267
|
|
|
return $event->reveal(); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|