|
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\Response; |
|
13
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
|
14
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
15
|
|
|
|
|
16
|
|
|
class AmpOptimizerSubscriberTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function setUp(): void |
|
19
|
|
|
{ |
|
20
|
|
|
BypassFinals::enable(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testSubscribedEvents() |
|
24
|
|
|
{ |
|
25
|
|
|
$events = AmpOptimizerSubscriber::getSubscribedEvents(); |
|
26
|
|
|
$eventsExpected = [KernelEvents::RESPONSE => ['onKernelResponse', -10]]; |
|
27
|
|
|
$this->assertEquals($events, $eventsExpected); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testNotMasterRequest() |
|
31
|
|
|
{ |
|
32
|
|
|
$instance = $this->getInstance(false); |
|
33
|
|
|
$event = $this->getEventNotMasterRequestMocked(); |
|
34
|
|
|
$instance->onKernelResponse($event); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testNotAmpRequest() |
|
38
|
|
|
{ |
|
39
|
|
|
$instance = $this->getInstance(false); |
|
40
|
|
|
$event = $this->getEventNotAmpRequestMocked(); |
|
41
|
|
|
$instance->onKernelResponse($event); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testTransformRequest() |
|
45
|
|
|
{ |
|
46
|
|
|
$instance = $this->getInstance(); |
|
47
|
|
|
$event = $this->getEventMasterRequestMocked(); |
|
48
|
|
|
$instance->onKernelResponse($event); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param bool $transform |
|
53
|
|
|
* @param array $config |
|
54
|
|
|
* @return AmpOptimizerSubscriber |
|
55
|
|
|
*/ |
|
56
|
|
|
private function getInstance($transform = true, $config = []): AmpOptimizerSubscriber |
|
57
|
|
|
{ |
|
58
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
|
59
|
|
|
|
|
60
|
|
|
$transformationEngine = $this->prophesize(TransformationEngine::class); |
|
61
|
|
|
|
|
62
|
|
|
if ($transform) { |
|
63
|
|
|
$transformationEngine->optimizeHtml( |
|
64
|
|
|
Argument::type('string'), |
|
65
|
|
|
Argument::type(ErrorCollection::class) |
|
66
|
|
|
)->shouldBeCalled(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!$transform) { |
|
70
|
|
|
$transformationEngine->optimizeHtml( |
|
71
|
|
|
Argument::type('string'), |
|
72
|
|
|
Argument::type(ErrorCollection::class) |
|
73
|
|
|
)->shouldNotBeCalled(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return new AmpOptimizerSubscriber( |
|
77
|
|
|
$logger->reveal(), |
|
78
|
|
|
$transformationEngine->reveal(), |
|
79
|
|
|
$config |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return ResponseEvent |
|
85
|
|
|
*/ |
|
86
|
|
|
private function getEventMasterRequestMocked(): ResponseEvent |
|
87
|
|
|
{ |
|
88
|
|
|
$response = $this->prophesize(Response::class); |
|
89
|
|
|
$response->getContent()->shouldBeCalled()->willReturn('<html ⚡></html>'); |
|
90
|
|
|
$response->setContent(null)->shouldBeCalled(); |
|
91
|
|
|
$response = $response->reveal(); |
|
92
|
|
|
|
|
93
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
|
94
|
|
|
$event->isMasterRequest()->shouldBeCalled()->willReturn(true); |
|
95
|
|
|
$event->getResponse()->shouldBeCalled()->willReturn($response); |
|
96
|
|
|
return $event->reveal(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return ResponseEvent |
|
101
|
|
|
*/ |
|
102
|
|
|
private function getEventNotAmpRequestMocked(): ResponseEvent |
|
103
|
|
|
{ |
|
104
|
|
|
$response = $this->prophesize(Response::class); |
|
105
|
|
|
$response->getContent()->shouldBeCalled()->willReturn('<html></html>'); |
|
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 getEventNotMasterRequestMocked(): ResponseEvent |
|
118
|
|
|
{ |
|
119
|
|
|
$event = $this->prophesize(ResponseEvent::class); |
|
120
|
|
|
$event->isMasterRequest()->shouldBeCalled()->willReturn(false); |
|
121
|
|
|
$event->getResponse()->shouldNotBeCalled(); |
|
122
|
|
|
return $event->reveal(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|