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 |
||
| 25 | class ViewControllerTest extends \PHPUnit_Framework_TestCase { |
||
| 26 | |||
| 27 | private $appName; |
||
| 28 | private $request; |
||
| 29 | private $config; |
||
| 30 | private $userSession; |
||
| 31 | private $urlGenerator; |
||
| 32 | |||
| 33 | private $dummyUser; |
||
| 34 | |||
| 35 | private $controller; |
||
| 36 | |||
| 37 | public function setUp() { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @dataProvider indexDataProvider |
||
| 63 | */ |
||
| 64 | public function testIndex($serverVersion, $isIE, $shareeActions, $shareeCanEdit) { |
||
| 65 | $this->request->expects($this->once()) |
||
| 66 | ->method('isUserAgent') |
||
| 67 | ->with(['/(MSIE)|(Trident)/']) |
||
| 68 | ->will($this->returnValue($isIE)); |
||
| 69 | |||
| 70 | $this->config->expects($this->at(0)) |
||
| 71 | ->method('getSystemValue') |
||
| 72 | ->with('version') |
||
| 73 | ->will($this->returnValue($serverVersion)); |
||
| 74 | |||
| 75 | $this->config->expects($this->at(1)) |
||
| 76 | ->method('getAppValue') |
||
| 77 | ->with($this->appName, 'installed_version') |
||
| 78 | ->will($this->returnValue('42.13.37')); |
||
| 79 | |||
| 80 | $this->config->expects($this->at(2)) |
||
| 81 | ->method('getAppValue') |
||
| 82 | ->with('theming', 'color', '#0082C9') |
||
| 83 | ->will($this->returnValue('#ff00ff')); |
||
| 84 | |||
| 85 | $this->config->expects($this->at(3)) |
||
| 86 | ->method('getAppValue') |
||
| 87 | ->with('core', 'shareapi_allow_links', 'yes') |
||
| 88 | ->will($this->returnValue('yes')); |
||
| 89 | |||
| 90 | $this->userSession->expects($this->once()) |
||
| 91 | ->method('getUser') |
||
| 92 | ->will($this->returnValue($this->dummyUser)); |
||
| 93 | |||
| 94 | $this->dummyUser->expects($this->once()) |
||
| 95 | ->method('getUID') |
||
| 96 | ->will($this->returnValue('user123')); |
||
| 97 | |||
| 98 | $this->dummyUser->expects($this->once()) |
||
| 99 | ->method('getEMailAddress') |
||
| 100 | ->will($this->returnValue('[email protected]')); |
||
| 101 | |||
| 102 | $this->config->expects($this->at(4)) |
||
| 103 | ->method('getUserValue') |
||
| 104 | ->with('user123', $this->appName, 'currentView', null) |
||
| 105 | ->will($this->returnValue('someView')); |
||
| 106 | |||
| 107 | $this->config->expects($this->at(5)) |
||
| 108 | ->method('getUserValue') |
||
| 109 | ->with('user123', $this->appName, 'skipPopover', 'no') |
||
| 110 | ->will($this->returnValue('someSkipPopoverValue')); |
||
| 111 | |||
| 112 | $this->config->expects($this->at(6)) |
||
| 113 | ->method('getUserValue') |
||
| 114 | ->with('user123', $this->appName, 'showWeekNr', 'no') |
||
| 115 | ->will($this->returnValue('someShowWeekNrValue')); |
||
| 116 | |||
| 117 | $this->config->expects($this->at(7)) |
||
| 118 | ->method('getUserValue') |
||
| 119 | ->with('user123', $this->appName, 'firstRun', null) |
||
| 120 | ->will($this->returnValue('someFirstRunValue')); |
||
| 121 | |||
| 122 | $actual = $this->controller->index(); |
||
| 123 | |||
| 124 | $this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual); |
||
| 125 | $this->assertEquals([ |
||
| 126 | 'appVersion' => '42.13.37', |
||
| 127 | 'initialView' => 'someView', |
||
| 128 | 'emailAddress' => '[email protected]', |
||
| 129 | 'skipPopover' => 'someSkipPopoverValue', |
||
| 130 | 'weekNumbers' => 'someShowWeekNrValue', |
||
| 131 | 'firstRun' => 'someFirstRunValue', |
||
| 132 | 'canSharePublicLink' => 'yes', |
||
| 133 | 'defaultColor' => '#ff00ff', |
||
| 134 | 'isPublic' => false, |
||
| 135 | 'isEmbedded' => false, |
||
| 136 | 'isIE' => $isIE, |
||
| 137 | 'token' => '', |
||
| 138 | 'shareeCanEditShares' => $shareeActions, |
||
| 139 | 'shareeCanEditCalendarProperties' => $shareeCanEdit, |
||
| 140 | ], $actual->getParams()); |
||
| 141 | $this->assertEquals('main', $actual->getTemplateName()); |
||
| 142 | } |
||
| 143 | |||
| 144 | View Code Duplication | public function indexDataProvider() { |
|
| 152 | |||
| 153 | public function testIndexNoMonthFallback() { |
||
| 154 | $this->request->expects($this->once()) |
||
| 155 | ->method('isUserAgent') |
||
| 156 | ->with(['/(MSIE)|(Trident)/']) |
||
| 157 | ->will($this->returnValue(false)); |
||
| 158 | |||
| 159 | $this->config->expects($this->at(0)) |
||
| 160 | ->method('getSystemValue') |
||
| 161 | ->with('version') |
||
| 162 | ->will($this->returnValue('12.0.0')); |
||
| 163 | |||
| 164 | $this->userSession->expects($this->once()) |
||
| 165 | ->method('getUser') |
||
| 166 | ->will($this->returnValue($this->dummyUser)); |
||
| 167 | |||
| 168 | $this->dummyUser->expects($this->once()) |
||
| 169 | ->method('getUID') |
||
| 170 | ->will($this->returnValue('user123')); |
||
| 171 | |||
| 172 | $this->dummyUser->expects($this->once()) |
||
| 173 | ->method('getEMailAddress') |
||
| 174 | ->will($this->returnValue('[email protected]')); |
||
| 175 | |||
| 176 | $this->config->expects($this->at(1)) |
||
| 177 | ->method('getAppValue') |
||
| 178 | ->with($this->appName, 'installed_version') |
||
| 179 | ->will($this->returnValue('42.13.37')); |
||
| 180 | |||
| 181 | $this->config->expects($this->at(2)) |
||
| 182 | ->method('getAppValue') |
||
| 183 | ->with('theming', 'color', '#0082C9') |
||
| 184 | ->will($this->returnValue('#ff00ff')); |
||
| 185 | |||
| 186 | $this->config->expects($this->at(3)) |
||
| 187 | ->method('getAppValue') |
||
| 188 | ->with('core', 'shareapi_allow_links', 'yes') |
||
| 189 | ->will($this->returnValue('no')); |
||
| 190 | |||
| 191 | $this->config->expects($this->at(4)) |
||
| 192 | ->method('getUserValue') |
||
| 193 | ->with('user123', $this->appName, 'currentView', null) |
||
| 194 | ->will($this->returnValue(null)); |
||
| 195 | |||
| 196 | $this->config->expects($this->at(5)) |
||
| 197 | ->method('getUserValue') |
||
| 198 | ->with('user123', $this->appName, 'skipPopover', 'no') |
||
| 199 | ->will($this->returnValue('someSkipPopoverValue')); |
||
| 200 | |||
| 201 | $this->config->expects($this->at(6)) |
||
| 202 | ->method('getUserValue') |
||
| 203 | ->with('user123', $this->appName, 'showWeekNr', 'no') |
||
| 204 | ->will($this->returnValue('someShowWeekNrValue')); |
||
| 205 | |||
| 206 | $this->config->expects($this->at(7)) |
||
| 207 | ->method('getUserValue') |
||
| 208 | ->with('user123', $this->appName, 'firstRun', null) |
||
| 209 | ->will($this->returnValue('someFirstRunValue')); |
||
| 210 | |||
| 211 | $actual = $this->controller->index(); |
||
| 212 | |||
| 213 | $this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual); |
||
| 214 | $this->assertEquals([ |
||
| 215 | 'appVersion' => '42.13.37', |
||
| 216 | 'initialView' => 'month', |
||
| 217 | 'emailAddress' => '[email protected]', |
||
| 218 | 'skipPopover' => 'someSkipPopoverValue', |
||
| 219 | 'weekNumbers' => 'someShowWeekNrValue', |
||
| 220 | 'firstRun' => 'someFirstRunValue', |
||
| 221 | 'defaultColor' => '#ff00ff', |
||
| 222 | 'isPublic' => false, |
||
| 223 | 'isEmbedded' => false, |
||
| 224 | 'isIE' => false, |
||
| 225 | 'token' => '', |
||
| 226 | 'shareeCanEditShares' => 'no', |
||
| 227 | 'shareeCanEditCalendarProperties' => 'yes', |
||
| 228 | 'canSharePublicLink' => 'no' |
||
| 229 | ], $actual->getParams()); |
||
| 230 | $this->assertEquals('main', $actual->getTemplateName()); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @dataProvider indexFirstRunDetectionProvider |
||
| 235 | */ |
||
| 236 | public function testIndexFirstRunDetection($initialView, $expectedFirstRun, $expectsSetRequest) { |
||
| 237 | $this->request->expects($this->once()) |
||
| 238 | ->method('isUserAgent') |
||
| 239 | ->with(['/(MSIE)|(Trident)/']) |
||
| 240 | ->will($this->returnValue(false)); |
||
| 241 | |||
| 242 | $this->config->expects($this->at(0)) |
||
| 243 | ->method('getSystemValue') |
||
| 244 | ->with('version') |
||
| 245 | ->will($this->returnValue('9.1.0.0')); |
||
| 246 | |||
| 247 | $this->userSession->expects($this->once()) |
||
| 248 | ->method('getUser') |
||
| 249 | ->will($this->returnValue($this->dummyUser)); |
||
| 250 | |||
| 251 | $this->dummyUser->expects($this->once()) |
||
| 252 | ->method('getUID') |
||
| 253 | ->will($this->returnValue('user123')); |
||
| 254 | |||
| 255 | $this->dummyUser->expects($this->once()) |
||
| 256 | ->method('getEMailAddress') |
||
| 257 | ->will($this->returnValue('[email protected]')); |
||
| 258 | |||
| 259 | $this->config->expects($this->at(1)) |
||
| 260 | ->method('getAppValue') |
||
| 261 | ->with($this->appName, 'installed_version') |
||
| 262 | ->will($this->returnValue('42.13.37')); |
||
| 263 | |||
| 264 | $this->config->expects($this->at(2)) |
||
| 265 | ->method('getAppValue') |
||
| 266 | ->with('theming', 'color', '#0082C9') |
||
| 267 | ->will($this->returnValue('#ff00ff')); |
||
| 268 | |||
| 269 | $this->config->expects($this->at(3)) |
||
| 270 | ->method('getAppValue') |
||
| 271 | ->with('core', 'shareapi_allow_links', 'yes') |
||
| 272 | ->will($this->returnValue('no')); |
||
| 273 | |||
| 274 | $this->config->expects($this->at(4)) |
||
| 275 | ->method('getUserValue') |
||
| 276 | ->with('user123', $this->appName, 'currentView', null) |
||
| 277 | ->will($this->returnValue($initialView)); |
||
| 278 | |||
| 279 | $this->config->expects($this->at(5)) |
||
| 280 | ->method('getUserValue') |
||
| 281 | ->with('user123', $this->appName, 'skipPopover', 'no') |
||
| 282 | ->will($this->returnValue('someSkipPopoverValue')); |
||
| 283 | |||
| 284 | $this->config->expects($this->at(6)) |
||
| 285 | ->method('getUserValue') |
||
| 286 | ->with('user123', $this->appName, 'showWeekNr', 'no') |
||
| 287 | ->will($this->returnValue('someShowWeekNrValue')); |
||
| 288 | |||
| 289 | $this->config->expects($this->at(7)) |
||
| 290 | ->method('getUserValue') |
||
| 291 | ->with('user123', $this->appName, 'firstRun', null) |
||
| 292 | ->will($this->returnValue(null)); |
||
| 293 | |||
| 294 | if ($expectsSetRequest) { |
||
| 295 | $this->config->expects($this->at(8)) |
||
| 296 | ->method('setUserValue') |
||
| 297 | ->with('user123'); |
||
| 298 | } |
||
| 299 | |||
| 300 | $actual = $this->controller->index(); |
||
| 301 | |||
| 302 | $this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual); |
||
| 303 | $this->assertEquals([ |
||
| 304 | 'appVersion' => '42.13.37', |
||
| 305 | 'initialView' => $initialView ? 'someRandominitialView' : 'month', |
||
| 306 | 'emailAddress' => '[email protected]', |
||
| 307 | 'skipPopover' => 'someSkipPopoverValue', |
||
| 308 | 'weekNumbers' => 'someShowWeekNrValue', |
||
| 309 | 'firstRun' => $expectedFirstRun, |
||
| 310 | 'defaultColor' => '#ff00ff', |
||
| 311 | 'isPublic' => false, |
||
| 312 | 'isEmbedded' => false, |
||
| 313 | 'isIE' => false, |
||
| 314 | 'token' => '', |
||
| 315 | 'shareeCanEditShares' => 'yes', |
||
| 316 | 'shareeCanEditCalendarProperties' => 'no', |
||
| 317 | 'canSharePublicLink' => 'no' |
||
| 318 | ], $actual->getParams()); |
||
| 319 | $this->assertEquals('main', $actual->getTemplateName()); |
||
| 320 | } |
||
| 321 | |||
| 322 | public function indexFirstRunDetectionProvider() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @dataProvider indexPublicDataProvider |
||
| 331 | */ |
||
| 332 | View Code Duplication | public function testPublicIndex($serverVersion, $isIE, $shareeActions, $shareeCanEdit) { |
|
| 333 | $this->request->expects($this->once()) |
||
| 334 | ->method('isUserAgent') |
||
| 335 | ->with(['/(MSIE)|(Trident)/']) |
||
| 336 | ->will($this->returnValue($isIE)); |
||
| 337 | |||
| 338 | $this->config->expects($this->at(0)) |
||
| 339 | ->method('getSystemValue') |
||
| 340 | ->with('version') |
||
| 341 | ->will($this->returnValue($serverVersion)); |
||
| 342 | |||
| 343 | $this->config->expects($this->at(1)) |
||
| 344 | ->method('getAppValue') |
||
| 345 | ->with($this->appName, 'installed_version') |
||
| 346 | ->will($this->returnValue('42.13.37')); |
||
| 347 | |||
| 348 | $this->config->expects($this->at(2)) |
||
| 349 | ->method('getAppValue') |
||
| 350 | ->with('theming', 'color', '#0082C9') |
||
| 351 | ->will($this->returnValue('#ff00ff')); |
||
| 352 | |||
| 353 | $this->config->expects($this->at(3)) |
||
| 354 | ->method('getAppValue') |
||
| 355 | ->with('core', 'shareapi_allow_links', 'yes') |
||
| 356 | ->will($this->returnValue('no')); |
||
| 357 | |||
| 358 | $this->request->expects($this->at(1)) |
||
| 359 | ->method('getServerProtocol') |
||
| 360 | ->will($this->returnValue('fancy_protocol')); |
||
| 361 | |||
| 362 | $this->request->expects($this->at(2)) |
||
| 363 | ->method('getServerHost') |
||
| 364 | ->will($this->returnValue('nextcloud-host.tld')); |
||
| 365 | |||
| 366 | $this->request->expects($this->at(3)) |
||
| 367 | ->method('getRequestUri') |
||
| 368 | ->will($this->returnValue('/request/uri/123/42')); |
||
| 369 | |||
| 370 | $this->urlGenerator->expects($this->at(0)) |
||
| 371 | ->method('imagePath') |
||
| 372 | ->with('core', 'favicon-touch.png') |
||
| 373 | ->will($this->returnValue('/core/img/foo')); |
||
| 374 | |||
| 375 | $this->urlGenerator->expects($this->at(1)) |
||
| 376 | ->method('getAbsoluteURL') |
||
| 377 | ->with('/core/img/foo') |
||
| 378 | ->will($this->returnValue('fancy_protocol://foo.bar/core/img/foo')); |
||
| 379 | |||
| 380 | $this->urlGenerator->expects($this->at(2)) |
||
| 381 | ->method('linkTo') |
||
| 382 | ->with('', 'remote.php') |
||
| 383 | ->will($this->returnValue('remote.php')); |
||
| 384 | |||
| 385 | $this->urlGenerator->expects($this->at(3)) |
||
| 386 | ->method('getAbsoluteURL') |
||
| 387 | ->with('remote.php/dav/public-calendars/fancy_token_123?export') |
||
| 388 | ->will($this->returnValue('fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export')); |
||
| 389 | |||
| 390 | $this->request->expects($this->at(4)) |
||
| 391 | ->method('getServerProtocol') |
||
| 392 | ->will($this->returnValue('fancy_protocol')); |
||
| 393 | |||
| 394 | $actual = $this->controller->publicIndexForEmbedding('fancy_token_123'); |
||
| 395 | |||
| 396 | $this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual); |
||
| 397 | $this->assertEquals([ |
||
| 398 | 'appVersion' => '42.13.37', |
||
| 399 | 'initialView' => 'month', |
||
| 400 | 'emailAddress' => '', |
||
| 401 | 'skipPopover' => 'no', |
||
| 402 | 'weekNumbers' => 'no', |
||
| 403 | 'isPublic' => true, |
||
| 404 | 'isEmbedded' => true, |
||
| 405 | 'shareURL' => 'fancy_protocol://nextcloud-host.tld/request/uri/123/42', |
||
| 406 | 'previewImage' => 'fancy_protocol://foo.bar/core/img/foo', |
||
| 407 | 'firstRun' => 'no', |
||
| 408 | 'isIE' => $isIE, |
||
| 409 | 'defaultColor' => '#ff00ff', |
||
| 410 | 'webcalURL' => 'webcal://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export', |
||
| 411 | 'downloadURL' => 'fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export', |
||
| 412 | 'token' => 'fancy_token_123', |
||
| 413 | 'shareeCanEditShares' => $shareeActions, |
||
| 414 | 'shareeCanEditCalendarProperties' => $shareeCanEdit, |
||
| 415 | 'canSharePublicLink' => 'no', |
||
| 416 | ], $actual->getParams()); |
||
| 417 | $this->assertEquals('main', $actual->getTemplateName()); |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @dataProvider indexPublicDataProvider |
||
| 422 | */ |
||
| 423 | View Code Duplication | public function testPublicIndexWithBranding($serverVersion, $isIE, $shareeActions, $shareeCanEdit) { |
|
| 510 | |||
| 511 | View Code Duplication | public function indexPublicDataProvider() { |
|
| 519 | } |
||
| 520 |
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.