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 |
||
| 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() |
||
| 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() |
||
| 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>.') |
||
| 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>.') |
||
| 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.') |
||
| 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 = '') |
|
| 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 = '') |
|
| 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.') |
||
| 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.') |
||
| 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.') |
||
| 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.') |
||
| 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.') |
||
| 253 | |||
| 254 | } |
||
| 255 | |||
| 256 | ?> |
||