1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Unit\EventSubscriber; |
4
|
|
|
|
5
|
|
|
use AmpProject\Optimizer\ErrorCollection; |
6
|
|
|
use AmpProject\Optimizer\TransformationEngine; |
7
|
|
|
use DG\BypassFinals; |
8
|
|
|
use Hola\AmpToolboxBundle\EventSubscriber\AmpOptimizerSubscriber; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Prophecy\Argument; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
15
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
16
|
|
|
|
17
|
|
|
class AmpOptimizerSubscriberTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public function setUp(): void |
20
|
|
|
{ |
21
|
|
|
BypassFinals::enable(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testSubscribedEvents() |
25
|
|
|
{ |
26
|
|
|
$events = AmpOptimizerSubscriber::getSubscribedEvents(); |
27
|
|
|
$eventsExpected = [KernelEvents::RESPONSE => ['onKernelResponse', -10]]; |
28
|
|
|
$this->assertEquals($events, $eventsExpected); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testNotMasterRequest() |
32
|
|
|
{ |
33
|
|
|
$instance = $this->getInstance(false); |
34
|
|
|
$event = $this->getEventNotMasterRequestMocked(); |
35
|
|
|
$instance->onKernelResponse($event); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testNotAmpRequest() |
39
|
|
|
{ |
40
|
|
|
$instance = $this->getInstance(false); |
41
|
|
|
$event = $this->getEventNotAmpRequestMocked('text/html'); |
42
|
|
|
$instance->onKernelResponse($event); |
43
|
|
|
|
44
|
|
|
$event = $this->getEventNotAmpRequestMocked('image/jpeg'); |
45
|
|
|
$instance->onKernelResponse($event); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testTransformRequest() |
49
|
|
|
{ |
50
|
|
|
$instance = $this->getInstance(); |
51
|
|
|
$event = $this->getEventMasterRequestMocked(); |
52
|
|
|
$instance->onKernelResponse($event); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testTransformDisabledRequest() |
56
|
|
|
{ |
57
|
|
|
$instance = $this->getInstance(false, []); |
58
|
|
|
$event = $this->getEventMasterRequestTransformDisabledMocked(); |
59
|
|
|
$instance->onKernelResponse($event); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param bool $transform |
64
|
|
|
* @param array $config |
65
|
|
|
* @return AmpOptimizerSubscriber |
66
|
|
|
*/ |
67
|
|
|
private function getInstance($transform = true, $config = ['transform_enabled' => true]): AmpOptimizerSubscriber |
68
|
|
|
{ |
69
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
70
|
|
|
|
71
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
72
|
|
|
|
73
|
|
|
if ($transform) { |
74
|
|
|
$transformationEngine->optimizeHtml( |
75
|
|
|
Argument::type('string'), |
76
|
|
|
Argument::type(ErrorCollection::class) |
77
|
|
|
)->shouldBeCalled(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!$transform) { |
81
|
|
|
$transformationEngine->optimizeHtml( |
82
|
|
|
Argument::type('string'), |
83
|
|
|
Argument::type(ErrorCollection::class) |
84
|
|
|
)->shouldNotBeCalled(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new AmpOptimizerSubscriber( |
88
|
|
|
$logger->reveal(), |
89
|
|
|
$transformationEngine->reveal(), |
90
|
|
|
$config |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return ResponseEvent |
96
|
|
|
*/ |
97
|
|
|
private function getEventMasterRequestMocked(): ResponseEvent |
98
|
|
|
{ |
99
|
|
|
$headers = $this->prophesize(ParameterBag::class); |
100
|
|
|
$headers->get(Argument::exact('Content-type'))->willReturn('text/html'); |
101
|
|
|
|
102
|
|
|
$response = $this->prophesize(Response::class); |
103
|
|
|
$response->getContent()->shouldBeCalled()->willReturn('<html ⚡></html>'); |
104
|
|
|
$response->setContent(null)->shouldBeCalled(); |
105
|
|
|
$response->headers = $headers; |
106
|
|
|
$response = $response->reveal(); |
107
|
|
|
|
108
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
109
|
|
|
$event->isMasterRequest()->shouldBeCalled()->willReturn(true); |
110
|
|
|
$event->getResponse()->shouldBeCalled()->willReturn($response); |
111
|
|
|
return $event->reveal(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return ResponseEvent |
116
|
|
|
*/ |
117
|
|
|
private function getEventMasterRequestTransformDisabledMocked(): ResponseEvent |
118
|
|
|
{ |
119
|
|
|
$response = $this->prophesize(Response::class); |
120
|
|
|
$response->getContent()->shouldNotBeCalled(); |
121
|
|
|
$response->setContent(null)->shouldNotBeCalled(); |
122
|
|
|
$response = $response->reveal(); |
123
|
|
|
|
124
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
125
|
|
|
$event->isMasterRequest()->shouldNotBeCalled()->willReturn(true); |
126
|
|
|
$event->getResponse()->shouldNotBeCalled()->willReturn($response); |
127
|
|
|
return $event->reveal(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $contentType |
132
|
|
|
* @return ResponseEvent |
133
|
|
|
*/ |
134
|
|
|
private function getEventNotAmpRequestMocked($contentType = 'text/html'): ResponseEvent |
135
|
|
|
{ |
136
|
|
|
$headers = $this->prophesize(ParameterBag::class); |
137
|
|
|
$headers->get(Argument::exact('Content-type'))->willReturn($contentType); |
138
|
|
|
|
139
|
|
|
$response = $this->prophesize(Response::class); |
140
|
|
|
if ($contentType === 'text/html') { |
141
|
|
|
$response->getContent()->shouldBeCalled()->willReturn('<html></html>'); |
142
|
|
|
} |
143
|
|
|
if ($contentType === 'image/jpeg') { |
144
|
|
|
$response->getContent()->shouldNotBeCalled(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$response->headers = $headers; |
148
|
|
|
$response = $response->reveal(); |
149
|
|
|
|
150
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
151
|
|
|
$event->isMasterRequest()->shouldBeCalled()->willReturn(true); |
152
|
|
|
$event->getResponse()->shouldBeCalled()->willReturn($response); |
153
|
|
|
return $event->reveal(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return ResponseEvent |
158
|
|
|
*/ |
159
|
|
|
private function getEventNotMasterRequestMocked(): ResponseEvent |
160
|
|
|
{ |
161
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
162
|
|
|
$event->isMasterRequest()->shouldBeCalled()->willReturn(false); |
163
|
|
|
$event->getResponse()->shouldNotBeCalled(); |
164
|
|
|
return $event->reveal(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|