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( array( new PropertyNoValueSnak( 1 ) ) ); |
||
40 | $original = new ReferenceList( array( $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() { |
||
48 | $reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
49 | $list = new ReferenceList( array( $reference, $reference ) ); |
||
50 | $this->assertCount( 1, $list ); |
||
51 | } |
||
52 | |||
53 | public function testConstructorDoesNotIgnoreCopies() { |
||
54 | $reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
55 | $list = new ReferenceList( array( $reference, clone $reference ) ); |
||
56 | $this->assertCount( 2, $list ); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @dataProvider invalidConstructorArgumentsProvider |
||
61 | * @expectedException InvalidArgumentException |
||
62 | */ |
||
63 | public function testGivenInvalidConstructorArguments_constructorThrowsException( $input ) { |
||
64 | new ReferenceList( $input ); |
||
65 | } |
||
66 | |||
67 | public function invalidConstructorArgumentsProvider() { |
||
68 | $id1 = new PropertyId( 'P1' ); |
||
69 | |||
70 | return array( |
||
71 | array( null ), |
||
72 | array( false ), |
||
73 | array( 1 ), |
||
74 | array( 0.1 ), |
||
75 | array( 'string' ), |
||
76 | array( $id1 ), |
||
77 | array( new PropertyNoValueSnak( $id1 ) ), |
||
78 | array( new Reference() ), |
||
79 | array( new SnakList( array( new PropertyNoValueSnak( $id1 ) ) ) ), |
||
80 | array( array( new PropertyNoValueSnak( $id1 ) ) ), |
||
81 | array( array( new ReferenceList() ) ), |
||
82 | array( array( new SnakList() ) ), |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | public function testGetIterator_isTraversable() { |
||
97 | |||
98 | /** |
||
99 | * @dataProvider instanceProvider |
||
100 | */ |
||
101 | public function testHasReferenceBeforeRemoveButNotAfter( ReferenceList $array ) { |
||
102 | if ( $array->count() === 0 ) { |
||
103 | $this->assertTrue( true ); |
||
104 | return; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @var Reference $hashable |
||
109 | */ |
||
110 | foreach ( iterator_to_array( $array ) as $hashable ) { |
||
111 | $this->assertTrue( $array->hasReference( $hashable ) ); |
||
112 | $array->removeReference( $hashable ); |
||
113 | $this->assertFalse( $array->hasReference( $hashable ) ); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | public function testGivenCloneOfReferenceInList_hasReferenceReturnsTrue() { |
||
130 | |||
131 | /** |
||
132 | * @dataProvider instanceProvider |
||
133 | */ |
||
134 | public function testRemoveReference( ReferenceList $array ) { |
||
135 | $elementCount = count( $array ); |
||
136 | |||
137 | /** |
||
138 | * @var Reference $element |
||
139 | */ |
||
140 | foreach ( iterator_to_array( $array ) as $element ) { |
||
141 | $this->assertTrue( $array->hasReference( $element ) ); |
||
142 | |||
143 | $array->removeReference( $element ); |
||
144 | |||
145 | $this->assertFalse( $array->hasReference( $element ) ); |
||
146 | $this->assertEquals( --$elementCount, count( $array ) ); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | public function testRemoveReferenceRemovesIdenticalObjects() { |
||
158 | |||
159 | public function testRemoveReferenceDoesNotRemoveCopies() { |
||
169 | |||
170 | public function testAddReferenceOnEmptyList() { |
||
171 | $reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
172 | |||
173 | $references = new ReferenceList(); |
||
174 | $references->addReference( $reference ); |
||
175 | |||
176 | $this->assertCount( 1, $references ); |
||
177 | |||
178 | $expectedList = new ReferenceList( array( $reference ) ); |
||
179 | $this->assertSameReferenceOrder( $expectedList, $references ); |
||
180 | } |
||
181 | |||
182 | private function assertSameReferenceOrder( ReferenceList $expectedList, ReferenceList $references ) { |
||
183 | $this->assertEquals( |
||
184 | iterator_to_array( $expectedList ), |
||
185 | iterator_to_array( $references ) |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | public function testAddReferenceOnNonEmptyList() { |
||
190 | $reference1 = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
191 | $reference2 = new Reference( array( new PropertyNoValueSnak( 2 ) ) ); |
||
192 | $reference3 = new Reference( array( new PropertyNoValueSnak( 3 ) ) ); |
||
193 | |||
194 | $references = new ReferenceList( array( $reference1, $reference2 ) ); |
||
195 | $references->addReference( $reference3 ); |
||
196 | |||
197 | $this->assertCount( 3, $references ); |
||
198 | |||
199 | $expectedList = new ReferenceList( array( $reference1, $reference2, $reference3 ) ); |
||
200 | $this->assertSameReferenceOrder( $expectedList, $references ); |
||
201 | } |
||
202 | |||
203 | public function testAddReferenceIgnoresIdenticalObjects() { |
||
210 | |||
211 | public function testAddReferenceDoesNotIgnoreCopies() { |
||
212 | $list = new ReferenceList(); |
||
213 | $reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
214 | $list->addReference( $reference ); |
||
215 | $list->addReference( clone $reference ); |
||
216 | $this->assertCount( 2, $list ); |
||
217 | } |
||
218 | |||
219 | public function testAddReferenceAtIndexIgnoresIdenticalObjects() { |
||
226 | |||
227 | public function testAddReferenceAtIndexMovesIdenticalObjects() { |
||
228 | $list = new ReferenceList(); |
||
244 | |||
245 | public function testAddReferenceAtIndexZero() { |
||
246 | $reference1 = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
247 | $reference2 = new Reference( array( new PropertyNoValueSnak( 2 ) ) ); |
||
248 | $reference3 = new Reference( array( new PropertyNoValueSnak( 3 ) ) ); |
||
249 | |||
250 | $references = new ReferenceList( array( $reference1, $reference2 ) ); |
||
251 | $references->addReference( $reference3, 0 ); |
||
252 | |||
253 | $expectedList = new ReferenceList( array( $reference3, $reference1, $reference2 ) ); |
||
254 | $this->assertSameReferenceOrder( $expectedList, $references ); |
||
255 | } |
||
256 | |||
257 | public function testAddReferenceAtNegativeIndex() { |
||
264 | |||
265 | public function testGivenEmptyReference_addReferenceDoesNotAdd() { |
||
276 | |||
277 | public function testGivenEmptyReferenceAndIndex_addReferenceDoesNotAdd() { |
||
288 | |||
289 | /** |
||
290 | * @dataProvider instanceProvider |
||
291 | */ |
||
292 | public function testIndexOf( ReferenceList $array ) { |
||
293 | $this->assertFalse( $array->indexOf( new Reference() ) ); |
||
294 | |||
295 | $i = 0; |
||
296 | foreach ( $array as $reference ) { |
||
297 | $this->assertEquals( $i++, $array->indexOf( $reference ) ); |
||
298 | } |
||
299 | } |
||
300 | |||
301 | public function testIndexOf_checksForIdentity() { |
||
311 | |||
312 | /** |
||
313 | * @dataProvider instanceProvider |
||
314 | */ |
||
315 | public function testEquals( ReferenceList $array ) { |
||
319 | |||
320 | /** |
||
321 | * @dataProvider instanceProvider |
||
322 | */ |
||
323 | public function testGetValueHashReturnsString( ReferenceList $array ) { |
||
324 | $this->assertInternalType( 'string', $array->getValueHash() ); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @dataProvider instanceProvider |
||
329 | */ |
||
330 | public function testGetValueHashIsTheSameForClone( ReferenceList $array ) { |
||
331 | $copy = unserialize( serialize( $array ) ); |
||
332 | $this->assertEquals( $array->getValueHash(), $copy->getValueHash() ); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @dataProvider instanceProvider |
||
337 | */ |
||
338 | public function testHasReferenceHash( ReferenceList $references ) { |
||
348 | |||
349 | /** |
||
350 | * @dataProvider instanceProvider |
||
351 | */ |
||
352 | public function testGetReference( ReferenceList $references ) { |
||
362 | |||
363 | /** |
||
364 | * @dataProvider instanceProvider |
||
365 | */ |
||
366 | public function testRemoveReferenceHash( ReferenceList $references ) { |
||
384 | |||
385 | public function testRemoveReferenceHashRemovesIdenticalObjects() { |
||
393 | |||
394 | public function testRemoveReferenceHashDoesNotRemoveCopies() { |
||
404 | |||
405 | public function testRemoveReferenceHashUpdatesIndexes() { |
||
414 | |||
415 | public function testGivenOneSnak_addNewReferenceAddsSnak() { |
||
416 | $references = new ReferenceList(); |
||
417 | $snak = new PropertyNoValueSnak( 1 ); |
||
418 | |||
419 | $references->addNewReference( $snak ); |
||
420 | $this->assertTrue( $references->hasReference( new Reference( array( $snak ) ) ) ); |
||
421 | } |
||
422 | |||
423 | public function testGivenMultipleSnaks_addNewReferenceAddsThem() { |
||
424 | $references = new ReferenceList(); |
||
425 | $snak1 = new PropertyNoValueSnak( 1 ); |
||
426 | $snak2 = new PropertyNoValueSnak( 3 ); |
||
427 | $snak3 = new PropertyNoValueSnak( 2 ); |
||
428 | |||
429 | $references->addNewReference( $snak1, $snak2, $snak3 ); |
||
430 | |||
431 | $expectedSnaks = array( $snak1, $snak2, $snak3 ); |
||
432 | $this->assertTrue( $references->hasReference( new Reference( $expectedSnaks ) ) ); |
||
433 | } |
||
434 | |||
435 | public function testGivenAnArrayOfSnaks_addNewReferenceAddsThem() { |
||
446 | |||
447 | public function testAddNewReferenceDoesNotIgnoreIdenticalObjects() { |
||
454 | |||
455 | public function testAddNewReferenceDoesNotIgnoreCopies() { |
||
462 | |||
463 | public function testGivenNoneSnak_addNewReferenceThrowsException() { |
||
464 | $references = new ReferenceList(); |
||
465 | |||
466 | $this->setExpectedException( 'InvalidArgumentException' ); |
||
467 | $references->addNewReference( new PropertyNoValueSnak( 1 ), null ); |
||
468 | } |
||
469 | |||
470 | public function testEmptySerializationStability() { |
||
474 | |||
475 | public function testSerializationStability() { |
||
486 | |||
487 | public function testSerializeUnserializeRoundtrip() { |
||
498 | |||
499 | public function testUnserializeCreatesNonIdenticalClones() { |
||
510 | |||
511 | public function testGivenEmptyList_isEmpty() { |
||
512 | $references = new ReferenceList(); |
||
513 | $this->assertTrue( $references->isEmpty() ); |
||
514 | } |
||
515 | |||
516 | public function testGivenNonEmptyList_isNotEmpty() { |
||
517 | $references = new ReferenceList(); |
||
518 | $references->addNewReference( new PropertyNoValueSnak( 1 ) ); |
||
519 | |||
520 | $this->assertFalse( $references->isEmpty() ); |
||
521 | } |
||
522 | |||
523 | } |
||
524 |
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.