1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: iulianp |
6
|
|
|
* Date: 15.10.2016 |
7
|
|
|
* Time: 19:14. |
8
|
|
|
*/ |
9
|
|
|
class MaintenanceSubscriberTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
const MAINTENANCE_ROUTE_URI = '/maintenance'; |
12
|
|
|
const MAINTENANCE_ROUTE = 'iulyanp_maintenance_homepage'; |
13
|
|
|
|
14
|
|
|
const TEST_ROUTE_URI = '/test'; |
15
|
|
|
const TEST_ROUTE = 'iulyanp_maintenance_test'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @test |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function onKernelRequestRedirectToMaintenance() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$request = $this->getStub( |
23
|
|
|
'Symfony\Component\HttpFoundation\Request', |
|
|
|
|
24
|
|
|
[ |
25
|
|
|
'getRequestUri' => self::TEST_ROUTE_URI, |
26
|
|
|
'get' => self::TEST_ROUTE, |
27
|
|
|
] |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
$event = $this->getStub( |
31
|
|
|
'Symfony\Component\HttpKernel\Event\GetResponseEvent', |
|
|
|
|
32
|
|
|
[ |
33
|
|
|
'isMasterRequest' => true, |
34
|
|
|
'getRequest' => $request, |
35
|
|
|
'setResponse' => new \Symfony\Component\HttpFoundation\RedirectResponse( |
36
|
|
|
self::MAINTENANCE_ROUTE_URI |
37
|
|
|
), |
38
|
|
|
'getResponse' => new \Symfony\Component\HttpFoundation\RedirectResponse( |
39
|
|
|
self::MAINTENANCE_ROUTE_URI |
40
|
|
|
), |
41
|
|
|
] |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$router = $this->getStub( |
45
|
|
|
'Symfony\Bundle\FrameworkBundle\Routing\Router', |
|
|
|
|
46
|
|
|
[ |
47
|
|
|
'generate' => self::MAINTENANCE_ROUTE_URI, |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$subscriber = new \Iulyanp\MaintenanceBundle\EventListener\MaintenanceSubscriber( |
52
|
|
|
$router, [ |
|
|
|
|
53
|
|
|
'enabled' => true, |
54
|
|
|
'due_date' => (new \DateTime('+1 day'))->format('d-m-Y H:i:s'), |
55
|
|
|
'maintenance_route' => self::MAINTENANCE_ROUTE, |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$subscriber->onKernelRequest($event); |
60
|
|
|
|
61
|
|
|
$this->assertEquals(self::MAINTENANCE_ROUTE_URI, $event->getResponse()->getTargetUrl()); |
62
|
|
|
$this->assertContains( |
63
|
|
|
sprintf('Redirecting to <a href="%s">%s</a>', self::MAINTENANCE_ROUTE_URI, self::MAINTENANCE_ROUTE_URI), |
64
|
|
|
$event->getResponse()->getContent() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @test |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function onKernelRequestNormallyRedirect() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$request = $this->getStub( |
74
|
|
|
'Symfony\Component\HttpFoundation\Request', |
|
|
|
|
75
|
|
|
[ |
76
|
|
|
'getRequestUri' => self::TEST_ROUTE_URI, |
77
|
|
|
'get' => self::TEST_ROUTE, |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$event = $this->getStub( |
82
|
|
|
'Symfony\Component\HttpKernel\Event\GetResponseEvent', |
|
|
|
|
83
|
|
|
[ |
84
|
|
|
'isMasterRequest' => true, |
85
|
|
|
'getRequest' => $request, |
86
|
|
|
'setResponse' => new \Symfony\Component\HttpFoundation\RedirectResponse(self::TEST_ROUTE_URI), |
87
|
|
|
'getResponse' => new \Symfony\Component\HttpFoundation\RedirectResponse(self::TEST_ROUTE_URI), |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$router = $this->getStub( |
92
|
|
|
'Symfony\Bundle\FrameworkBundle\Routing\Router', |
|
|
|
|
93
|
|
|
[ |
94
|
|
|
'generate' => self::MAINTENANCE_ROUTE_URI, |
95
|
|
|
] |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$subscriber = new \Iulyanp\MaintenanceBundle\EventListener\MaintenanceSubscriber( |
99
|
|
|
$router, [ |
|
|
|
|
100
|
|
|
'enabled' => false, |
101
|
|
|
'due_date' => (new \DateTime('+1 day'))->format('d-m-Y H:i:s'), |
102
|
|
|
'maintenance_route' => self::MAINTENANCE_ROUTE, |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$subscriber->onKernelRequest($event); |
107
|
|
|
|
108
|
|
|
$this->assertEquals(self::TEST_ROUTE_URI, $event->getResponse()->getTargetUrl()); |
109
|
|
|
$this->assertContains( |
110
|
|
|
sprintf('Redirecting to <a href="%s">%s</a>', self::TEST_ROUTE_URI, self::TEST_ROUTE_URI), |
111
|
|
|
$event->getResponse()->getContent() |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @test |
117
|
|
|
*/ |
118
|
|
|
public function onKernelRequestRedirectToHomepageWhenMaintenanceDisabled() |
119
|
|
|
{ |
120
|
|
|
$request = $this->getStub( |
121
|
|
|
'Symfony\Component\HttpFoundation\Request', |
|
|
|
|
122
|
|
|
[ |
123
|
|
|
'getRequestUri' => self::MAINTENANCE_ROUTE_URI, |
124
|
|
|
'get' => self::MAINTENANCE_ROUTE, |
125
|
|
|
] |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$event = $this->getStub( |
129
|
|
|
'Symfony\Component\HttpKernel\Event\GetResponseEvent', |
|
|
|
|
130
|
|
|
[ |
131
|
|
|
'isMasterRequest' => true, |
132
|
|
|
'getRequest' => $request, |
133
|
|
|
'setResponse' => new \Symfony\Component\HttpFoundation\RedirectResponse('/'), |
134
|
|
|
'getResponse' => new \Symfony\Component\HttpFoundation\RedirectResponse('/'), |
135
|
|
|
] |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$router = $this->getStub( |
139
|
|
|
'Symfony\Bundle\FrameworkBundle\Routing\Router', |
|
|
|
|
140
|
|
|
[ |
141
|
|
|
'generate' => self::MAINTENANCE_ROUTE_URI, |
142
|
|
|
] |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$subscriber = new \Iulyanp\MaintenanceBundle\EventListener\MaintenanceSubscriber( |
146
|
|
|
$router, [ |
|
|
|
|
147
|
|
|
'enabled' => false, |
148
|
|
|
'due_date' => (new \DateTime('-1 day'))->format('d-m-Y H:i:s'), |
149
|
|
|
'maintenance_route' => self::MAINTENANCE_ROUTE, |
150
|
|
|
] |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$subscriber->onKernelRequest($event); |
154
|
|
|
|
155
|
|
|
$this->assertEquals('/', $event->getResponse()->getTargetUrl()); |
156
|
|
|
$this->assertContains('Redirecting to <a href="/">/</a>', $event->getResponse()->getContent()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param object $class |
161
|
|
|
* @param array $getters |
162
|
|
|
* |
163
|
|
|
* @return PHPUnit_Framework_MockObject_MockObject |
164
|
|
|
*/ |
165
|
|
|
private function getStub($class, array $getters = []) |
166
|
|
|
{ |
167
|
|
|
$stub = $this->getMockBuilder($class) |
|
|
|
|
168
|
|
|
->disableOriginalConstructor() |
169
|
|
|
->getMock(); |
170
|
|
|
|
171
|
|
|
foreach ($getters as $method => $value) { |
172
|
|
|
$stub->method($method)->willReturn($value); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $stub; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.