|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace CoreTest\Controller; |
|
11
|
|
|
|
|
12
|
|
|
use Test\Bootstrap; |
|
13
|
|
|
use Zend\Http\Response; |
|
14
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
|
15
|
|
|
use Zend\Mvc\MvcEvent; |
|
16
|
|
|
use Zend\Mvc\Router\RouteMatch; |
|
17
|
|
|
use Zend\Mvc\Router\SimpleRouteStack; |
|
18
|
|
|
use Zend\Mvc\Service\RouterFactory; |
|
19
|
|
|
use PHPUnit_Framework_ExpectationFailedException; |
|
20
|
|
|
|
|
21
|
|
|
abstract class AbstractControllerTestCase extends \PHPUnit_Framework_TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var RouteMatch |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $routeMatch; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var MvcEvent |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $event; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var AbstractActionController |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $controller; |
|
37
|
|
|
|
|
38
|
|
|
public function init($controllerName, $actionName = 'index', $lang = 'en') |
|
39
|
|
|
{ |
|
40
|
|
|
$this->routeMatch = new RouteMatch( |
|
41
|
|
|
array( |
|
42
|
|
|
'controller' => $controllerName, |
|
43
|
|
|
'action' => $actionName, |
|
44
|
|
|
'lang' => $lang |
|
45
|
|
|
) |
|
46
|
|
|
); |
|
47
|
|
|
$this->event = new MvcEvent(); |
|
48
|
|
|
$this->event->setRouteMatch($this->routeMatch); |
|
49
|
|
|
|
|
50
|
|
|
/** @var SimpleRouteStack $router */ |
|
51
|
|
|
$routerFactory = new RouterFactory(); |
|
52
|
|
|
$router = $routerFactory->createService(clone Bootstrap::getServiceManager()); |
|
53
|
|
|
$router->setDefaultParam('lang', $lang); |
|
54
|
|
|
|
|
55
|
|
|
$this->event->setRouter($router); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function assertResponseStatusCode($code) |
|
59
|
|
|
{ |
|
60
|
|
|
/** @var Response $response */ |
|
61
|
|
|
$response = $this->controller->getResponse(); |
|
62
|
|
|
$this->assertSame($code, $response->getStatusCode()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get response header by key |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $header |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Zend\Http\Header\HeaderInterface|false |
|
|
|
|
|
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getResponseHeader($header) |
|
73
|
|
|
{ |
|
74
|
|
|
/** @var Response $response */ |
|
75
|
|
|
$response = $this->controller->getResponse(); |
|
76
|
|
|
$headers = $response->getHeaders(); |
|
77
|
|
|
$responseHeader = $headers->get($header, false); |
|
|
|
|
|
|
78
|
|
|
return $responseHeader; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Assert that response is a redirect |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function assertRedirect() |
|
85
|
|
|
{ |
|
86
|
|
|
$responseHeader = $this->getResponseHeader('Location'); |
|
87
|
|
|
if (false === $responseHeader) { |
|
88
|
|
|
throw new PHPUnit_Framework_ExpectationFailedException( |
|
89
|
|
|
'Failed asserting response is a redirect' |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
$this->assertNotEquals(false, $responseHeader); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Assert that response is NOT a redirect |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function assertNotRedirect() |
|
99
|
|
|
{ |
|
100
|
|
|
$responseHeader = $this->getResponseHeader('Location'); |
|
101
|
|
|
if (false !== $responseHeader) { |
|
102
|
|
|
throw new PHPUnit_Framework_ExpectationFailedException( |
|
103
|
|
|
sprintf( |
|
104
|
|
|
'Failed asserting response is NOT a redirect, actual redirection is "%s"', |
|
105
|
|
|
$responseHeader->getFieldValue() |
|
|
|
|
|
|
106
|
|
|
) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
$this->assertFalse($responseHeader); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Assert that response redirects to given URL |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $url |
|
116
|
|
|
* |
|
117
|
|
|
* @throws \PHPUnit_Framework_ExpectationFailedException |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function assertRedirectTo($url) |
|
120
|
|
|
{ |
|
121
|
|
|
$responseHeader = $this->getResponseHeader('Location'); |
|
122
|
|
|
if (!$responseHeader) { |
|
123
|
|
|
throw new PHPUnit_Framework_ExpectationFailedException( |
|
124
|
|
|
'Failed asserting response is a redirect' |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
if ($url != $responseHeader->getFieldValue()) { |
|
128
|
|
|
throw new PHPUnit_Framework_ExpectationFailedException( |
|
129
|
|
|
sprintf( |
|
130
|
|
|
'Failed asserting response redirects to "%s", actual redirection is "%s"', |
|
131
|
|
|
$url, |
|
132
|
|
|
$responseHeader->getFieldValue() |
|
|
|
|
|
|
133
|
|
|
) |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
$this->assertEquals($url, $responseHeader->getFieldValue()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Assert that response does not redirect to given URL |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $url |
|
143
|
|
|
* |
|
144
|
|
|
* @throws \PHPUnit_Framework_ExpectationFailedException |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function assertNotRedirectTo($url) |
|
147
|
|
|
{ |
|
148
|
|
|
$responseHeader = $this->getResponseHeader('Location'); |
|
149
|
|
|
if (!$responseHeader) { |
|
150
|
|
|
throw new PHPUnit_Framework_ExpectationFailedException( |
|
151
|
|
|
'Failed asserting response is a redirect' |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
if ($url == $responseHeader->getFieldValue()) { |
|
|
|
|
|
|
155
|
|
|
throw new PHPUnit_Framework_ExpectationFailedException( |
|
156
|
|
|
sprintf( |
|
157
|
|
|
'Failed asserting response redirects to "%s"', |
|
158
|
|
|
$url |
|
159
|
|
|
) |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
$this->assertNotEquals($url, $responseHeader->getFieldValue()); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Assert that redirect location matches pattern |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $pattern |
|
169
|
|
|
* |
|
170
|
|
|
* @throws \PHPUnit_Framework_ExpectationFailedException |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function assertRedirectRegex($pattern) |
|
173
|
|
|
{ |
|
174
|
|
|
$responseHeader = $this->getResponseHeader('Location'); |
|
175
|
|
|
if (!$responseHeader) { |
|
176
|
|
|
throw new PHPUnit_Framework_ExpectationFailedException( |
|
177
|
|
|
'Failed asserting response is a redirect' |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
if (!preg_match($pattern, $responseHeader->getFieldValue())) { |
|
181
|
|
|
throw new PHPUnit_Framework_ExpectationFailedException( |
|
182
|
|
|
sprintf( |
|
183
|
|
|
'Failed asserting response redirects to URL MATCHING "%s", actual redirection is "%s"', |
|
184
|
|
|
$pattern, |
|
185
|
|
|
$responseHeader->getFieldValue() |
|
|
|
|
|
|
186
|
|
|
) |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
$this->assertTrue((bool)preg_match($pattern, $responseHeader->getFieldValue())); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Assert that redirect location does not match pattern |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $pattern |
|
196
|
|
|
* |
|
197
|
|
|
* @throws \PHPUnit_Framework_ExpectationFailedException |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function assertNotRedirectRegex($pattern) |
|
200
|
|
|
{ |
|
201
|
|
|
$responseHeader = $this->getResponseHeader('Location'); |
|
202
|
|
|
if (!$responseHeader) { |
|
203
|
|
|
throw new PHPUnit_Framework_ExpectationFailedException( |
|
204
|
|
|
'Failed asserting response is a redirect' |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
if (preg_match($pattern, $responseHeader->getFieldValue())) { |
|
|
|
|
|
|
208
|
|
|
throw new PHPUnit_Framework_ExpectationFailedException( |
|
209
|
|
|
sprintf( |
|
210
|
|
|
'Failed asserting response DOES NOT redirect to URL MATCHING "%s"', |
|
211
|
|
|
$pattern |
|
212
|
|
|
) |
|
213
|
|
|
); |
|
214
|
|
|
} |
|
215
|
|
|
$this->assertFalse((bool)preg_match($pattern, $responseHeader->getFieldValue())); |
|
216
|
|
|
} |
|
217
|
|
|
} |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.