1
|
|
|
<?php |
2
|
|
|
namespace Agavi\Testing; |
3
|
|
|
|
4
|
|
|
// +---------------------------------------------------------------------------+ |
5
|
|
|
// | This file is part of the Agavi package. | |
6
|
|
|
// | Copyright (c) 2005-2011 the Agavi Project. | |
7
|
|
|
// | | |
8
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
9
|
|
|
// | file that was distributed with this source code. You can also view the | |
10
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
11
|
|
|
// | vi: set noexpandtab: | |
12
|
|
|
// | Local Variables: | |
13
|
|
|
// | indent-tabs-mode: t | |
14
|
|
|
// | End: | |
15
|
|
|
// +---------------------------------------------------------------------------+ |
16
|
|
|
use Agavi\Dispatcher\ExecutionContainer; |
17
|
|
|
use Agavi\Response\WebResponse; |
18
|
|
|
use Agavi\Testing\PHPUnit\Constraint\ConstraintViewHandlesOutputType; |
19
|
|
|
use Agavi\Util\Toolkit; |
20
|
|
|
use Agavi\View\View; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ViewTestCase is the base class for all view testcases and provides |
24
|
|
|
* the necessary assertions |
25
|
|
|
* |
26
|
|
|
* |
27
|
|
|
* @package agavi |
28
|
|
|
* @subpackage testing |
29
|
|
|
* |
30
|
|
|
* @author Felix Gilcher <[email protected]> |
31
|
|
|
* @copyright The Agavi Project |
32
|
|
|
* |
33
|
|
|
* @since 1.0.0 |
34
|
|
|
* |
35
|
|
|
* @version $Id$ |
36
|
|
|
*/ |
37
|
|
|
abstract class ViewTestCase extends FragmentTestCase |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var string the (short) name of the view |
41
|
|
|
*/ |
42
|
|
|
protected $viewName; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var mixed the result of the view execution |
46
|
|
|
*/ |
47
|
|
|
protected $viewResult; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* creates the view instance for this testcase |
51
|
|
|
* |
52
|
|
|
* @return View |
53
|
|
|
* |
54
|
|
|
* @author Felix Gilcher <[email protected]> |
55
|
|
|
* @since 1.0.0 |
56
|
|
|
*/ |
57
|
|
|
protected function createViewInstance() |
58
|
|
|
{ |
59
|
|
|
$this->getContext()->getDispatcher()->initializeModule($this->moduleName); |
60
|
|
|
$viewName = $this->normalizeViewName($this->viewName); |
61
|
|
|
$viewInstance = $this->getContext()->getDispatcher()->createViewInstance($this->moduleName, $viewName); |
62
|
|
|
$viewInstance->initialize($this->container); |
63
|
|
|
return $viewInstance; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* runs the view instance for this testcase |
68
|
|
|
* |
69
|
|
|
* @param string $otName the name of the output type to run the view for |
70
|
|
|
* null for the default output type |
71
|
|
|
* |
72
|
|
|
* @author Felix Gilcher <[email protected]> |
73
|
|
|
* @since 1.0.0 |
74
|
|
|
*/ |
75
|
|
|
protected function runView($otName = null) |
76
|
|
|
{ |
77
|
|
|
$this->container->setControllerInstance($this->createControllerInstance()); |
|
|
|
|
78
|
|
|
$this->container->setOutputType($this->getContext()->getDispatcher()->getOutputType($otName)); |
79
|
|
|
$this->container->setViewInstance($this->createViewInstance()); |
80
|
|
|
$executionFilter = $this->createExecutionFilter(); |
81
|
|
|
$this->viewResult = $executionFilter->executeView($this->container); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* assert that the view handles the given output type |
86
|
|
|
* |
87
|
|
|
* @param string $method the output type name |
88
|
|
|
* @param boolean $acceptGeneric true if the generic 'execute' method should be accepted as handled |
89
|
|
|
* @param string $message an optional message to display if the test fails |
90
|
|
|
* |
91
|
|
|
* @author Felix Gilcher <[email protected]> |
92
|
|
|
* @since 1.0.0 |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
protected function assertHandlesOutputType($method, $acceptGeneric = false, $message = '') |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$viewInstance = $this->createViewInstance(); |
97
|
|
|
$constraint = new ConstraintViewHandlesOutputType($viewInstance, $acceptGeneric); |
98
|
|
|
|
99
|
|
|
self::assertThat($method, $constraint, $message); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* assert that the view does not handle the given output type |
104
|
|
|
* |
105
|
|
|
* @param string $method the output type name |
106
|
|
|
* @param boolean $acceptGeneric true if the generic 'execute' method should be accepted as handled |
107
|
|
|
* @param string $message an optional message to display if the test fails |
108
|
|
|
* |
109
|
|
|
* @author Felix Gilcher <[email protected]> |
110
|
|
|
* @since 1.0.0 |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
protected function assertNotHandlesOutputType($method, $acceptGeneric = false, $message = '') |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$viewInstance = $this->createViewInstance(); |
115
|
|
|
$constraint = self::logicalNot(new ConstraintViewHandlesOutputType($viewInstance, $acceptGeneric)); |
116
|
|
|
|
117
|
|
|
self::assertThat($method, $constraint, $message); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* assert that the response contains a redirect |
122
|
|
|
* |
123
|
|
|
* @param string $message the message to emit on failure |
124
|
|
|
* |
125
|
|
|
* @author Felix Gilcher <[email protected]> |
126
|
|
|
* @since 1.0.0 |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
protected function assertViewRedirects($message = 'Failed asserting that the view redirects') |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$response = $this->container->getResponse(); |
131
|
|
|
try { |
132
|
|
|
$this->assertTrue($response->hasRedirect(), $message); |
133
|
|
|
} catch (\Exception $e) { |
134
|
|
|
$this->fail($message); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* assert that the response contains no redirect |
140
|
|
|
* |
141
|
|
|
* @param string $message the message to emit on failure |
142
|
|
|
* |
143
|
|
|
* @author Felix Gilcher <[email protected]> |
144
|
|
|
* @since 1.0.0 |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
protected function assertViewRedirectsNot($message = 'Failed asserting that the view does not redirect') |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$response = $this->container->getResponse(); |
149
|
|
|
try { |
150
|
|
|
$this->assertFalse($response->hasRedirect(), $message); |
151
|
|
|
} catch (\Exception $e) { |
152
|
|
|
$this->fail($message); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* assert that the response contains the expected redirect |
158
|
|
|
* |
159
|
|
|
* @param mixed $expected the expected redirect |
160
|
|
|
* @param string $message the message to emit on failure |
161
|
|
|
* |
162
|
|
|
* @author Felix Gilcher <[email protected]> |
163
|
|
|
* @since 1.0.0 |
164
|
|
|
*/ |
165
|
|
|
protected function assertViewRedirectsTo($expected, $message = 'Failed asserting that the view redirects to the given target.') |
166
|
|
|
{ |
167
|
|
|
$response = $this->container->getResponse(); |
168
|
|
|
try { |
169
|
|
|
$this->assertEquals($expected, $response->getRedirect(), $message); |
170
|
|
|
} catch (\Exception $e) { |
171
|
|
|
$this->fail($message); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Assert that the view sets the given content type. |
177
|
|
|
* |
178
|
|
|
* this assertion only works on WebResponse or subclasses |
179
|
|
|
* |
180
|
|
|
* @param string $expected the expected content type |
181
|
|
|
* @param string $message the message to emit on failure |
182
|
|
|
* |
183
|
|
|
* @author Felix Gilcher <[email protected]> |
184
|
|
|
* @since 1.0.0 |
185
|
|
|
*/ |
186
|
|
View Code Duplication |
protected function assertViewSetsContentType($expected, $message = 'Failed asserting that the view sets the content type "%1$s".') |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$response = $this->container->getResponse(); |
189
|
|
|
|
190
|
|
|
if (!($response instanceof WebResponse)) { |
191
|
|
|
$this->fail(sprintf($message . ' (response is not an WebResponse)', $expected)); |
192
|
|
|
} |
193
|
|
|
$this->assertEquals($expected, $response->getContentType(), sprintf($message, $expected)); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Assert that the view sets the given header with the given value. |
198
|
|
|
* |
199
|
|
|
* this response only works on WebResponse and subclasses |
200
|
|
|
* |
201
|
|
|
* @param string $expected the name of the expected header |
202
|
|
|
* @param string $expectedValue the value of the expected header |
203
|
|
|
* @param string $message the message to emit on failure |
204
|
|
|
* |
205
|
|
|
* @author Felix Gilcher <[email protected]> |
206
|
|
|
* @since 1.0.0 |
207
|
|
|
*/ |
208
|
|
View Code Duplication |
protected function assertViewSetsHeader($expected, $expectedValue = null, $message = 'Failed asserting that the view sets a header named <%1$s> with the value <%2$s>') |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
$response = $this->container->getResponse(); |
211
|
|
|
|
212
|
|
|
if (!($response instanceof WebResponse)) { |
213
|
|
|
$this->fail(sprintf($message . ' (response is not an WebResponse)', $expected)); |
214
|
|
|
} |
215
|
|
|
$this->assertEquals($expectedValue, $response->getHttpHeader($expected), sprintf($message, $expected, $expectedValue)); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Assert that the view sets the given cookie with the given value.<y></y> |
220
|
|
|
* |
221
|
|
|
* this response only works on WebResponse and subclasses |
222
|
|
|
* |
223
|
|
|
* @param string $expected the name of the expected cookie |
224
|
|
|
* @param string $expectedValue the value of the expected header |
225
|
|
|
* @param string $message the message to emit on failure |
226
|
|
|
* |
227
|
|
|
* @author Felix Gilcher <[email protected]> |
228
|
|
|
* @since 1.0.0 |
229
|
|
|
*/ |
230
|
|
|
protected function assertViewSetsCookie($expected, $expectedValue = null, $message = 'Failed asserting that the view sets a cookie named <%1$s> with a value of <%2$s>') |
231
|
|
|
{ |
232
|
|
|
$response = $this->container->getResponse(); |
233
|
|
|
|
234
|
|
|
if (!($response instanceof WebResponse)) { |
235
|
|
|
$this->fail(sprintf($message . ' (response is not an WebResponse)', $expected, var_export($expectedValue, true))); |
236
|
|
|
} |
237
|
|
|
$this->assertEquals($expectedValue, $response->getCookie($expected), sprintf($message, $expected, var_export($expectedValue, true))); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* assert that the response has the given http status |
242
|
|
|
* |
243
|
|
|
* this assertion only works on WebResponse or subclasses |
244
|
|
|
* |
245
|
|
|
* @param string $expected the expected http status |
246
|
|
|
* @param string $message the message to emit on failure |
247
|
|
|
* |
248
|
|
|
* @author Felix Gilcher <[email protected]> |
249
|
|
|
* @since 1.0.0 |
250
|
|
|
*/ |
251
|
|
View Code Duplication |
protected function assertViewResponseHasHTTPStatus($expected, $message = 'Failed asserting that the response status is %1$s.') |
|
|
|
|
252
|
|
|
{ |
253
|
|
|
$response = $this->container->getResponse(); |
254
|
|
|
|
255
|
|
|
if (!($response instanceof WebResponse)) { |
256
|
|
|
$this->fail(sprintf($message . ' (response is not an WebResponse)', $expected)); |
257
|
|
|
} |
258
|
|
|
$this->assertEquals($expected, $response->getHttpStatusCode(), sprintf($message, $expected)); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* assert that the response has the given content |
263
|
|
|
* |
264
|
|
|
* @param mixed $expected the expected content |
265
|
|
|
* @param string $message the message to emit on failure |
266
|
|
|
* |
267
|
|
|
* @author Felix Gilcher <[email protected]> |
268
|
|
|
* @since 1.0.0 |
269
|
|
|
*/ |
270
|
|
|
protected function assertViewResponseHasContent($expected, $message = 'Failed asserting that the response has content <%1$s>.') |
271
|
|
|
{ |
272
|
|
|
$response = $this->container->getResponse(); |
273
|
|
|
$this->assertEquals($expected, $response->getContent(), sprintf($message, $expected)); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* assert that the view result has the given content |
278
|
|
|
* |
279
|
|
|
* @param mixed $expected the expected content |
280
|
|
|
* @param string $message the message to emit on failure |
281
|
|
|
* |
282
|
|
|
* @author Felix Gilcher <[email protected]> |
283
|
|
|
* @since 1.0.0 |
284
|
|
|
*/ |
285
|
|
|
protected function assertViewResultEquals($expected, $message = 'Failed asserting the expected view result.') |
286
|
|
|
{ |
287
|
|
|
$this->assertEquals($expected, $this->viewResult, sprintf($message, $expected)); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* assert that the view forwards to the given module/controller |
292
|
|
|
* |
293
|
|
|
* @param string $expectedModule the expected module name |
294
|
|
|
* @param string $expectedController the expected controller name |
295
|
|
|
* @param string $message the message to emit on failure |
296
|
|
|
* |
297
|
|
|
* @author Felix Gilcher <[email protected]> |
298
|
|
|
* @since 1.0.0 |
299
|
|
|
*/ |
300
|
|
|
protected function assertViewForwards($expectedModule, $expectedController, $message = 'Failed asserting that the view forwards to "%1$s" "%2$s".') |
301
|
|
|
{ |
302
|
|
|
if (!($this->viewResult instanceof ExecutionContainer)) { |
303
|
|
|
$this->fail(sprintf($message, $expectedModule, $expectedController)); |
304
|
|
|
} |
305
|
|
|
$this->assertEquals($expectedModule, $this->viewResult->getModuleName()); |
306
|
|
|
$this->assertEquals(Toolkit::canonicalName($expectedController), $this->viewResult->getControllerName()); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* assert that the view has the given layer |
311
|
|
|
* |
312
|
|
|
* @param string $expectedLayer the expected layer name |
313
|
|
|
* @param string $message the message to emit on failure |
314
|
|
|
* |
315
|
|
|
* @author Felix Gilcher <[email protected]> |
316
|
|
|
* @since 1.0.0 |
317
|
|
|
*/ |
318
|
|
View Code Duplication |
protected function assertHasLayer($expectedLayer, $message = 'Failed asserting that the view contains the layer "%1$s".') |
|
|
|
|
319
|
|
|
{ |
320
|
|
|
$viewInstance = $this->container->getViewInstance(); |
321
|
|
|
$layer = $viewInstance->getLayer($expectedLayer); |
322
|
|
|
|
323
|
|
|
if (null === $layer) { |
324
|
|
|
$this->fail(sprintf($message, $expectedLayer)); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* assert that the view has the given layer |
330
|
|
|
* |
331
|
|
|
* @param string $expectedLayer the expected layer name |
332
|
|
|
* @param string $message the message to emit on failure |
333
|
|
|
* |
334
|
|
|
* @author David Zülke <[email protected]> |
335
|
|
|
* @since 1.0.6 |
336
|
|
|
*/ |
337
|
|
View Code Duplication |
protected function assertNotHasLayer($expectedLayer, $message = '') |
|
|
|
|
338
|
|
|
{ |
339
|
|
|
$viewInstance = $this->container->getViewInstance(); |
340
|
|
|
$layer = $viewInstance->getLayer($expectedLayer); |
341
|
|
|
|
342
|
|
|
if (null !== $layer) { |
343
|
|
|
$this->fail('Failed asserting that the view does not contain the layer.'); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.