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 |
||
| 14 | class ContentTest extends RESTFunctionalTestCase |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Covers POST /content/objects. |
||
| 18 | * |
||
| 19 | * @return string REST content ID |
||
| 20 | */ |
||
| 21 | public function testCreateContent() |
||
| 22 | { |
||
| 23 | $request = $this->createHttpRequest('POST', '/api/ezp/v2/content/objects', 'ContentCreate+xml', 'ContentInfo+json'); |
||
| 24 | $string = $this->addTestSuffix(__FUNCTION__); |
||
| 25 | $body = <<< XML |
||
| 26 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 27 | <ContentCreate> |
||
| 28 | <ContentType href="/api/ezp/v2/content/types/1" /> |
||
| 29 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
| 30 | <LocationCreate> |
||
| 31 | <ParentLocation href="/api/ezp/v2/content/locations/1/2" /> |
||
| 32 | <priority>0</priority> |
||
| 33 | <hidden>false</hidden> |
||
| 34 | <sortField>PATH</sortField> |
||
| 35 | <sortOrder>ASC</sortOrder> |
||
| 36 | </LocationCreate> |
||
| 37 | <Section href="/api/ezp/v2/content/sections/1" /> |
||
| 38 | <alwaysAvailable>true</alwaysAvailable> |
||
| 39 | <remoteId>{$string}</remoteId> |
||
| 40 | <User href="/api/ezp/v2/user/users/14" /> |
||
| 41 | <modificationDate>2012-09-30T12:30:00</modificationDate> |
||
| 42 | <fields> |
||
| 43 | <field> |
||
| 44 | <fieldDefinitionIdentifier>name</fieldDefinitionIdentifier> |
||
| 45 | <languageCode>eng-GB</languageCode> |
||
| 46 | <fieldValue>{$string}</fieldValue> |
||
| 47 | </field> |
||
| 48 | </fields> |
||
| 49 | </ContentCreate> |
||
| 50 | XML; |
||
| 51 | $request->setContent($body); |
||
| 52 | |||
| 53 | $response = $this->sendHttpRequest($request); |
||
| 54 | |||
| 55 | self::assertHttpResponseCodeEquals($response, 201); |
||
| 56 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
| 57 | |||
| 58 | $href = $response->getHeader('Location'); |
||
| 59 | $this->addCreatedElement($href); |
||
| 60 | |||
| 61 | return $href; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @depends testCreateContent |
||
| 66 | * Covers PUBLISH /content/objects/<contentId>/versions/<versionNumber> |
||
| 67 | * |
||
| 68 | * @return string REST content ID |
||
| 69 | */ |
||
| 70 | public function testPublishContent($restContentHref) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @depends testPublishContent |
||
| 82 | * Covers GET /content/objects?remoteId=<remoteId> |
||
| 83 | */ |
||
| 84 | public function testRedirectContent($restContentHref) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @depends testPublishContent |
||
| 96 | */ |
||
| 97 | public function testLoadContent($restContentHref) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @depends testPublishContent |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function testUpdateContentMetadata($restContentHref) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @depends testPublishContent |
||
| 130 | * |
||
| 131 | * @return string ContentVersion REST ID |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function testCreateDraftFromVersion($restContentHref) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @depends testPublishContent |
||
| 147 | * Covers GET /content/objects/<contentId>/currentversion |
||
| 148 | * @covers \eZ\Publish\Core\REST\Server\Controller\Content::redirectCurrentVersion |
||
| 149 | */ |
||
| 150 | public function testRedirectCurrentVersion($restContentHref) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @depends testCreateDraftFromVersion |
||
| 163 | * Covers GET /content/objects/<contentId>/versions/<versionNumber> |
||
| 164 | * |
||
| 165 | * @param string $restContentVersionHref |
||
| 166 | */ |
||
| 167 | public function testLoadContentVersion($restContentVersionHref) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Covers COPY /content/objects/<contentId>. |
||
| 180 | * @depends testPublishContent |
||
| 181 | * |
||
| 182 | * @return string the copied content href |
||
| 183 | */ |
||
| 184 | public function testCopyContent($restContentHref) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Covers DELETE /content/objects/<versionNumber>. |
||
| 203 | * @depends testCopyContent |
||
| 204 | */ |
||
| 205 | public function testDeleteContent($restContentHref) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @depends testPublishContent |
||
| 217 | * Covers GET /content/objects/<contentId>/versions |
||
| 218 | */ |
||
| 219 | public function testLoadContentVersions($restContentHref) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @depends testPublishContent |
||
| 230 | * |
||
| 231 | * @param string $restContentHref /content/objects/<contentId> |
||
| 232 | * Covers COPY /content/objects/<contentId>/currentversion |
||
| 233 | * |
||
| 234 | * @return string the ID of the created version (/content/objects/<contentId>/versions/<versionNumber> |
||
| 235 | */ |
||
| 236 | View Code Duplication | public function testCreateDraftFromCurrentVersion($restContentHref) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @depends testCreateDraftFromCurrentVersion |
||
| 250 | * |
||
| 251 | * @param string $restContentVersionHref /api/ezp/v2/content/objects/<contentId>/versions>/<versionNumber> |
||
| 252 | * Covers DELETE /api/ezp/v2/content/objects/<contentId>/versions>/<versionNumber> |
||
| 253 | */ |
||
| 254 | public function testDeleteContentVersion($restContentVersionHref) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @depends testCreateDraftFromVersion |
||
| 265 | * Covers PATCH /content/objects/<contentId>/versions>/<versionNumber> |
||
| 266 | * |
||
| 267 | * @param string $restContentVersionHref /content/objects/<contentId>/versions>/<versionNumber> |
||
| 268 | */ |
||
| 269 | public function testUpdateVersion($restContentVersionHref) |
||
| 270 | { |
||
| 271 | $xml = <<< XML |
||
| 272 | <VersionUpdate> |
||
| 273 | <fields> |
||
| 274 | <field> |
||
| 275 | <fieldDefinitionIdentifier>name</fieldDefinitionIdentifier> |
||
| 276 | <languageCode>eng-GB</languageCode> |
||
| 277 | <fieldValue>testUpdateVersion</fieldValue> |
||
| 278 | </field> |
||
| 279 | </fields> |
||
| 280 | </VersionUpdate> |
||
| 281 | XML; |
||
| 282 | |||
| 283 | $request = $this->createHttpRequest('PATCH', $restContentVersionHref, 'VersionUpdate+xml', 'Version+json'); |
||
| 284 | $request->setContent($xml); |
||
| 285 | $response = $this->sendHttpRequest( |
||
| 286 | $request |
||
| 287 | ); |
||
| 288 | |||
| 289 | self::assertHttpResponseCodeEquals($response, 200); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @depends testPublishContent |
||
| 294 | * Covers GET /content/objects/<contentId>/relations |
||
| 295 | */ |
||
| 296 | public function testRedirectCurrentVersionRelations($restContentHref) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @depends testCreateDraftFromVersion |
||
| 312 | * Covers GET /content/objects/<contentId>/versions/<versionNumber>/relations |
||
| 313 | */ |
||
| 314 | public function testLoadVersionRelations($restContentVersionHref) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @depends testCreateDraftFromVersion |
||
| 325 | * Covers POST /content/objects/<contentId>/versions/<versionNumber>/relations/<relationId> |
||
| 326 | * |
||
| 327 | * @return string created relation HREF (/content/objects/<contentId>/versions/<versionNumber>/relations/<relationId> |
||
| 328 | */ |
||
| 329 | public function testCreateRelation($restContentVersionHref) |
||
| 330 | { |
||
| 331 | $content = <<< XML |
||
| 332 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 333 | <RelationCreate> |
||
| 334 | <Destination href="/api/ezp/v2/content/objects/10"/> |
||
| 335 | </RelationCreate> |
||
| 336 | XML; |
||
| 337 | |||
| 338 | $request = $this->createHttpRequest('POST', "$restContentVersionHref/relations", 'RelationCreate+xml', 'Relation+json'); |
||
| 339 | $request->setContent($content); |
||
| 340 | |||
| 341 | $response = $this->sendHttpRequest($request); |
||
| 342 | |||
| 343 | self::assertHttpResponseCodeEquals($response, 201); |
||
| 344 | |||
| 345 | $response = json_decode($response->getContent(), true); |
||
| 346 | |||
| 347 | return $response['Relation']['_href']; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @depends testCreateRelation |
||
| 352 | * Covers GET /content/objects/<contentId>/versions/<versionNo>/relations/<relationId> |
||
| 353 | */ |
||
| 354 | public function testLoadVersionRelation($restContentRelationHref) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Returns the Content key from the decoded JSON of $restContentId's contentInfo. |
||
| 367 | * |
||
| 368 | * |
||
| 369 | * @throws \InvalidArgumentException |
||
| 370 | * |
||
| 371 | * @param string $restContentHref /api/ezp/v2/content/objects/<contentId> |
||
| 372 | * |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | private function loadContent($restContentHref) |
||
| 392 | |||
| 393 | public function testCreateView() |
||
| 394 | { |
||
| 395 | $body = <<< XML |
||
| 396 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 397 | <ViewInput> |
||
| 398 | <identifier>testCreateView</identifier> |
||
| 399 | <Query> |
||
| 400 | <Criteria> |
||
| 401 | <ContentTypeIdentifierCriterion>folder</ContentTypeIdentifierCriterion> |
||
| 402 | </Criteria> |
||
| 403 | <limit>10</limit> |
||
| 404 | <offset>0</offset> |
||
| 405 | </Query> |
||
| 406 | </ViewInput> |
||
| 407 | XML; |
||
| 408 | $request = $this->createHttpRequest('POST', '/api/ezp/v2/content/views', 'ViewInput+xml', 'View+json'); |
||
| 409 | $request->setContent($body); |
||
| 410 | $response = $this->sendHttpRequest( |
||
| 411 | $request |
||
| 412 | ); |
||
| 413 | |||
| 414 | // Returns 301 since 6.0 (deprecated in favour of /views) |
||
| 415 | self::assertHttpResponseCodeEquals($response, 301); |
||
| 416 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Covers DELETE /content/objects/<contentId>/versions/<versionNo>/translations/<languageCode>. |
||
| 421 | * |
||
| 422 | * @depends testCreateDraftFromVersion |
||
| 423 | * |
||
| 424 | * @param string $restContentVersionHref |
||
| 425 | */ |
||
| 426 | public function testDeleteTranslationFromDraft($restContentVersionHref) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Test that VersionInfo loaded in VersionList contains working DeleteTranslation resource link. |
||
| 448 | * |
||
| 449 | * Covers DELETE /content/objects/<contentId>/versions/<versionNo>/translations/<languageCode>. |
||
| 450 | * Covers GET /content/objects/<contentId>/versions |
||
| 451 | * |
||
| 452 | * @depends testCreateDraftFromVersion |
||
| 453 | * |
||
| 454 | * @param string $restContentVersionHref |
||
| 455 | */ |
||
| 456 | public function testLoadContentVersionsProvidesDeleteTranslationFromDraftResourceLink($restContentVersionHref) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Publish another Version with new Translation. |
||
| 507 | * |
||
| 508 | * @param string $restContentVersionHref |
||
| 509 | * |
||
| 510 | * @param string $languageCode |
||
| 511 | * @param string $languageName |
||
| 512 | * |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | View Code Duplication | private function createVersionTranslation($restContentVersionHref, $languageCode, $languageName) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Make REST API calls to check if the given Language exists and create it if it doesn't. |
||
| 542 | * |
||
| 543 | * @param string $languageCode |
||
| 544 | * @param string $languageName |
||
| 545 | */ |
||
| 546 | private function ensureLanguageExists($languageCode, $languageName) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Iterate through Version Items returned by REST view for ContentType: VersionList+json |
||
| 553 | * and return first VersionInfo data matching given status. |
||
| 554 | * |
||
| 555 | * @param array $versionList |
||
| 556 | * @param string $status uppercase string representation of Version status |
||
| 557 | * |
||
| 558 | * @return array |
||
| 559 | */ |
||
| 560 | private function getVersionInfoFromJSONVersionListByStatus(array $versionList, $status) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Assert that Version REST Response contains proper fields. |
||
| 573 | * |
||
| 574 | * @param \Buzz\Message\Response $response |
||
| 575 | */ |
||
| 576 | private function assertVersionResponseContainsExpectedFields(Response $response) |
||
| 591 | } |
||
| 592 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.