Completed
Pull Request — master (#7)
by Markus
07:32
created

ControllerTestCase   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 220
Duplicated Lines 6.36 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 14
loc 220
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 7

12 Methods

Rating   Name   Duplication   Size   Complexity  
A runController() 0 7 1
A performValidation() 0 5 1
A assertViewNameEquals() 0 5 1
A assertViewModuleNameEquals() 0 4 1
A assertDefaultView() 0 5 1
A assertHandlesMethod() 7 7 1
A assertNotHandlesMethod() 7 7 1
A assertIsSimple() 0 5 1
A assertIsNotSimple() 0 5 1
A assertValidatedArgument() 0 5 1
A assertFailedArgument() 0 5 1
A assertSucceededArgument() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
{	
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
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());
0 ignored issues
show
Bug introduced by
The method setControllerInstance() does not seem to exist on object<Agavi\Dispatcher\ExecutionContainer>.

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.

Loading history...
62
		//$executionFilter = $this->createExecutionFilter();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
		$this->container->initRequestData();
0 ignored issues
show
Bug introduced by
The method initRequestData() cannot be called from this context as it is declared protected in class Agavi\Dispatcher\ExecutionContainer.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method setControllerInstance() does not seem to exist on object<Agavi\Dispatcher\ExecutionContainer>.

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.

Loading history...
78
		$this->validationSuccess = $this->container->performValidation($this->container);
0 ignored issues
show
Unused Code introduced by
The call to ExecutionContainer::performValidation() has too many arguments starting with $this->container.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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 = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...