|
1
|
|
|
<?php |
|
2
|
|
|
namespace Agavi\Testing; |
|
3
|
|
|
// +---------------------------------------------------------------------------+ |
|
4
|
|
|
// | This file is part of the Agavi package. | |
|
5
|
|
|
// | Copyright (c) 2005-2011 the Agavi Project. | |
|
6
|
|
|
// | | |
|
7
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
|
8
|
|
|
// | file that was distributed with this source code. You can also view the | |
|
9
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
|
10
|
|
|
// | vi: set noexpandtab: | |
|
11
|
|
|
// | Local Variables: | |
|
12
|
|
|
// | indent-tabs-mode: t | |
|
13
|
|
|
// | End: | |
|
14
|
|
|
// +---------------------------------------------------------------------------+ |
|
15
|
|
|
use Agavi\Dispatcher\ExecutionContainer; |
|
16
|
|
|
use Agavi\Request\RequestDataHolder; |
|
17
|
|
|
use Agavi\Testing\PHPUnit\Constraint\ConstraintControllerHandlesMethod; |
|
18
|
|
|
use Agavi\Validator\ValidationArgument; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* ControllerTestCase is the base class for all controller testcases and provides |
|
22
|
|
|
* the necessary assertions |
|
23
|
|
|
* |
|
24
|
|
|
* |
|
25
|
|
|
* @package agavi |
|
26
|
|
|
* @subpackage testing |
|
27
|
|
|
* |
|
28
|
|
|
* @author Felix Gilcher <[email protected]> |
|
29
|
|
|
* @copyright The Agavi Project |
|
30
|
|
|
* |
|
31
|
|
|
* @since 1.0.0 |
|
32
|
|
|
* |
|
33
|
|
|
* @version $Id$ |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract class ControllerTestCase extends FragmentTestCase |
|
36
|
|
|
{ |
|
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string the name of the resulting view |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $viewName; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string the name of the resulting view's module |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $viewModuleName; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var ExecutionContainer |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $container; |
|
51
|
|
|
/** |
|
52
|
|
|
* run the controller for this testcase |
|
53
|
|
|
* |
|
54
|
|
|
* @return void |
|
55
|
|
|
* |
|
56
|
|
|
* @author Felix Gilcher <[email protected]> |
|
57
|
|
|
* @since 1.0.0 |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function runController() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->container->setControllerInstance($this->createControllerInstance()); |
|
|
|
|
|
|
62
|
|
|
//$executionFilter = $this->createExecutionFilter(); |
|
|
|
|
|
|
63
|
|
|
$this->container->initRequestData(); |
|
|
|
|
|
|
64
|
|
|
list($this->viewModuleName, $this->viewName) = $this->container->runController(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* register the validators for this testcase |
|
69
|
|
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
* |
|
72
|
|
|
* @author Felix Gilcher <[email protected]> |
|
73
|
|
|
* @since 1.0.0 |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function performValidation() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->container->setControllerInstance($this->createControllerInstance()); |
|
|
|
|
|
|
78
|
|
|
$this->validationSuccess = $this->container->performValidation($this->container); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* asserts that the viewName is the expected value after runController was called |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $expected the expected viewname in short form ('Success' etc) |
|
85
|
|
|
* @param string $message an optional message to display if the test fails |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
* |
|
89
|
|
|
* @author Felix Gilcher <[email protected]> |
|
90
|
|
|
* @since 1.0.0 |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function assertViewNameEquals($expected, $message = 'Failed asserting that the view\'s name is <%1$s>.') |
|
93
|
|
|
{ |
|
94
|
|
|
$expected = $this->normalizeViewName($expected); |
|
95
|
|
|
$this->assertEquals($expected, $this->viewName, sprintf($message, $expected)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* asserts that the view's modulename is the expected value after runController was called |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $expected the expected moduleName |
|
102
|
|
|
* @param string $message an optional message to display if the test fails |
|
103
|
|
|
* |
|
104
|
|
|
* @return void |
|
105
|
|
|
* |
|
106
|
|
|
* @author Felix Gilcher <[email protected]> |
|
107
|
|
|
* @since 1.0.0 |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function assertViewModuleNameEquals($expected, $message = 'Failed asserting that the view\'s module name is <%1$s>.') |
|
110
|
|
|
{ |
|
111
|
|
|
$this->assertEquals($expected, $this->viewModuleName, sprintf($message, $expected)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* asserts that the DefaultView is the expected |
|
116
|
|
|
* |
|
117
|
|
|
* @param mixed $expected A string containing the view name associated with the |
|
118
|
|
|
* controller. |
|
119
|
|
|
* Or an array with the following indices: |
|
120
|
|
|
* - The parent module of the view that will be executed. |
|
121
|
|
|
* - The view that will be executed. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $message an optional message to display if the test fails |
|
124
|
|
|
* |
|
125
|
|
|
* @return void |
|
126
|
|
|
* |
|
127
|
|
|
* @see Controller::getDefaultViewName() |
|
128
|
|
|
* |
|
129
|
|
|
* @author Felix Gilcher <[email protected]> |
|
130
|
|
|
* @since 1.0.0 |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function assertDefaultView($expected, $message = 'Failed asserting that the defaultView is the expected value.') |
|
133
|
|
|
{ |
|
134
|
|
|
$controllerInstance = $this->createControllerInstance(); |
|
135
|
|
|
$this->assertEquals($expected, $controllerInstance->getDefaultViewName(), $message); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* assert that the controller handles the given request method |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $message the method name |
|
142
|
|
|
* @param boolean $acceptGeneric true if the generic 'execute' method should be accepted as handled |
|
143
|
|
|
* @param string $message an optional message to display if the test fails |
|
144
|
|
|
* |
|
145
|
|
|
* @author Felix Gilcher <[email protected]> |
|
146
|
|
|
* @since 1.0.0 |
|
147
|
|
|
*/ |
|
148
|
|
View Code Duplication |
protected function assertHandlesMethod($method, $acceptGeneric = true, $message = '') |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
$controllerInstance = $this->createControllerInstance(); |
|
151
|
|
|
$constraint = new ConstraintControllerHandlesMethod($controllerInstance, $acceptGeneric); |
|
152
|
|
|
|
|
153
|
|
|
self::assertThat($method, $constraint, $message); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* assert that the controller does not handle the given request method |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $method the method name |
|
160
|
|
|
* @param boolean $acceptGeneric true if the generic 'execute' method should be accepted as handled |
|
161
|
|
|
* @param string $message an optional message to display if the test fails |
|
162
|
|
|
* |
|
163
|
|
|
* @author Felix Gilcher <[email protected]> |
|
164
|
|
|
* @since 1.0.0 |
|
165
|
|
|
*/ |
|
166
|
|
View Code Duplication |
protected function assertNotHandlesMethod($method, $acceptGeneric = true, $message = '') |
|
|
|
|
|
|
167
|
|
|
{ |
|
168
|
|
|
$controllerInstance = $this->createControllerInstance(); |
|
169
|
|
|
$constraint = self::logicalNot(new ConstraintControllerHandlesMethod($controllerInstance, $acceptGeneric)); |
|
170
|
|
|
|
|
171
|
|
|
self::assertThat($method, $constraint, $message); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* assert that the controller is simple |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $message an optional message to display if the test fails |
|
178
|
|
|
* |
|
179
|
|
|
* @author Felix Gilcher <[email protected]> |
|
180
|
|
|
* @since 1.0.0 |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function assertIsSimple($message = 'Failed asserting that the controller is simple.') |
|
183
|
|
|
{ |
|
184
|
|
|
$controllerInstance = $this->createControllerInstance(); |
|
185
|
|
|
$this->assertTrue($controllerInstance->isSimple(), $message); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* assert that the controller is not simple |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $message an optional message to display if the test fails |
|
192
|
|
|
* |
|
193
|
|
|
* @author Felix Gilcher <[email protected]> |
|
194
|
|
|
* @since 1.0.0 |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function assertIsNotSimple($message = 'Failed asserting that the controller is not simple.') |
|
197
|
|
|
{ |
|
198
|
|
|
$controllerInstance = $this->createControllerInstance(); |
|
199
|
|
|
$this->assertFalse($controllerInstance->isSimple(), $message); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* asserts that the given argument has been touched by a validator |
|
204
|
|
|
* |
|
205
|
|
|
* This does not imply that the validation failed or succeeded, just |
|
206
|
|
|
* that a validator attempted to validate the given argument |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $argumentName the name of the argument |
|
209
|
|
|
* @param string $source the source of the argument |
|
210
|
|
|
* @param string $message an optional message |
|
211
|
|
|
* |
|
212
|
|
|
* @author Felix Gilcher <[email protected]> |
|
213
|
|
|
* @since 1.0.0 |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function assertValidatedArgument($argumentName, $source = RequestDataHolder::SOURCE_PARAMETERS, $message = 'Failed asserting that the argument <%1$s> is validated.') |
|
216
|
|
|
{ |
|
217
|
|
|
$report = $this->container->getValidationManager()->getReport(); |
|
218
|
|
|
$this->assertTrue($report->isArgumentValidated(new ValidationArgument($argumentName, $source)), sprintf($message, $argumentName)); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* asserts that the given argument has failed the validation |
|
223
|
|
|
* |
|
224
|
|
|
* @param string $argumentName the name of the argument |
|
225
|
|
|
* @param string $source the source of the argument |
|
226
|
|
|
* @param string $message an optional message |
|
227
|
|
|
* |
|
228
|
|
|
* @author Felix Gilcher <[email protected]> |
|
229
|
|
|
* @since 1.0.0 |
|
230
|
|
|
*/ |
|
231
|
|
|
protected function assertFailedArgument($argumentName, $source = RequestDataHolder::SOURCE_PARAMETERS, $message = 'Failed asserting that the argument <%1$s> is failed.') |
|
232
|
|
|
{ |
|
233
|
|
|
$report = $this->container->getValidationManager()->getReport(); |
|
234
|
|
|
$this->assertTrue($report->isArgumentFailed(new ValidationArgument($argumentName, $source)), sprintf($message, $argumentName)); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* asserts that the given argument has succeeded the validation |
|
239
|
|
|
* |
|
240
|
|
|
* @param string $argumentName the name of the argument |
|
241
|
|
|
* @param string $source the source of the argument |
|
242
|
|
|
* @param string $message an optional message |
|
243
|
|
|
* |
|
244
|
|
|
* @author Felix Gilcher <[email protected]> |
|
245
|
|
|
* @since 1.0.0 |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function assertSucceededArgument($argumentName, $source = RequestDataHolder::SOURCE_PARAMETERS, $message = 'Failed asserting that the argument <%1$s> is succeeded.') |
|
248
|
|
|
{ |
|
249
|
|
|
$report = $this->container->getValidationManager()->getReport(); |
|
250
|
|
|
$success = $report->isArgumentValidated(new ValidationArgument($argumentName, $source)) && ! $report->isArgumentFailed(new ValidationArgument($argumentName, $source)); |
|
251
|
|
|
$this->assertTrue($success, sprintf($message, $argumentName)); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
?> |
|
|
|
|
|