Complex classes like ItemTest 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 ItemTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ItemTest extends PHPUnit_Framework_TestCase { |
||
33 | |||
34 | private function getNewEmpty() { |
||
37 | |||
38 | public function testGetId() { |
||
51 | |||
52 | public function testSetIdUsingNumber() { |
||
57 | |||
58 | public function testGetSiteLinkWithNonSetSiteId() { |
||
59 | $item = new Item(); |
||
60 | |||
61 | $this->setExpectedException( 'OutOfBoundsException' ); |
||
|
|||
62 | $item->getSiteLinkList()->getBySiteId( 'enwiki' ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @dataProvider simpleSiteLinkProvider |
||
67 | */ |
||
68 | public function testAddSiteLink( SiteLink $siteLink ) { |
||
69 | $item = new Item(); |
||
70 | |||
71 | $item->getSiteLinkList()->addSiteLink( $siteLink ); |
||
72 | |||
73 | $this->assertEquals( |
||
74 | $siteLink, |
||
75 | $item->getSiteLinkList()->getBySiteId( $siteLink->getSiteId() ) |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | public function simpleSiteLinkProvider() { |
||
80 | $argLists = array(); |
||
81 | |||
82 | $argLists[] = array( |
||
83 | new SiteLink( |
||
84 | 'enwiki', |
||
85 | 'Wikidata', |
||
86 | array( |
||
87 | new ItemId( 'Q42' ) |
||
88 | ) |
||
89 | ) |
||
90 | ); |
||
91 | $argLists[] = array( |
||
92 | new SiteLink( |
||
93 | 'nlwiki', |
||
94 | 'Wikidata' |
||
95 | ) |
||
96 | ); |
||
97 | $argLists[] = array( |
||
98 | new SiteLink( |
||
99 | 'enwiki', |
||
100 | 'Nyan!', |
||
101 | array( |
||
102 | new ItemId( 'Q42' ), |
||
103 | new ItemId( 'Q149' ) |
||
104 | ) |
||
105 | ) |
||
106 | ); |
||
107 | $argLists[] = array( |
||
108 | new SiteLink( |
||
109 | 'foo bar', |
||
110 | 'baz bah', |
||
111 | array( |
||
112 | new ItemId( 'Q3' ), |
||
113 | new ItemId( 'Q7' ) |
||
114 | ) |
||
115 | ) |
||
116 | ); |
||
117 | |||
118 | return $argLists; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @dataProvider simpleSiteLinksProvider |
||
123 | */ |
||
124 | public function testGetSiteLinks() { |
||
125 | $siteLinks = func_get_args(); |
||
126 | $item = new Item(); |
||
127 | |||
128 | foreach ( $siteLinks as $siteLink ) { |
||
129 | $item->getSiteLinkList()->addSiteLink( $siteLink ); |
||
130 | } |
||
131 | |||
132 | $this->assertInternalType( 'array', $item->getSiteLinks() ); |
||
133 | $this->assertEquals( $siteLinks, $item->getSiteLinks() ); |
||
134 | } |
||
135 | |||
136 | public function simpleSiteLinksProvider() { |
||
137 | $argLists = array(); |
||
138 | |||
139 | $argLists[] = array(); |
||
140 | |||
141 | $argLists[] = array( new SiteLink( 'enwiki', 'Wikidata', array( new ItemId( 'Q42' ) ) ) ); |
||
142 | |||
143 | $argLists[] = array( |
||
144 | new SiteLink( 'enwiki', 'Wikidata' ), |
||
145 | new SiteLink( 'nlwiki', 'Wikidata', array( new ItemId( 'Q3' ) ) ) |
||
146 | ); |
||
147 | |||
148 | $argLists[] = array( |
||
149 | new SiteLink( 'enwiki', 'Wikidata' ), |
||
150 | new SiteLink( 'nlwiki', 'Wikidata' ), |
||
151 | new SiteLink( 'foo bar', 'baz bah', array( new ItemId( 'Q2' ) ) ) |
||
152 | ); |
||
153 | |||
154 | return $argLists; |
||
155 | } |
||
156 | |||
157 | public function testHasLinkToSiteForFalse() { |
||
158 | $item = new Item(); |
||
159 | $item->getSiteLinkList()->addNewSiteLink( 'ENWIKI', 'Wikidata', array( new ItemId( 'Q42' ) ) ); |
||
160 | |||
161 | $this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) ); |
||
162 | $this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) ); |
||
163 | $this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) ); |
||
164 | } |
||
165 | |||
166 | public function testHasLinkToSiteForTrue() { |
||
167 | $item = new Item(); |
||
168 | $item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Wikidata', array( new ItemId( 'Q42' ) ) ); |
||
169 | $item->getSiteLinkList()->addNewSiteLink( 'dewiki', 'Wikidata' ); |
||
170 | $item->getSiteLinkList()->addNewSiteLink( 'foo bar', 'Wikidata' ); |
||
171 | |||
172 | $this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) ); |
||
173 | $this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) ); |
||
174 | $this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) ); |
||
175 | } |
||
176 | |||
177 | public function testEmptyItemReturnsEmptySiteLinkList() { |
||
181 | |||
182 | public function testAddSiteLinkOverridesOldLinks() { |
||
192 | |||
193 | public function testEmptyItemIsEmpty() { |
||
197 | |||
198 | public function testItemWithIdIsEmpty() { |
||
199 | $item = new Item( new ItemId( 'Q1337' ) ); |
||
200 | $this->assertTrue( $item->isEmpty() ); |
||
201 | } |
||
202 | |||
203 | public function testItemWithStuffIsNotEmpty() { |
||
204 | $item = new Item(); |
||
205 | $item->getFingerprint()->setAliasGroup( 'en', array( 'foo' ) ); |
||
206 | $this->assertFalse( $item->isEmpty() ); |
||
207 | |||
208 | $item = new Item(); |
||
209 | $item->getSiteLinkList()->addNewSiteLink( 'en', 'o_O' ); |
||
210 | $this->assertFalse( $item->isEmpty() ); |
||
211 | |||
212 | $item = new Item(); |
||
213 | $item->getStatements()->addStatement( $this->newStatement() ); |
||
214 | $this->assertFalse( $item->isEmpty() ); |
||
215 | } |
||
216 | |||
217 | public function testItemWithSitelinksHasSitelinks() { |
||
218 | $item = new Item(); |
||
219 | $item->getSiteLinkList()->addNewSiteLink( 'en', 'foo' ); |
||
220 | $this->assertFalse( $item->getSiteLinkList()->isEmpty() ); |
||
221 | } |
||
222 | |||
223 | public function testItemWithoutSitelinksHasNoSitelinks() { |
||
224 | $item = new Item(); |
||
225 | $this->assertTrue( $item->getSiteLinkList()->isEmpty() ); |
||
226 | } |
||
227 | |||
228 | private function newStatement() { |
||
229 | $statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
||
230 | $statement->setGuid( 'kittens' ); |
||
231 | return $statement; |
||
232 | } |
||
233 | |||
234 | public function testEmptyConstructor() { |
||
245 | |||
246 | public function testCanConstructWithStatementList() { |
||
259 | |||
260 | public function testSetStatements() { |
||
261 | $item = new Item(); |
||
262 | $item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) ); |
||
263 | |||
264 | $item->setStatements( new StatementList() ); |
||
265 | $this->assertTrue( $item->getStatements()->isEmpty() ); |
||
266 | } |
||
267 | |||
268 | public function equalsProvider() { |
||
288 | |||
289 | /** |
||
290 | * @dataProvider equalsProvider |
||
291 | */ |
||
292 | public function testEquals( Item $firstItem, Item $secondItem ) { |
||
293 | $this->assertTrue( $firstItem->equals( $secondItem ) ); |
||
294 | $this->assertTrue( $secondItem->equals( $firstItem ) ); |
||
295 | } |
||
296 | |||
297 | private function getBaseItem() { |
||
298 | $item = new Item( new ItemId( 'Q42' ) ); |
||
299 | $item->getFingerprint()->setLabel( 'en', 'Same' ); |
||
300 | $item->getFingerprint()->setDescription( 'en', 'Same' ); |
||
301 | $item->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) ); |
||
302 | $item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Same' ); |
||
303 | $item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) ); |
||
304 | |||
305 | return $item; |
||
306 | } |
||
307 | |||
308 | public function notEqualsProvider() { |
||
309 | $differentLabel = $this->getBaseItem(); |
||
310 | $differentLabel->getFingerprint()->setLabel( 'en', 'Different' ); |
||
311 | |||
312 | $differentDescription = $this->getBaseItem(); |
||
313 | $differentDescription->getFingerprint()->setDescription( 'en', 'Different' ); |
||
314 | |||
315 | $differentAlias = $this->getBaseItem(); |
||
316 | $differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) ); |
||
317 | |||
318 | $differentSiteLink = $this->getBaseItem(); |
||
319 | $differentSiteLink->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' ); |
||
320 | $differentSiteLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Different' ); |
||
321 | |||
322 | $differentStatement = $this->getBaseItem(); |
||
323 | $differentStatement->setStatements( new StatementList() ); |
||
324 | $differentStatement->getStatements()->addNewStatement( new PropertyNoValueSnak( 24 ) ); |
||
325 | |||
326 | $item = $this->getBaseItem(); |
||
327 | |||
328 | return array( |
||
329 | 'empty' => array( $item, new Item() ), |
||
330 | 'label' => array( $item, $differentLabel ), |
||
331 | 'description' => array( $item, $differentDescription ), |
||
332 | 'alias' => array( $item, $differentAlias ), |
||
333 | 'siteLink' => array( $item, $differentSiteLink ), |
||
334 | 'statement' => array( $item, $differentStatement ), |
||
335 | ); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * @dataProvider notEqualsProvider |
||
340 | */ |
||
341 | public function testNotEquals( Item $firstItem, Item $secondItem ) { |
||
342 | $this->assertFalse( $firstItem->equals( $secondItem ) ); |
||
343 | $this->assertFalse( $secondItem->equals( $firstItem ) ); |
||
344 | } |
||
345 | |||
346 | public function cloneProvider() { |
||
357 | |||
358 | /** |
||
359 | * @dataProvider cloneProvider |
||
360 | */ |
||
361 | public function testCloneIsEqualButNotIdentical( Item $original, Item $clone ) { |
||
384 | |||
385 | /** |
||
386 | * @dataProvider cloneProvider |
||
387 | */ |
||
388 | public function testOriginalDoesNotChangeWithClone( Item $original, Item $clone ) { |
||
412 | |||
413 | // Below are tests copied from EntityTest |
||
414 | |||
415 | public function labelProvider() { |
||
422 | |||
423 | /** |
||
424 | * @dataProvider labelProvider |
||
425 | * @param string $languageCode |
||
426 | * @param string $labelText |
||
427 | * @param string $moarText |
||
428 | */ |
||
429 | public function testSetLabel( $languageCode, $labelText, $moarText = 'ohi there' ) { |
||
440 | |||
441 | public function descriptionProvider() { |
||
448 | |||
449 | /** |
||
450 | * @dataProvider descriptionProvider |
||
451 | * @param string $languageCode |
||
452 | * @param string $description |
||
453 | * @param string $moarText |
||
454 | */ |
||
455 | public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) { |
||
466 | |||
467 | public function aliasesProvider() { |
||
491 | |||
492 | /** |
||
493 | * @dataProvider aliasesProvider |
||
494 | */ |
||
495 | public function testSetAliases( array $aliasesLists ) { |
||
514 | |||
515 | /** |
||
516 | * @dataProvider aliasesProvider |
||
517 | */ |
||
518 | public function testSetEmptyAlias( array $aliasesLists ) { |
||
539 | |||
540 | public function instanceProvider() { |
||
575 | |||
576 | /** |
||
577 | * @dataProvider instanceProvider |
||
578 | * @param Item $entity |
||
579 | */ |
||
580 | public function testCopy( Item $entity ) { |
||
589 | |||
590 | public function testCopyRetainsLabels() { |
||
601 | |||
602 | /** |
||
603 | * @dataProvider instanceProvider |
||
604 | * @param Item $entity |
||
605 | */ |
||
606 | public function testSerialize( Item $entity ) { |
||
616 | |||
617 | public function testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() { |
||
625 | |||
626 | public function testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() { |
||
642 | |||
643 | public function testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() { |
||
665 | |||
666 | public function testGivenEmptyFingerprint_noTermsAreSet() { |
||
672 | |||
673 | public function testGivenEmptyFingerprint_existingTermsAreRemoved() { |
||
684 | |||
685 | public function testWhenSettingFingerprint_getFingerprintReturnsIt() { |
||
704 | |||
705 | public function testGetLabels() { |
||
716 | |||
717 | public function testGetDescriptions() { |
||
728 | |||
729 | public function testGetAliasGroups() { |
||
740 | |||
741 | public function testGetLabels_sameListAsFingerprint() { |
||
749 | |||
750 | public function testGetDescriptions_sameListAsFingerprint() { |
||
758 | |||
759 | public function testGetAliasGroups_sameListAsFingerprint() { |
||
767 | |||
768 | } |
||
769 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.