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