Complex classes like ReferenceListTest 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 ReferenceListTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class ReferenceListTest extends PHPUnit_Framework_TestCase { |
||
26 | |||
27 | public function instanceProvider() { |
||
36 | |||
37 | public function getElementInstances() { |
||
44 | |||
45 | public function getConstructorArg() { |
||
51 | |||
52 | public function testCanConstructWithReferenceListObject() { |
||
60 | |||
61 | /** |
||
62 | * @dataProvider invalidConstructorArgumentsProvider |
||
63 | * @expectedException InvalidArgumentException |
||
64 | */ |
||
65 | public function testGivenInvalidConstructorArguments_constructorThrowsException( $input ) { |
||
68 | |||
69 | public function invalidConstructorArgumentsProvider() { |
||
87 | |||
88 | public function testGetIterator_isTraversable() { |
||
89 | $references = new ReferenceList(); |
||
90 | $references->addNewReference( new PropertyNoValueSnak( 1 ) ); |
||
91 | $iterator = $references->getIterator(); |
||
92 | |||
93 | $this->assertInstanceOf( 'Traversable', $iterator ); |
||
94 | $this->assertCount( 1, $iterator ); |
||
95 | foreach ( $references as $reference ) { |
||
96 | $this->assertInstanceOf( 'Wikibase\DataModel\Reference', $reference ); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @dataProvider instanceProvider |
||
102 | */ |
||
103 | public function testHasReferenceBeforeRemoveButNotAfter( ReferenceList $array ) { |
||
118 | |||
119 | public function testGivenCloneOfReferenceInList_hasReferenceReturnsTrue() { |
||
120 | $list = new ReferenceList(); |
||
121 | |||
122 | $reference = new Reference( array( new PropertyNoValueSnak( 42 ) ) ); |
||
123 | $sameReference = unserialize( serialize( $reference ) ); |
||
124 | |||
125 | $list->addReference( $reference ); |
||
126 | |||
127 | $this->assertTrue( |
||
128 | $list->hasReference( $sameReference ), |
||
129 | 'hasReference should return true when a reference with the same value is present, even when its another instance' |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @dataProvider instanceProvider |
||
135 | */ |
||
136 | public function testRemoveReference( ReferenceList $array ) { |
||
159 | |||
160 | public function testAddReferenceOnEmptyList() { |
||
171 | |||
172 | private function assertSameReferenceOrder( ReferenceList $expectedList, ReferenceList $references ) { |
||
178 | |||
179 | public function testAddReferenceOnNonEmptyList() { |
||
192 | |||
193 | public function testAddReferenceAtIndexZero() { |
||
204 | |||
205 | public function testAddReferenceAtNegativeIndex() { |
||
212 | |||
213 | public function testGivenEmptyReference_addReferenceDoesNotAdd() { |
||
224 | |||
225 | public function testGivenEmptyReferenceAndIndex_addReferenceDoesNotAdd() { |
||
226 | $reference1 = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
227 | $reference2 = new Reference( array( new PropertyNoValueSnak( 2 ) ) ); |
||
228 | $emptyReference = new Reference( array() ); |
||
229 | |||
230 | $references = new ReferenceList( array( $reference1, $reference2 ) ); |
||
231 | $references->addReference( $emptyReference, 0 ); |
||
232 | |||
233 | $expectedList = new ReferenceList( array( $reference1, $reference2 ) ); |
||
234 | $this->assertSameReferenceOrder( $expectedList, $references ); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @dataProvider instanceProvider |
||
239 | */ |
||
240 | public function testIndexOf( ReferenceList $array ) { |
||
248 | |||
249 | public function testIndexOf_checksForIdentity() { |
||
250 | $reference1 = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
251 | $reference2 = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
259 | |||
260 | /** |
||
261 | * @dataProvider instanceProvider |
||
262 | */ |
||
263 | public function testEquals( ReferenceList $array ) { |
||
267 | |||
268 | /** |
||
269 | * @dataProvider instanceProvider |
||
270 | */ |
||
271 | public function testGetValueHashReturnsString( ReferenceList $array ) { |
||
274 | |||
275 | /** |
||
276 | * @dataProvider instanceProvider |
||
277 | */ |
||
278 | public function testGetValueHashIsTheSameForClone( ReferenceList $array ) { |
||
282 | |||
283 | /** |
||
284 | * @dataProvider instanceProvider |
||
285 | */ |
||
286 | public function testHasReferenceHash( ReferenceList $references ) { |
||
296 | |||
297 | /** |
||
298 | * @dataProvider instanceProvider |
||
299 | */ |
||
300 | public function testGetReference( ReferenceList $references ) { |
||
310 | |||
311 | /** |
||
312 | * @dataProvider instanceProvider |
||
313 | */ |
||
314 | public function testRemoveReferenceHash( ReferenceList $references ) { |
||
332 | |||
333 | public function testGivenOneSnak_addNewReferenceAddsSnak() { |
||
340 | |||
341 | public function testGivenMultipleSnaks_addNewReferenceAddsThem() { |
||
352 | |||
353 | public function testGivenAnArrayOfSnaks_addNewReferenceAddsThem() { |
||
364 | |||
365 | public function testGivenNoneSnak_addNewReferenceThrowsException() { |
||
371 | |||
372 | public function testSerializationStability() { |
||
376 | |||
377 | public function testSerializeRoundtrip() { |
||
390 | |||
391 | public function testGivenEmptyList_isEmpty() { |
||
395 | |||
396 | public function testGivenNonEmptyList_isNotEmpty() { |
||
402 | |||
403 | } |
||
404 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: