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() { |
||
35 | return new Item(); |
||
36 | } |
||
37 | |||
38 | public function testGetId() { |
||
39 | $item = new Item(); |
||
40 | $this->assertNull( $item->getId() ); |
||
41 | |||
42 | $item->setId( new ItemId( 'Q1' ) ); |
||
43 | $this->assertEquals( new ItemId( 'Q1' ), $item->getId() ); |
||
44 | |||
45 | $item->setId( null ); |
||
46 | $this->assertNull( $item->getId() ); |
||
47 | |||
48 | $item = new Item( new ItemId( 'Q2' ) ); |
||
49 | $this->assertEquals( new ItemId( 'Q2' ), $item->getId() ); |
||
50 | } |
||
51 | |||
52 | public function testSetIdUsingNumber() { |
||
53 | $item = new Item(); |
||
54 | $item->setId( 42 ); |
||
55 | $this->assertEquals( new ItemId( 'Q42' ), $item->getId() ); |
||
56 | } |
||
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() { |
||
178 | $item = new Item(); |
||
179 | $this->assertTrue( $item->getSiteLinkList()->isEmpty() ); |
||
180 | } |
||
181 | |||
182 | public function testAddSiteLinkOverridesOldLinks() { |
||
183 | $item = new Item(); |
||
184 | |||
185 | $item->getSiteLinkList()->addNewSiteLink( 'kittens', 'foo' ); |
||
186 | |||
187 | $newLink = new SiteLink( 'kittens', 'bar' ); |
||
188 | $item->addSiteLink( $newLink ); |
||
189 | |||
190 | $this->assertTrue( $item->getSiteLinkList()->getBySiteId( 'kittens' )->equals( $newLink ) ); |
||
191 | } |
||
192 | |||
193 | public function testEmptyItemIsEmpty() { |
||
194 | $item = new Item(); |
||
195 | $this->assertTrue( $item->isEmpty() ); |
||
196 | } |
||
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 testClearRemovesAllButId() { |
||
235 | $item = new Item( new ItemId( 'Q42' ) ); |
||
236 | $item->getFingerprint()->setLabel( 'en', 'foo' ); |
||
237 | $item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Foo' ); |
||
238 | $item->getStatements()->addStatement( $this->newStatement() ); |
||
239 | |||
240 | $item->clear(); |
||
241 | |||
242 | $this->assertEquals( new ItemId( 'Q42' ), $item->getId() ); |
||
243 | $this->assertTrue( $item->getFingerprint()->isEmpty() ); |
||
244 | $this->assertTrue( $item->getLabels()->isEmpty() ); |
||
245 | $this->assertTrue( $item->getDescriptions()->isEmpty() ); |
||
246 | $this->assertTrue( $item->getAliasGroups()->isEmpty() ); |
||
247 | $this->assertTrue( $item->getSiteLinkList()->isEmpty() ); |
||
248 | $this->assertTrue( $item->getStatements()->isEmpty() ); |
||
249 | } |
||
250 | |||
251 | public function testEmptyConstructor() { |
||
252 | $item = new Item(); |
||
253 | |||
254 | $this->assertNull( $item->getId() ); |
||
255 | $this->assertTrue( $item->getFingerprint()->isEmpty() ); |
||
256 | $this->assertTrue( $item->getLabels()->isEmpty() ); |
||
257 | $this->assertTrue( $item->getDescriptions()->isEmpty() ); |
||
258 | $this->assertTrue( $item->getAliasGroups()->isEmpty() ); |
||
259 | $this->assertTrue( $item->getSiteLinkList()->isEmpty() ); |
||
260 | $this->assertTrue( $item->getStatements()->isEmpty() ); |
||
261 | } |
||
262 | |||
263 | public function testCanConstructWithStatementList() { |
||
276 | |||
277 | public function testSetStatements() { |
||
284 | |||
285 | public function testGetStatementsReturnsCorrectTypeAfterClear() { |
||
291 | |||
292 | public function equalsProvider() { |
||
293 | $firstItem = new Item(); |
||
294 | $firstItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) ); |
||
295 | |||
296 | $secondItem = new Item(); |
||
297 | $secondItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) ); |
||
298 | |||
299 | $secondItemWithId = $secondItem->copy(); |
||
300 | $secondItemWithId->setId( 42 ); |
||
301 | |||
302 | $differentId = $secondItemWithId->copy(); |
||
303 | $differentId->setId( 43 ); |
||
304 | |||
305 | return array( |
||
306 | array( new Item(), new Item() ), |
||
307 | array( $firstItem, $secondItem ), |
||
308 | array( $secondItem, $secondItemWithId ), |
||
309 | array( $secondItemWithId, $differentId ), |
||
310 | ); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * @dataProvider equalsProvider |
||
315 | */ |
||
316 | public function testEquals( Item $firstItem, Item $secondItem ) { |
||
320 | |||
321 | private function getBaseItem() { |
||
331 | |||
332 | public function notEqualsProvider() { |
||
361 | |||
362 | /** |
||
363 | * @dataProvider notEqualsProvider |
||
364 | */ |
||
365 | public function testNotEquals( Item $firstItem, Item $secondItem ) { |
||
369 | |||
370 | public function cloneProvider() { |
||
381 | |||
382 | /** |
||
383 | * @dataProvider cloneProvider |
||
384 | */ |
||
385 | public function testCloneIsEqualButNotIdentical( Item $original, Item $clone ) { |
||
408 | |||
409 | /** |
||
410 | * @dataProvider cloneProvider |
||
411 | */ |
||
412 | public function testOriginalDoesNotChangeWithClone( Item $original, Item $clone ) { |
||
413 | $originalStatement = $original->getStatements()->getFirstStatementWithGuid( null ); |
||
414 | $clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null ); |
||
415 | |||
416 | $clone->setLabel( 'en', 'clone' ); |
||
417 | $clone->setDescription( 'en', 'clone' ); |
||
418 | $clone->setAliases( 'en', array( 'clone' ) ); |
||
419 | $clonedStatement->setGuid( 'clone' ); |
||
420 | $clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) ); |
||
421 | $clonedStatement->setRank( Statement::RANK_DEPRECATED ); |
||
422 | $clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) ); |
||
423 | $clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) ); |
||
424 | $clone->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' ); |
||
425 | |||
426 | $this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() ); |
||
427 | $this->assertFalse( $original->getFingerprint()->hasDescription( 'en' ) ); |
||
428 | $this->assertFalse( $original->getFingerprint()->hasAliasGroup( 'en' ) ); |
||
429 | $this->assertNull( $originalStatement->getGuid() ); |
||
436 | |||
437 | // Below are tests copied from EntityTest |
||
438 | |||
439 | public function labelProvider() { |
||
446 | |||
447 | /** |
||
448 | * @dataProvider labelProvider |
||
449 | * @param string $languageCode |
||
450 | * @param string $labelText |
||
451 | * @param string $moarText |
||
452 | */ |
||
453 | public function testSetLabel( $languageCode, $labelText, $moarText = 'ohi there' ) { |
||
464 | |||
465 | public function descriptionProvider() { |
||
472 | |||
473 | /** |
||
474 | * @dataProvider descriptionProvider |
||
475 | * @param string $languageCode |
||
476 | * @param string $description |
||
477 | * @param string $moarText |
||
478 | */ |
||
479 | public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) { |
||
490 | |||
491 | public function aliasesProvider() { |
||
515 | |||
516 | /** |
||
517 | * @dataProvider aliasesProvider |
||
518 | */ |
||
519 | public function testSetAliases( array $aliasesLists ) { |
||
538 | |||
539 | /** |
||
540 | * @dataProvider aliasesProvider |
||
541 | */ |
||
542 | public function testSetEmptyAlias( array $aliasesLists ) { |
||
563 | |||
564 | public function instanceProvider() { |
||
599 | |||
600 | /** |
||
601 | * @dataProvider instanceProvider |
||
602 | * @param Item $entity |
||
603 | */ |
||
604 | public function testCopy( Item $entity ) { |
||
613 | |||
614 | public function testCopyRetainsLabels() { |
||
625 | |||
626 | /** |
||
627 | * @dataProvider instanceProvider |
||
628 | * @param Item $entity |
||
629 | */ |
||
630 | public function testSerialize( Item $entity ) { |
||
640 | |||
641 | public function testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() { |
||
649 | |||
650 | public function testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() { |
||
666 | |||
667 | public function testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() { |
||
689 | |||
690 | public function testGivenEmptyFingerprint_noTermsAreSet() { |
||
696 | |||
697 | public function testGivenEmptyFingerprint_existingTermsAreRemoved() { |
||
708 | |||
709 | public function testWhenSettingFingerprint_getFingerprintReturnsIt() { |
||
728 | |||
729 | public function testGetLabels() { |
||
740 | |||
741 | public function testGetDescriptions() { |
||
752 | |||
753 | public function testGetAliasGroups() { |
||
764 | |||
765 | public function testGetLabels_sameListAsFingerprint() { |
||
773 | |||
774 | public function testGetDescriptions_sameListAsFingerprint() { |
||
782 | |||
783 | public function testGetAliasGroups_sameListAsFingerprint() { |
||
791 | |||
792 | } |
||
793 |
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.