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() { |
||
37 | |||
38 | public function testCanConstructWithReferenceListObject() { |
||
39 | $reference = new Reference( [ new PropertyNoValueSnak( 1 ) ] ); |
||
40 | $original = new ReferenceList( [ $reference ] ); |
||
41 | $copy = new ReferenceList( $original ); |
||
42 | |||
43 | $this->assertSame( 1, $copy->count() ); |
||
44 | $this->assertNotNull( $copy->getReference( $reference->getHash() ) ); |
||
45 | } |
||
46 | |||
47 | public function testConstructorIgnoresIdenticalObjects() { |
||
52 | |||
53 | public function testConstructorDoesNotIgnoreCopies() { |
||
54 | $reference = new Reference( [ new PropertyNoValueSnak( 1 ) ] ); |
||
55 | $list = new ReferenceList( [ $reference, clone $reference ] ); |
||
56 | $this->assertCount( 2, $list ); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @dataProvider invalidConstructorArgumentsProvider |
||
61 | * @expectedException InvalidArgumentException |
||
62 | */ |
||
63 | public function testGivenInvalidConstructorArguments_constructorThrowsException( $input ) { |
||
66 | |||
67 | public function invalidConstructorArgumentsProvider() { |
||
85 | |||
86 | public function testGetIterator_isTraversable() { |
||
87 | $references = new ReferenceList(); |
||
88 | $references->addNewReference( new PropertyNoValueSnak( 1 ) ); |
||
89 | $iterator = $references->getIterator(); |
||
90 | |||
91 | $this->assertInstanceOf( 'Traversable', $iterator ); |
||
92 | $this->assertCount( 1, $iterator ); |
||
93 | foreach ( $references as $reference ) { |
||
94 | $this->assertInstanceOf( 'Wikibase\DataModel\Reference', $reference ); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @dataProvider instanceProvider |
||
100 | */ |
||
101 | public function testHasReferenceBeforeRemoveButNotAfter( ReferenceList $array ) { |
||
116 | |||
117 | public function testGivenCloneOfReferenceInList_hasReferenceReturnsTrue() { |
||
118 | $list = new ReferenceList(); |
||
119 | |||
120 | $reference = new Reference( [ new PropertyNoValueSnak( 42 ) ] ); |
||
121 | $sameReference = unserialize( serialize( $reference ) ); |
||
122 | |||
123 | $list->addReference( $reference ); |
||
124 | |||
125 | $this->assertTrue( |
||
126 | $list->hasReference( $sameReference ), |
||
127 | 'hasReference should return true when a reference with the same value is present, even when its another instance' |
||
128 | ); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @dataProvider instanceProvider |
||
133 | */ |
||
134 | public function testRemoveReference( ReferenceList $array ) { |
||
149 | |||
150 | public function testRemoveReferenceRemovesIdenticalObjects() { |
||
151 | $reference = new Reference( [ new PropertyNoValueSnak( 1 ) ] ); |
||
152 | $references = new ReferenceList( [ $reference, $reference ] ); |
||
153 | |||
154 | $references->removeReference( $reference ); |
||
155 | |||
156 | $this->assertTrue( $references->isEmpty() ); |
||
157 | } |
||
158 | |||
159 | public function testRemoveReferenceDoesNotRemoveCopies() { |
||
160 | $reference = new Reference( [ new PropertyNoValueSnak( 1 ) ] ); |
||
161 | $references = new ReferenceList( [ $reference, clone $reference ] ); |
||
162 | |||
163 | $references->removeReference( $reference ); |
||
164 | |||
165 | $this->assertFalse( $references->isEmpty() ); |
||
166 | $this->assertTrue( $references->hasReference( $reference ) ); |
||
167 | $this->assertNotSame( $reference, $references->getReference( $reference->getHash() ) ); |
||
168 | } |
||
169 | |||
170 | public function testAddReferenceOnEmptyList() { |
||
181 | |||
182 | private function assertSameReferenceOrder( ReferenceList $expectedList, ReferenceList $references ) { |
||
188 | |||
189 | public function testAddReferenceAtTheEnd() { |
||
190 | $reference1 = new Reference( [ new PropertyNoValueSnak( 1 ) ] ); |
||
191 | $reference2 = new Reference( [ new PropertyNoValueSnak( 2 ) ] ); |
||
192 | $reference3 = new Reference( [ new PropertyNoValueSnak( 3 ) ] ); |
||
193 | |||
202 | |||
203 | public function testAddReferenceBetweenExistingReferences() { |
||
204 | $reference1 = new Reference( [ new PropertyNoValueSnak( 1 ) ] ); |
||
205 | $reference2 = new Reference( [ new PropertyNoValueSnak( 2 ) ] ); |
||
206 | $list = new ReferenceList( [ $reference1, $reference2 ] ); |
||
207 | |||
208 | $reference3 = new Reference( [ new PropertyNoValueSnak( 3 ) ] ); |
||
209 | $list->addReference( $reference3, 1 ); |
||
210 | |||
211 | $this->assertCount( 3, $list ); |
||
212 | $this->assertSame( 1, $list->indexOf( $reference3 ) ); |
||
213 | } |
||
214 | |||
215 | public function testAddReferenceIgnoresIdenticalObjects() { |
||
222 | |||
223 | public function testAddReferenceDoesNotIgnoreCopies() { |
||
230 | |||
231 | public function testAddReferenceAtIndexIgnoresIdenticalObjects() { |
||
238 | |||
239 | public function testAddReferenceAtIndexMovesIdenticalObjects() { |
||
270 | |||
271 | public function testAddReferenceAtIndexZero() { |
||
282 | |||
283 | public function testAddReferenceAtNegativeIndex() { |
||
290 | |||
291 | public function testGivenEmptyReference_addReferenceDoesNotAdd() { |
||
302 | |||
303 | public function testGivenEmptyReferenceAndIndex_addReferenceDoesNotAdd() { |
||
314 | |||
315 | /** |
||
316 | * @dataProvider instanceProvider |
||
317 | */ |
||
318 | public function testIndexOf( ReferenceList $array ) { |
||
326 | |||
327 | public function testIndexOf_checksForIdentity() { |
||
337 | |||
338 | /** |
||
339 | * @dataProvider instanceProvider |
||
340 | */ |
||
341 | public function testEquals( ReferenceList $array ) { |
||
345 | |||
346 | /** |
||
347 | * @dataProvider instanceProvider |
||
348 | */ |
||
349 | public function testGetValueHashReturnsString( ReferenceList $array ) { |
||
352 | |||
353 | /** |
||
354 | * @dataProvider instanceProvider |
||
355 | */ |
||
356 | public function testGetValueHashIsTheSameForClone( ReferenceList $array ) { |
||
360 | |||
361 | /** |
||
362 | * @dataProvider instanceProvider |
||
363 | */ |
||
364 | public function testHasReferenceHash( ReferenceList $references ) { |
||
374 | |||
375 | /** |
||
376 | * @dataProvider instanceProvider |
||
377 | */ |
||
378 | public function testGetReference( ReferenceList $references ) { |
||
388 | |||
389 | /** |
||
390 | * @dataProvider instanceProvider |
||
391 | */ |
||
392 | public function testRemoveReferenceHash( ReferenceList $references ) { |
||
410 | |||
411 | public function testRemoveReferenceHashRemovesIdenticalObjects() { |
||
419 | |||
420 | public function testRemoveReferenceHashDoesNotRemoveCopies() { |
||
430 | |||
431 | public function testRemoveReferenceHashUpdatesIndexes() { |
||
440 | |||
441 | public function testGivenOneSnak_addNewReferenceAddsSnak() { |
||
448 | |||
449 | public function testGivenMultipleSnaks_addNewReferenceAddsThem() { |
||
460 | |||
461 | public function testGivenAnArrayOfSnaks_addNewReferenceAddsThem() { |
||
472 | |||
473 | public function testAddNewReferenceDoesNotIgnoreIdenticalObjects() { |
||
480 | |||
481 | public function testAddNewReferenceDoesNotIgnoreCopies() { |
||
488 | |||
489 | public function testGivenNoneSnak_addNewReferenceThrowsException() { |
||
495 | |||
496 | public function testEmptySerializationStability() { |
||
500 | |||
501 | public function testSerializationStability() { |
||
512 | |||
513 | public function testSerializeUnserializeRoundtrip() { |
||
524 | |||
525 | public function testUnserializeCreatesNonIdenticalClones() { |
||
536 | |||
537 | public function testGivenEmptyList_isEmpty() { |
||
541 | |||
542 | public function testGivenNonEmptyList_isNotEmpty() { |
||
548 | |||
549 | } |
||
550 |