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 |
||
| 23 | class UserHandlerTest extends HandlerTest |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Commented lines represent cached functions covered by specific unit tests further down. |
||
| 27 | * |
||
| 28 | * @return array |
||
| 29 | */ |
||
| 30 | public function providerForUnCachedMethods() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @dataProvider providerForUnCachedMethods |
||
| 58 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler |
||
| 59 | */ |
||
| 60 | View Code Duplication | public function testUnCachedMethods($method, array $arguments) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::create |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function testCreate() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::update |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function testUpdate() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::delete |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function testDelete() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::loadRole |
||
| 198 | */ |
||
| 199 | View Code Duplication | public function testLoadRoleIsMiss() |
|
| 200 | { |
||
| 201 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 202 | |||
| 203 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
| 204 | $this->cacheMock |
||
| 205 | ->expects($this->once()) |
||
| 206 | ->method('getItem') |
||
| 207 | ->with('user', 'role', 33) |
||
| 208 | ->will($this->returnValue($cacheItemMock)); |
||
| 209 | |||
| 210 | $cacheItemMock |
||
| 211 | ->expects($this->once()) |
||
| 212 | ->method('get') |
||
| 213 | ->will($this->returnValue(null)); |
||
| 214 | |||
| 215 | $cacheItemMock |
||
| 216 | ->expects($this->once()) |
||
| 217 | ->method('isMiss') |
||
| 218 | ->will($this->returnValue(true)); |
||
| 219 | |||
| 220 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\User\\Handler'); |
||
| 221 | $this->persistenceHandlerMock |
||
| 222 | ->expects($this->once()) |
||
| 223 | ->method('userHandler') |
||
| 224 | ->will($this->returnValue($innerHandlerMock)); |
||
| 225 | |||
| 226 | $innerHandlerMock |
||
| 227 | ->expects($this->once()) |
||
| 228 | ->method('loadRole') |
||
| 229 | ->with(33) |
||
| 230 | ->will( |
||
| 231 | $this->returnValue(new Role()) |
||
| 232 | ); |
||
| 233 | |||
| 234 | $cacheItemMock |
||
| 235 | ->expects($this->once()) |
||
| 236 | ->method('set') |
||
| 237 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\User\\Role')) |
||
| 238 | ->will($this->returnValue($cacheItemMock)); |
||
| 239 | |||
| 240 | $cacheItemMock |
||
| 241 | ->expects($this->once()) |
||
| 242 | ->method('save') |
||
| 243 | ->with(); |
||
| 244 | |||
| 245 | $handler = $this->persistenceCacheHandler->userHandler(); |
||
| 246 | $handler->loadRole(33); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::loadRole |
||
| 251 | */ |
||
| 252 | View Code Duplication | public function testLoadRoleHasCache() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::loadRoleAssignmentsByGroupId |
||
| 287 | */ |
||
| 288 | View Code Duplication | public function testLoadRoleAssignmentsByGroupIdIsMiss() |
|
| 289 | { |
||
| 290 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 291 | |||
| 292 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
| 293 | $this->cacheMock |
||
| 294 | ->expects($this->once()) |
||
| 295 | ->method('getItem') |
||
| 296 | ->with('user', 'role', 'assignments', 'byGroup', 42) |
||
| 297 | ->will($this->returnValue($cacheItemMock)); |
||
| 298 | |||
| 299 | $cacheItemMock |
||
| 300 | ->expects($this->once()) |
||
| 301 | ->method('get') |
||
| 302 | ->will($this->returnValue(null)); |
||
| 303 | |||
| 304 | $cacheItemMock |
||
| 305 | ->expects($this->once()) |
||
| 306 | ->method('isMiss') |
||
| 307 | ->will($this->returnValue(true)); |
||
| 308 | |||
| 309 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\User\\Handler'); |
||
| 310 | $this->persistenceHandlerMock |
||
| 311 | ->expects($this->once()) |
||
| 312 | ->method('userHandler') |
||
| 313 | ->will($this->returnValue($innerHandlerMock)); |
||
| 314 | |||
| 315 | $innerHandlerMock |
||
| 316 | ->expects($this->once()) |
||
| 317 | ->method('loadRoleAssignmentsByGroupId') |
||
| 318 | ->with(42, false) |
||
| 319 | ->will( |
||
| 320 | $this->returnValue( |
||
| 321 | array( |
||
| 322 | new RoleAssignment(array('roleId' => 33)), |
||
| 323 | ) |
||
| 324 | ) |
||
| 325 | ); |
||
| 326 | |||
| 327 | $cacheItemMock |
||
| 328 | ->expects($this->once()) |
||
| 329 | ->method('set') |
||
| 330 | ->with($this->isType('array')) |
||
| 331 | ->will($this->returnValue($cacheItemMock)); |
||
| 332 | |||
| 333 | $cacheItemMock |
||
| 334 | ->expects($this->once()) |
||
| 335 | ->method('save') |
||
| 336 | ->with(); |
||
| 337 | |||
| 338 | $handler = $this->persistenceCacheHandler->userHandler(); |
||
| 339 | $roleAssignments = $handler->loadRoleAssignmentsByGroupId(42); |
||
| 340 | |||
| 341 | $this->assertEquals(1, count($roleAssignments)); |
||
| 342 | $this->assertInstanceOf('\\eZ\\Publish\\SPI\\Persistence\\User\\RoleAssignment', $roleAssignments[0]); |
||
| 343 | $this->assertEquals(33, $roleAssignments[0]->roleId); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::loadRoleAssignmentsByGroupId |
||
| 348 | */ |
||
| 349 | View Code Duplication | public function testLoadRoleAssignmentsByGroupIdHasCache() |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::loadRoleAssignmentsByGroupId |
||
| 390 | */ |
||
| 391 | View Code Duplication | public function testLoadRoleAssignmentsByGroupIdInheritedIsMiss() |
|
| 392 | { |
||
| 393 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 394 | |||
| 395 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
| 396 | $this->cacheMock |
||
| 397 | ->expects($this->once()) |
||
| 398 | ->method('getItem') |
||
| 399 | ->with('user', 'role', 'assignments', 'byGroup', 'inherited', '42') |
||
| 400 | ->will($this->returnValue($cacheItemMock)); |
||
| 401 | |||
| 402 | $cacheItemMock |
||
| 403 | ->expects($this->once()) |
||
| 404 | ->method('get') |
||
| 405 | ->will($this->returnValue(null)); |
||
| 406 | |||
| 407 | $cacheItemMock |
||
| 408 | ->expects($this->once()) |
||
| 409 | ->method('isMiss') |
||
| 410 | ->will($this->returnValue(true)); |
||
| 411 | |||
| 412 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\User\\Handler'); |
||
| 413 | $this->persistenceHandlerMock |
||
| 414 | ->expects($this->once()) |
||
| 415 | ->method('userHandler') |
||
| 416 | ->will($this->returnValue($innerHandlerMock)); |
||
| 417 | |||
| 418 | $innerHandlerMock |
||
| 419 | ->expects($this->once()) |
||
| 420 | ->method('loadRoleAssignmentsByGroupId') |
||
| 421 | ->with(42, true) |
||
| 422 | ->will( |
||
| 423 | $this->returnValue(array(new RoleAssignment(array('roleId' => 33)))) |
||
| 424 | ); |
||
| 425 | |||
| 426 | $cacheItemMock |
||
| 427 | ->expects($this->once()) |
||
| 428 | ->method('set') |
||
| 429 | ->with($this->isType('array')) |
||
| 430 | ->will($this->returnValue($cacheItemMock)); |
||
| 431 | |||
| 432 | $cacheItemMock |
||
| 433 | ->expects($this->once()) |
||
| 434 | ->method('save') |
||
| 435 | ->with(); |
||
| 436 | |||
| 437 | $handler = $this->persistenceCacheHandler->userHandler(); |
||
| 438 | $roleAssignments = $handler->loadRoleAssignmentsByGroupId(42, true); |
||
| 439 | |||
| 440 | $this->assertEquals(1, count($roleAssignments)); |
||
| 441 | $this->assertInstanceOf('\\eZ\\Publish\\SPI\\Persistence\\User\\RoleAssignment', $roleAssignments[0]); |
||
| 442 | $this->assertEquals(33, $roleAssignments[0]->roleId); |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::loadRoleAssignmentsByGroupId |
||
| 447 | */ |
||
| 448 | View Code Duplication | public function testLoadRoleAssignmentsByGroupIdInheritedHasCache() |
|
| 486 | |||
| 487 | /** |
||
| 488 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::createRole |
||
| 489 | */ |
||
| 490 | View Code Duplication | public function testCreateRole() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::createRoleDraft |
||
| 531 | */ |
||
| 532 | public function testCreateRoleDraft() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::updateRole |
||
| 574 | */ |
||
| 575 | View Code Duplication | public function testUpdateRole() |
|
| 606 | |||
| 607 | /** |
||
| 608 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::updateRole |
||
| 609 | */ |
||
| 610 | View Code Duplication | public function testUpdateRoleDraft() |
|
| 641 | |||
| 642 | /** |
||
| 643 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::deleteRole |
||
| 644 | */ |
||
| 645 | View Code Duplication | public function testDeleteRole() |
|
| 678 | |||
| 679 | /** |
||
| 680 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::deleteRole |
||
| 681 | */ |
||
| 682 | public function testDeleteRoleDraft() |
||
| 707 | |||
| 708 | public function testPublishRoleDraftFromExistingRole() |
||
| 709 | { |
||
| 710 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 711 | |||
| 712 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\User\\Handler'); |
||
| 713 | $this->persistenceHandlerMock |
||
| 714 | ->expects($this->once()) |
||
| 715 | ->method('userHandler') |
||
| 716 | ->willReturn($innerHandlerMock); |
||
| 717 | |||
| 718 | $roleDraftId = 33; |
||
| 719 | $originalRoleId = 30; |
||
| 720 | $innerHandlerMock |
||
| 721 | ->expects($this->exactly(2)) |
||
| 722 | ->method('loadRole') |
||
| 723 | ->willReturnMap([ |
||
| 724 | [$roleDraftId, Role::STATUS_DRAFT, new Role(['originalId' => $originalRoleId])], |
||
| 725 | [$originalRoleId, Role::STATUS_DEFINED, new Role(['id' => $originalRoleId])], |
||
| 726 | ]); |
||
| 727 | $innerHandlerMock |
||
| 728 | ->expects($this->once()) |
||
| 729 | ->method('publishRoleDraft') |
||
| 730 | ->with($roleDraftId); |
||
| 731 | |||
| 732 | $this->cacheMock |
||
| 733 | ->expects($this->once()) |
||
| 734 | ->method('clear') |
||
| 735 | ->with('user', 'role', 'assignments') |
||
| 736 | ->will($this->returnValue(true)); |
||
| 737 | |||
| 738 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
| 739 | $this->cacheMock |
||
| 740 | ->expects($this->once()) |
||
| 741 | ->method('getItem') |
||
| 742 | ->with('user', 'role', $originalRoleId) |
||
| 743 | ->will($this->returnValue($cacheItemMock)); |
||
| 744 | |||
| 745 | $cacheItemMock |
||
| 746 | ->expects($this->once()) |
||
| 747 | ->method('set') |
||
| 748 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\User\\Role')) |
||
| 749 | ->will($this->returnValue($cacheItemMock)); |
||
| 750 | |||
| 751 | $cacheItemMock |
||
| 752 | ->expects($this->once()) |
||
| 753 | ->method('save') |
||
| 754 | ->with(); |
||
| 755 | |||
| 756 | $handler = $this->persistenceCacheHandler->userHandler(); |
||
| 757 | $handler->publishRoleDraft($roleDraftId); |
||
| 758 | } |
||
| 759 | |||
| 760 | public function testPublishNewRoleDraft() |
||
| 761 | { |
||
| 762 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 763 | |||
| 764 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\User\\Handler'); |
||
| 765 | $this->persistenceHandlerMock |
||
| 766 | ->expects($this->once()) |
||
| 767 | ->method('userHandler') |
||
| 768 | ->willReturn($innerHandlerMock); |
||
| 769 | |||
| 770 | $roleDraftId = 33; |
||
| 771 | $originalRoleId = -1; |
||
| 772 | $innerHandlerMock |
||
| 773 | ->expects($this->at(0)) |
||
| 774 | ->method('loadRole') |
||
| 775 | ->with($roleDraftId, Role::STATUS_DRAFT) |
||
| 776 | ->willReturn(new Role(['originalId' => $originalRoleId])); |
||
| 777 | $innerHandlerMock |
||
| 778 | ->expects($this->at(1)) |
||
| 779 | ->method('publishRoleDraft') |
||
| 780 | ->with($roleDraftId); |
||
| 781 | $innerHandlerMock |
||
| 782 | ->expects($this->at(2)) |
||
| 783 | ->method('loadRole') |
||
| 784 | ->with($originalRoleId, Role::STATUS_DEFINED) |
||
| 785 | ->willThrowException(new NotFoundException('foo', 'bar')); |
||
| 786 | $innerHandlerMock |
||
| 787 | ->expects($this->at(3)) |
||
| 788 | ->method('loadRole') |
||
| 789 | ->with($roleDraftId, Role::STATUS_DEFINED) |
||
| 790 | ->willReturn(new Role(['id' => $roleDraftId])); |
||
| 791 | |||
| 792 | $this->cacheMock |
||
| 793 | ->expects($this->once()) |
||
| 794 | ->method('clear') |
||
| 795 | ->with('user', 'role', 'assignments') |
||
| 796 | ->will($this->returnValue(true)); |
||
| 797 | |||
| 798 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
| 799 | $this->cacheMock |
||
| 800 | ->expects($this->once()) |
||
| 801 | ->method('getItem') |
||
| 802 | ->with('user', 'role', $roleDraftId) |
||
| 803 | ->will($this->returnValue($cacheItemMock)); |
||
| 804 | |||
| 805 | $cacheItemMock |
||
| 806 | ->expects($this->once()) |
||
| 807 | ->method('set') |
||
| 808 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\User\\Role')) |
||
| 809 | ->will($this->returnValue($cacheItemMock)); |
||
| 810 | |||
| 811 | $cacheItemMock |
||
| 812 | ->expects($this->once()) |
||
| 813 | ->method('save') |
||
| 814 | ->with(); |
||
| 815 | |||
| 816 | $handler = $this->persistenceCacheHandler->userHandler(); |
||
| 817 | $handler->publishRoleDraft($roleDraftId); |
||
| 818 | } |
||
| 819 | |||
| 820 | /** |
||
| 821 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::addPolicy |
||
| 822 | */ |
||
| 823 | View Code Duplication | public function testAddPolicy() |
|
| 850 | |||
| 851 | /** |
||
| 852 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::addPolicyByRoleDraft |
||
| 853 | */ |
||
| 854 | public function testAddPolicyByRoleDraft() |
||
| 879 | |||
| 880 | /** |
||
| 881 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::updatePolicy |
||
| 882 | */ |
||
| 883 | View Code Duplication | public function testUpdatePolicy() |
|
| 910 | |||
| 911 | /** |
||
| 912 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::deletePolicy |
||
| 913 | */ |
||
| 914 | public function testDeletePolicy() |
||
| 941 | |||
| 942 | /** |
||
| 943 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::assignRole |
||
| 944 | */ |
||
| 945 | View Code Duplication | public function testAssignRole() |
|
| 984 | |||
| 985 | /** |
||
| 986 | * @covers eZ\Publish\Core\Persistence\Cache\UserHandler::unassignRole |
||
| 987 | */ |
||
| 988 | View Code Duplication | public function testUnassignRole() |
|
| 1027 | } |
||
| 1028 |