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 |
||
| 15 | class RoleTest extends RESTFunctionalTestCase |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @covers POST /user/roles |
||
| 19 | * |
||
| 20 | * BC compatible mode, will return a role |
||
| 21 | * |
||
| 22 | * @return string The created role href |
||
| 23 | */ |
||
| 24 | View Code Duplication | public function testCreateRole() |
|
| 25 | { |
||
| 26 | $xml = <<< XML |
||
| 27 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 28 | <RoleInput> |
||
| 29 | <identifier>testCreateRole</identifier> |
||
| 30 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
| 31 | <names> |
||
| 32 | <value languageCode="eng-GB">testCreateRole</value> |
||
| 33 | </names> |
||
| 34 | <descriptions> |
||
| 35 | <value languageCode="eng-GB">testCreateRole description</value> |
||
| 36 | </descriptions> |
||
| 37 | </RoleInput> |
||
| 38 | XML; |
||
| 39 | $request = $this->createHttpRequest('POST', '/api/ezp/v2/user/roles', 'RoleInput+xml', 'Role+json'); |
||
| 40 | $request->setContent($xml); |
||
| 41 | $response = $this->sendHttpRequest($request); |
||
| 42 | |||
| 43 | self::assertHttpResponseCodeEquals($response, 201); |
||
| 44 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
| 45 | |||
| 46 | $href = $response->getHeader('Location'); |
||
| 47 | $this->addCreatedElement($href); |
||
| 48 | |||
| 49 | return $href; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @covers POST /user/roles |
||
| 54 | * |
||
| 55 | * BC incompatible mode, will return a role draft |
||
| 56 | * |
||
| 57 | * @return string The created role draft href |
||
| 58 | */ |
||
| 59 | View Code Duplication | public function testCreateRoleWithDraft() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @covers GET /user/roles |
||
| 94 | */ |
||
| 95 | public function testListRoles() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @depends testCreateRole |
||
| 106 | * @covers GET /user/roles/{roleId} |
||
| 107 | */ |
||
| 108 | public function testLoadRole($roleHref) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @depends testCreateRole |
||
| 119 | * @covers POST /user/roles/{roleId} |
||
| 120 | * |
||
| 121 | * @return string The created role draft href |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function testCreateRoleDraft($roleHref) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @depends testCreateRoleDraft |
||
| 158 | * @covers GET /user/roles/{roleId}/draft |
||
| 159 | */ |
||
| 160 | public function testLoadRoleDraft($roleDraftHref) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @depends testCreateRole |
||
| 171 | * @covers GET /user/roles/{roleId}/draftByRoleId |
||
| 172 | */ |
||
| 173 | public function testLoadRoleDraftByRoleId($roleHref) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @depends testCreateRole |
||
| 184 | * @covers PATCH /user/roles/{roleId} |
||
| 185 | */ |
||
| 186 | View Code Duplication | public function testUpdateRole($roleHref) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @depends testCreateRoleDraft |
||
| 212 | * @covers PATCH /user/roles/{roleId}/draft |
||
| 213 | */ |
||
| 214 | View Code Duplication | public function testUpdateRoleDraft($roleDraftHref) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @covers POST /user/roles/{roleId}/policies |
||
| 239 | * @depends testCreateRole |
||
| 240 | * |
||
| 241 | * @return string The created policy href |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function testAddPolicy($roleHref) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @covers POST /user/roles/{roleId}/policiesByRoleDraft |
||
| 275 | * @depends testCreateRoleDraft |
||
| 276 | * |
||
| 277 | * @return string The created policy href |
||
| 278 | */ |
||
| 279 | View Code Duplication | public function testAddPolicyByRoleDraft($roleDraftHref) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @covers GET /user/roles/{roleId}/policies/{policyId} |
||
| 315 | * @depends testAddPolicy |
||
| 316 | */ |
||
| 317 | public function testLoadPolicy($policyHref) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @covers GET /user/roles/{roleId}/policies |
||
| 328 | * @depends testCreateRole |
||
| 329 | */ |
||
| 330 | public function testLoadPolicies($roleHref) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @covers PATCH /user/roles/{roleId}/policies/{policyId} |
||
| 341 | * @depends testAddPolicy |
||
| 342 | */ |
||
| 343 | View Code Duplication | public function testUpdatePolicy($policyHref) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @covers PATCH /user/roles/{roleId}/policiesByRoleDraft/{policyId} |
||
| 367 | * @depends testAddPolicyByRoleDraft |
||
| 368 | */ |
||
| 369 | View Code Duplication | public function testUpdatePolicyByRoleDraft($policyHref) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @depends testCreateRole |
||
| 398 | * @covers POST /user/users/{userId}/roles |
||
| 399 | * |
||
| 400 | * @return string assigned role href |
||
| 401 | * |
||
| 402 | * @todo stop using the anonymous user, this is dangerous... |
||
| 403 | */ |
||
| 404 | View Code Duplication | public function testAssignRoleToUser($roleHref) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @covers GET /user/users/{userId}/roles/{roleId} |
||
| 437 | * @depends testAssignRoleToUser |
||
| 438 | */ |
||
| 439 | public function testLoadRoleAssignmentForUser($roleAssignmentHref) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @covers DELETE /user/users/{userId}/roles/{roleId} |
||
| 450 | * @depends testAssignRoleToUser |
||
| 451 | */ |
||
| 452 | public function testUnassignRoleFromUser($roleAssignmentHref) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @depends testCreateRole |
||
| 463 | * @covers POST /user/groups/{groupId}/roles |
||
| 464 | * |
||
| 465 | * @return string role assignment href |
||
| 466 | */ |
||
| 467 | View Code Duplication | public function testAssignRoleToUserGroup($roleHref) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * @covers GET /user/groups/{groupId}/roles/{roleId} |
||
| 500 | * @depends testAssignRoleToUserGroup |
||
| 501 | */ |
||
| 502 | View Code Duplication | public function testLoadRoleAssignmentForUserGroup($roleAssignmentHref) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * @covers DELETE /user/groups/{groupId}/roles/{roleId} |
||
| 514 | * @depends testAssignRoleToUserGroup |
||
| 515 | */ |
||
| 516 | View Code Duplication | public function testUnassignRoleFromUserGroup($roleAssignmentHref) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * @covers GET /user/users/{userId}/roles |
||
| 528 | */ |
||
| 529 | public function testLoadRoleAssignmentsForUser() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @covers GET /user/groups/{groupPath}/roles |
||
| 540 | */ |
||
| 541 | public function testLoadRoleAssignmentsForUserGroup() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @covers GET /user/policies?userId={userId} |
||
| 552 | */ |
||
| 553 | public function testListPoliciesForUser() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @covers DELETE /user/roles/{roleId}/policies/{policyId} |
||
| 564 | * @depends testAddPolicy |
||
| 565 | */ |
||
| 566 | public function testDeletePolicy($policyHref) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @covers DELETE /user/roles/{roleId}/policiesByRoleDraft/{policyId} |
||
| 577 | * @depends testAddPolicyByRoleDraft |
||
| 578 | */ |
||
| 579 | public function testRemovePolicyByRoleDraft($policyHref) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @covers DELETE /user/roles/{roleId}/policies |
||
| 590 | * @depends testCreateRole |
||
| 591 | */ |
||
| 592 | public function testDeletePolicies($roleHref) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @covers DELETE /user/roles/{roleId} |
||
| 603 | * @depends testCreateRole |
||
| 604 | */ |
||
| 605 | public function testDeleteRole($roleHref) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @covers PUBLISH /user/roles/{roleId}/draft |
||
| 616 | * @depends testCreateRoleDraft |
||
| 617 | */ |
||
| 618 | public function testPublishRoleDraft($roleDraftHref) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @covers DELETE /user/roles/{roleId}/draft |
||
| 629 | * @depends testCreateRoleDraft |
||
| 630 | */ |
||
| 631 | public function testDeleteRoleDraft($roleDraftHref) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Helper method for changing a roledraft href to a role href. |
||
| 645 | * |
||
| 646 | * @param string $roleDraftHref Role draft href |
||
| 647 | * |
||
| 648 | * @return string Role href |
||
| 649 | */ |
||
| 650 | private function roleDraftHrefToRoleHref($roleDraftHref) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Helper method for changing a policy href to a policy by role draft href. |
||
| 657 | * |
||
| 658 | * @param string $policyHref Policy href |
||
| 659 | * |
||
| 660 | * @return string Policy by role draft href |
||
| 661 | */ |
||
| 662 | private function policyHrefToPolicyDraftHref($policyHref) |
||
| 666 | } |
||
| 667 |