Complex classes like StatementListTest 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 StatementListTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class StatementListTest extends \PHPUnit_Framework_TestCase { |
||
26 | |||
27 | /** |
||
28 | * @param int $propertyId |
||
29 | * @param string|null $guid |
||
30 | * @param int $rank |
||
31 | * |
||
32 | * @return Statement |
||
33 | */ |
||
34 | private function getStatement( $propertyId, $guid, $rank = Statement::RANK_NORMAL ) { |
||
35 | $statement = $this->getMockBuilder( 'Wikibase\DataModel\Statement\Statement' ) |
||
36 | ->disableOriginalConstructor() |
||
37 | ->getMock(); |
||
38 | |||
39 | $statement->expects( $this->any() ) |
||
40 | ->method( 'getGuid' ) |
||
41 | ->will( $this->returnValue( $guid ) ); |
||
42 | |||
43 | $statement->expects( $this->any() ) |
||
44 | ->method( 'getPropertyId' ) |
||
45 | ->will( $this->returnValue( PropertyId::newFromNumber( $propertyId ) ) ); |
||
46 | |||
47 | $statement->expects( $this->any() ) |
||
48 | ->method( 'getRank' ) |
||
49 | ->will( $this->returnValue( $rank ) ); |
||
50 | |||
51 | return $statement; |
||
52 | } |
||
53 | |||
54 | private function getStatementWithSnak( $propertyId, $stringValue ) { |
||
55 | $snak = $this->newSnak( $propertyId, $stringValue ); |
||
56 | $statement = new Statement( $snak ); |
||
57 | $statement->setGuid( sha1( $snak->getHash() ) ); |
||
58 | return $statement; |
||
59 | } |
||
60 | |||
61 | private function newSnak( $propertyId, $stringValue ) { |
||
64 | |||
65 | public function testConstructorAcceptsDuplicatesWithNoGuid() { |
||
66 | $list = new StatementList( |
||
67 | $this->getStatement( 1, null ), |
||
68 | $this->getStatement( 1, null ) |
||
69 | ); |
||
70 | |||
71 | $this->assertSame( 2, $list->count() ); |
||
72 | } |
||
73 | |||
74 | public function testConstructorAcceptsDuplicatesWithSameGuid() { |
||
75 | $list = new StatementList( |
||
76 | $this->getStatement( 1, 'duplicate' ), |
||
77 | $this->getStatement( 1, 'duplicate' ) |
||
78 | ); |
||
79 | |||
80 | $this->assertSame( 2, $list->count() ); |
||
81 | } |
||
82 | |||
83 | public function testGivenNoStatements_getPropertyIdsReturnsEmptyArray() { |
||
87 | |||
88 | public function testGivenStatements_getPropertyIdsReturnsArrayWithoutDuplicates() { |
||
89 | $list = new StatementList( |
||
90 | $this->getStatement( 1, 'kittens' ), |
||
91 | $this->getStatement( 3, 'foo' ), |
||
92 | $this->getStatement( 2, 'bar' ), |
||
93 | $this->getStatement( 2, 'baz' ), |
||
94 | $this->getStatement( 1, 'bah' ) |
||
95 | ); |
||
96 | |||
97 | $this->assertEquals( |
||
98 | array( |
||
99 | 'P1' => new PropertyId( 'P1' ), |
||
100 | 'P3' => new PropertyId( 'P3' ), |
||
101 | 'P2' => new PropertyId( 'P2' ), |
||
102 | ), |
||
103 | $list->getPropertyIds() |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | public function testGivenStatementsWithArrayKeys_toArrayReturnsReindexedArray() { |
||
108 | $statement = $this->getStatement( 1, 'guid' ); |
||
109 | $list = new StatementList( array( 'ignore-me' => $statement ) ); |
||
110 | |||
111 | $this->assertSame( array( 0 => $statement ), $list->toArray() ); |
||
112 | } |
||
113 | |||
114 | public function testGivenSparseArray_toArrayReturnsReindexedArray() { |
||
115 | $statement = $this->getStatement( 1, 'guid' ); |
||
116 | $list = new StatementList( array( 1 => $statement ) ); |
||
117 | |||
118 | $this->assertSame( array( 0 => $statement ), $list->toArray() ); |
||
119 | } |
||
120 | |||
121 | public function testCanIterate() { |
||
122 | $statement = $this->getStatement( 1, 'kittens' ); |
||
123 | $list = new StatementList( $statement ); |
||
124 | |||
125 | foreach ( $list as $statementFormList ) { |
||
126 | $this->assertEquals( $statement, $statementFormList ); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public function testGetUniqueMainSnaksReturnsListWithoutDuplicates() { |
||
131 | $list = new StatementList( |
||
132 | $this->getStatementWithSnak( 1, 'foo' ), |
||
133 | $this->getStatementWithSnak( 2, 'foo' ), |
||
134 | $this->getStatementWithSnak( 1, 'foo' ), |
||
135 | $this->getStatementWithSnak( 2, 'bar' ), |
||
136 | $this->getStatementWithSnak( 1, 'bar' ) |
||
137 | ); |
||
138 | |||
139 | $this->assertEquals( |
||
140 | array( |
||
141 | $this->getStatementWithSnak( 1, 'foo' ), |
||
142 | $this->getStatementWithSnak( 2, 'foo' ), |
||
143 | $this->getStatementWithSnak( 2, 'bar' ), |
||
144 | $this->getStatementWithSnak( 1, 'bar' ), |
||
145 | ), |
||
146 | array_values( $list->getWithUniqueMainSnaks()->toArray() ) |
||
|
|||
147 | ); |
||
148 | } |
||
149 | |||
150 | public function testGetAllSnaksReturnsAllSnaks() { |
||
151 | $list = new StatementList( |
||
152 | $this->getStatementWithSnak( 1, 'foo' ), |
||
153 | $this->getStatementWithSnak( 2, 'foo' ), |
||
154 | $this->getStatementWithSnak( 1, 'foo' ), |
||
155 | $this->getStatementWithSnak( 2, 'bar' ), |
||
156 | $this->getStatementWithSnak( 1, 'bar' ) |
||
157 | ); |
||
158 | |||
159 | $this->assertEquals( |
||
160 | array( |
||
161 | $this->newSnak( 1, 'foo' ), |
||
162 | $this->newSnak( 2, 'foo' ), |
||
163 | $this->newSnak( 1, 'foo' ), |
||
164 | $this->newSnak( 2, 'bar' ), |
||
165 | $this->newSnak( 1, 'bar' ), |
||
166 | ), |
||
167 | $list->getAllSnaks() |
||
168 | ); |
||
169 | } |
||
170 | |||
171 | public function testAddStatementWithOnlyMainSnak() { |
||
172 | $list = new StatementList(); |
||
173 | |||
174 | $list->addNewStatement( $this->newSnak( 42, 'foo' ) ); |
||
175 | |||
176 | $this->assertEquals( |
||
177 | new StatementList( new Statement( $this->newSnak( 42, 'foo' ) ) ), |
||
178 | $list |
||
179 | ); |
||
180 | } |
||
181 | |||
182 | public function testAddStatementWithQualifiersAsSnakArray() { |
||
204 | |||
205 | public function testAddStatementWithQualifiersAsSnakList() { |
||
206 | $list = new StatementList(); |
||
207 | $snakList = new SnakList( array( |
||
208 | $this->newSnak( 1, 'bar' ) |
||
209 | ) ); |
||
210 | |||
211 | $list->addNewStatement( |
||
212 | $this->newSnak( 42, 'foo' ), |
||
213 | $snakList |
||
221 | |||
222 | public function testAddStatementWithGuid() { |
||
238 | |||
239 | public function testGivenNegativeIndex_addStatementFails() { |
||
240 | $statement = new Statement( new PropertyNoValueSnak( 1 ) ); |
||
246 | |||
247 | public function testGivenLargeIndex_addStatementAppends() { |
||
254 | |||
255 | public function testGivenZeroIndex_addStatementPrepends() { |
||
263 | |||
264 | public function testGivenValidIndex_addStatementInserts() { |
||
274 | |||
275 | public function testGivenGuidOfPresentStatement_statementIsRemoved() { |
||
285 | |||
286 | public function testGivenGuidOfMultipleStatements_multipleStatementsAreRemoved() { |
||
296 | |||
297 | public function testGivenNotPresentGuid_listIsNotModified() { |
||
307 | |||
308 | public function testGivenNullGuid_allStatementsWithNoGuidAreRemoved() { |
||
318 | |||
319 | public function testCanConstructWithTraversableContainingOnlyStatements() { |
||
333 | |||
334 | public function testGivenTraversableWithNonStatements_constructorThrowsException() { |
||
344 | |||
345 | public function testGivenNonTraversableOrArgList_constructorThrowsException() { |
||
349 | |||
350 | public function testCanConstructWithStatement() { |
||
358 | |||
359 | public function testCanConstructWithStatementArgumentList() { |
||
369 | |||
370 | public function testGivenArgumentListWithNonStatement_constructorThrowsException() { |
||
378 | |||
379 | public function testCountForEmptyList() { |
||
384 | |||
385 | public function testCountForNonEmptyList() { |
||
393 | |||
394 | /** |
||
395 | * @dataProvider statementArrayProvider |
||
396 | */ |
||
397 | public function testGivenIdenticalLists_equalsReturnsTrue( array $statements ) { |
||
403 | |||
404 | public function statementArrayProvider() { |
||
417 | |||
418 | public function testGivenDifferentLists_equalsReturnsFalse() { |
||
431 | |||
432 | public function testGivenListsWithDifferentDuplicates_equalsReturnsFalse() { |
||
447 | |||
448 | public function testGivenListsWithDifferentOrder_equalsReturnsFalse() { |
||
463 | |||
464 | public function testEmptyListDoesNotEqualNonEmptyList() { |
||
475 | |||
476 | public function testNonEmptyListDoesNotEqualEmptyList() { |
||
487 | |||
488 | public function testEmptyListIsEmpty() { |
||
493 | |||
494 | public function testNonEmptyListIsNotEmpty() { |
||
499 | |||
500 | public function testGetMainSnaks() { |
||
516 | |||
517 | public function testGivenNotKnownPropertyId_getByPropertyIdReturnsEmptyList() { |
||
526 | |||
527 | public function testGivenKnownPropertyId_getByPropertyIdReturnsListWithOnlyMatchingStatements() { |
||
543 | |||
544 | public function testGivenInvalidRank_getByRankReturnsEmptyList() { |
||
548 | |||
549 | public function testGivenValidRank_getByRankReturnsOnlyMatchingStatements() { |
||
571 | |||
572 | public function testWhenListIsEmpty_getBestStatementsReturnsEmptyList() { |
||
576 | |||
577 | public function testWhenOnlyDeprecatedStatements_getBestStatementsReturnsEmptyList() { |
||
587 | |||
588 | public function testWhenPreferredStatements_getBestStatementsReturnsOnlyThose() { |
||
607 | |||
608 | public function testWhenNoPreferredStatements_getBestStatementsReturnsOnlyNormalOnes() { |
||
624 | |||
625 | public function testGivenNotPresentStatement_getFirstStatementWithGuidReturnsNull() { |
||
630 | |||
631 | public function testGivenPresentStatement_getFirstStatementWithGuidReturnsStatement() { |
||
640 | |||
641 | public function testGivenDoublyPresentStatement_getFirstStatementWithGuidReturnsFirstMatch() { |
||
651 | |||
652 | public function testGivenStatementsWithNoGuid_getFirstStatementWithGuidReturnsFirstMatch() { |
||
660 | |||
661 | public function testGivenInvalidGuid_getFirstStatementWithGuidReturnsNull() { |
||
666 | |||
667 | public function testFilter() { |
||
688 | |||
689 | } |
||
690 |
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.