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:
Complex classes like ContentTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContentTest, and based on these observations, apply Extract Interface, too.
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 | View Code Duplication | public function testRedirectContent($restContentHref) |
|
85 | { |
||
86 | $response = $this->sendHttpRequest( |
||
87 | $this->createHttpRequest('GET', '/api/ezp/v2/content/objects?remoteId=' . $this->addTestSuffix('testCreateContent')) |
||
88 | ); |
||
89 | |||
90 | self::assertHttpResponseCodeEquals($response, 307); |
||
91 | self::assertEquals($response->getHeader('Location'), $restContentHref); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @depends testPublishContent |
||
96 | */ |
||
97 | public function testLoadContent($restContentHref) |
||
106 | |||
107 | /** |
||
108 | * @depends testPublishContent |
||
109 | */ |
||
110 | public function testUpdateContentMetadata($restContentHref) |
||
111 | { |
||
112 | $string = $this->addTestSuffix(__FUNCTION__); |
||
113 | $content = <<< XML |
||
114 | <?xml version="1.0" encoding="UTF-8"?> |
||
115 | <ContentUpdate> |
||
116 | <Owner href="/api/ezp/v2/user/users/10"/> |
||
117 | <remoteId>{$string}</remoteId> |
||
118 | </ContentUpdate> |
||
119 | XML; |
||
120 | $request = $this->createHttpRequest('PATCH', $restContentHref, 'ContentUpdate+xml', 'ContentInfo+json'); |
||
121 | $request->setContent($content); |
||
122 | $response = $this->sendHttpRequest($request); |
||
123 | self::assertHttpResponseCodeEquals($response, 200); |
||
124 | |||
125 | // @todo test data |
||
126 | } |
||
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 | View Code Duplication | public function testUpdateVersion($restContentVersionHref) |
|
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 | * Covers DELETE /content/objects/<contentId>/translations/<languageCode>. |
||
507 | */ |
||
508 | public function testDeleteTranslation() |
||
553 | |||
554 | /** |
||
555 | * Test that deleting content which has Version(s) with single Translation being deleted is supported. |
||
556 | * |
||
557 | * Covers DELETE /content/objects/<contentId>/translations/<languageCode>. |
||
558 | * |
||
559 | * @depends testDeleteTranslation |
||
560 | * |
||
561 | * @param string $restContentHref |
||
562 | */ |
||
563 | public function testDeleteTranslationOfContentWithSingleTranslationVersion($restContentHref) |
||
596 | |||
597 | /** |
||
598 | * Publish another Version with new Translation. |
||
599 | * |
||
600 | * @param string $restContentVersionHref |
||
601 | * |
||
602 | * @param string $languageCode |
||
603 | * @param string $languageName |
||
604 | * |
||
605 | * @return string |
||
606 | */ |
||
607 | View Code Duplication | private function createVersionTranslation($restContentVersionHref, $languageCode, $languageName) |
|
632 | |||
633 | /** |
||
634 | * Iterate through Version Items returned by REST view for ContentType: VersionList+json |
||
635 | * and return first VersionInfo data matching given status. |
||
636 | * |
||
637 | * @param array $versionList |
||
638 | * @param string $status uppercase string representation of Version status |
||
639 | * |
||
640 | * @return array |
||
641 | */ |
||
642 | private function getVersionInfoFromJSONVersionListByStatus(array $versionList, $status) |
||
652 | |||
653 | /** |
||
654 | * Assert that Version REST Response contains proper fields. |
||
655 | * |
||
656 | * @param \Buzz\Message\Response $response |
||
657 | */ |
||
658 | private function assertVersionResponseContainsExpectedFields(Response $response) |
||
673 | |||
674 | /** |
||
675 | * Create new Content Draft. |
||
676 | * |
||
677 | * @param string $restContentTypeHref Content Type REST resource link |
||
678 | * @param string $restParentLocationHref Parent Location REST resource link |
||
679 | * @param string $restSectionHref Section REST resource link |
||
680 | * @param string $restUserHref User REST resource link |
||
681 | * @param array $fieldValues multilingual field values <code>['fieldIdentifier' => ['languageCode' => 'value']]</code> |
||
682 | * |
||
683 | * @return array Content structure decoded from JSON |
||
684 | */ |
||
685 | private function createContentDraft($restContentTypeHref, $restParentLocationHref, $restSectionHref, $restUserHref, array $fieldValues) |
||
741 | |||
742 | /** |
||
743 | * Create Draft of a given Content and versionNo. |
||
744 | * |
||
745 | * @param string $restContentVersionHref REST resource link of Content Version |
||
746 | * |
||
747 | * @return string Content Version Draft REST resource link |
||
748 | */ |
||
749 | private function createDraftFromVersion($restContentVersionHref) |
||
750 | { |
||
751 | $response = $this->sendHttpRequest( |
||
752 | $this->createHttpRequest('COPY', "{$restContentVersionHref}") |
||
753 | ); |
||
754 | self::assertHttpResponseCodeEquals($response, 201); |
||
755 | |||
756 | $href = $response->getHeader('Location'); |
||
757 | self::assertNotEmpty($href); |
||
758 | |||
759 | return $href; |
||
760 | } |
||
761 | |||
762 | /** |
||
763 | * Publish Content Version Draft given by REST resource link. |
||
764 | * |
||
765 | * @param string $restContentVersionHref REST resource link of Version Draft |
||
766 | */ |
||
767 | private function publishContentVersionDraft($restContentVersionHref) |
||
774 | |||
775 | /** |
||
776 | * Update Main Translation of a Content. |
||
777 | * |
||
778 | * @param string $restContentHref REST resource link of Content |
||
779 | * @param string $languageCode new Main Translation language code |
||
780 | */ |
||
781 | View Code Duplication | private function updateMainTranslation($restContentHref, $languageCode) |
|
796 | } |
||
797 |