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() { |
||
28 | return array( |
||
29 | array( new ReferenceList( array() ) ), |
||
30 | array( new ReferenceList( array( |
||
31 | new Reference(), |
||
32 | new Reference( array( new PropertyNoValueSnak( 2 ) ) ), |
||
33 | new Reference( array( new PropertyNoValueSnak( 3 ) ) ), |
||
34 | ) ) ), |
||
35 | ); |
||
36 | } |
||
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 testConstructorDoesNotIgnoreCopies() { |
||
48 | $reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
49 | $list = new ReferenceList( array( $reference, clone $reference ) ); |
||
50 | $this->assertCount( 2, $list ); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @dataProvider invalidConstructorArgumentsProvider |
||
55 | * @expectedException InvalidArgumentException |
||
56 | */ |
||
57 | public function testGivenInvalidConstructorArguments_constructorThrowsException( $input ) { |
||
58 | new ReferenceList( $input ); |
||
59 | } |
||
60 | |||
61 | public function invalidConstructorArgumentsProvider() { |
||
62 | $id1 = new PropertyId( 'P1' ); |
||
63 | |||
64 | return array( |
||
65 | array( null ), |
||
66 | array( false ), |
||
67 | array( 1 ), |
||
68 | array( 0.1 ), |
||
69 | array( 'string' ), |
||
70 | array( $id1 ), |
||
71 | array( new PropertyNoValueSnak( $id1 ) ), |
||
72 | array( new Reference() ), |
||
73 | array( new SnakList( array( new PropertyNoValueSnak( $id1 ) ) ) ), |
||
74 | array( array( new PropertyNoValueSnak( $id1 ) ) ), |
||
75 | array( array( new ReferenceList() ) ), |
||
76 | array( array( new SnakList() ) ), |
||
77 | ); |
||
78 | } |
||
79 | |||
80 | public function testGetIterator_isTraversable() { |
||
81 | $references = new ReferenceList(); |
||
82 | $references->addNewReference( new PropertyNoValueSnak( 1 ) ); |
||
83 | $iterator = $references->getIterator(); |
||
84 | |||
85 | $this->assertInstanceOf( 'Traversable', $iterator ); |
||
86 | $this->assertCount( 1, $iterator ); |
||
87 | foreach ( $references as $reference ) { |
||
88 | $this->assertInstanceOf( 'Wikibase\DataModel\Reference', $reference ); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @dataProvider instanceProvider |
||
94 | */ |
||
95 | public function testHasReferenceBeforeRemoveButNotAfter( ReferenceList $array ) { |
||
96 | if ( $array->count() === 0 ) { |
||
97 | $this->assertTrue( true ); |
||
98 | return; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @var Reference $hashable |
||
103 | */ |
||
104 | foreach ( iterator_to_array( $array ) as $hashable ) { |
||
105 | $this->assertTrue( $array->hasReference( $hashable ) ); |
||
106 | $array->removeReference( $hashable ); |
||
107 | $this->assertFalse( $array->hasReference( $hashable ) ); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | public function testGivenCloneOfReferenceInList_hasReferenceReturnsTrue() { |
||
112 | $list = new ReferenceList(); |
||
113 | |||
114 | $reference = new Reference( array( new PropertyNoValueSnak( 42 ) ) ); |
||
115 | $sameReference = unserialize( serialize( $reference ) ); |
||
116 | |||
117 | $list->addReference( $reference ); |
||
118 | |||
119 | $this->assertTrue( |
||
120 | $list->hasReference( $sameReference ), |
||
121 | 'hasReference should return true when a reference with the same value is present, even when its another instance' |
||
122 | ); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @dataProvider instanceProvider |
||
127 | */ |
||
128 | public function testRemoveReference( ReferenceList $array ) { |
||
129 | $elementCount = count( $array ); |
||
130 | |||
131 | /** |
||
132 | * @var Reference $element |
||
133 | */ |
||
134 | foreach ( iterator_to_array( $array ) as $element ) { |
||
135 | $this->assertTrue( $array->hasReference( $element ) ); |
||
136 | |||
137 | $array->removeReference( $element ); |
||
138 | |||
139 | $this->assertFalse( $array->hasReference( $element ) ); |
||
140 | $this->assertEquals( --$elementCount, count( $array ) ); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | public function testRemoveReferenceRemovesIdenticalObjects() { |
||
145 | $reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
146 | $references = new ReferenceList( array( $reference, $reference ) ); |
||
147 | |||
148 | $references->removeReference( $reference ); |
||
149 | |||
150 | $this->assertTrue( $references->isEmpty() ); |
||
151 | } |
||
152 | |||
153 | public function testRemoveReferenceDoesNotRemoveCopies() { |
||
154 | $reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
155 | $references = new ReferenceList( array( $reference, clone $reference ) ); |
||
156 | |||
157 | $references->removeReference( $reference ); |
||
158 | |||
159 | $this->assertFalse( $references->isEmpty() ); |
||
160 | $this->assertTrue( $references->hasReference( $reference ) ); |
||
161 | $this->assertNotSame( $reference, $references->getReference( $reference->getHash() ) ); |
||
162 | } |
||
163 | |||
164 | public function testAddReferenceOnEmptyList() { |
||
165 | $reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
166 | |||
167 | $references = new ReferenceList(); |
||
168 | $references->addReference( $reference ); |
||
169 | |||
170 | $this->assertCount( 1, $references ); |
||
171 | |||
172 | $expectedList = new ReferenceList( array( $reference ) ); |
||
173 | $this->assertSameReferenceOrder( $expectedList, $references ); |
||
174 | } |
||
175 | |||
176 | private function assertSameReferenceOrder( ReferenceList $expectedList, ReferenceList $references ) { |
||
177 | $this->assertEquals( |
||
178 | iterator_to_array( $expectedList ), |
||
179 | iterator_to_array( $references ) |
||
180 | ); |
||
181 | } |
||
182 | |||
183 | public function testAddReferenceOnNonEmptyList() { |
||
184 | $reference1 = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
185 | $reference2 = new Reference( array( new PropertyNoValueSnak( 2 ) ) ); |
||
186 | $reference3 = new Reference( array( new PropertyNoValueSnak( 3 ) ) ); |
||
187 | |||
188 | $references = new ReferenceList( array( $reference1, $reference2 ) ); |
||
189 | $references->addReference( $reference3 ); |
||
190 | |||
191 | $this->assertCount( 3, $references ); |
||
192 | |||
193 | $expectedList = new ReferenceList( array( $reference1, $reference2, $reference3 ) ); |
||
194 | $this->assertSameReferenceOrder( $expectedList, $references ); |
||
195 | } |
||
196 | |||
197 | public function testAddReferenceDoesNotIgnoreCopies() { |
||
198 | $list = new ReferenceList(); |
||
199 | $reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
200 | $list->addReference( $reference ); |
||
201 | $list->addReference( clone $reference ); |
||
202 | $this->assertCount( 2, $list ); |
||
203 | } |
||
204 | |||
205 | public function testAddReferenceAtIndexZero() { |
||
216 | |||
217 | public function testAddReferenceAtNegativeIndex() { |
||
224 | |||
225 | public function testGivenEmptyReference_addReferenceDoesNotAdd() { |
||
236 | |||
237 | public function testGivenEmptyReferenceAndIndex_addReferenceDoesNotAdd() { |
||
248 | |||
249 | /** |
||
250 | * @dataProvider instanceProvider |
||
251 | */ |
||
252 | public function testIndexOf( ReferenceList $array ) { |
||
260 | |||
261 | public function testIndexOf_checksForIdentity() { |
||
271 | |||
272 | /** |
||
273 | * @dataProvider instanceProvider |
||
274 | */ |
||
275 | public function testEquals( ReferenceList $array ) { |
||
279 | |||
280 | /** |
||
281 | * @dataProvider instanceProvider |
||
282 | */ |
||
283 | public function testGetValueHashReturnsString( ReferenceList $array ) { |
||
286 | |||
287 | /** |
||
288 | * @dataProvider instanceProvider |
||
289 | */ |
||
290 | public function testGetValueHashIsTheSameForClone( ReferenceList $array ) { |
||
294 | |||
295 | /** |
||
296 | * @dataProvider instanceProvider |
||
297 | */ |
||
298 | public function testHasReferenceHash( ReferenceList $references ) { |
||
308 | |||
309 | /** |
||
310 | * @dataProvider instanceProvider |
||
311 | */ |
||
312 | public function testGetReference( ReferenceList $references ) { |
||
322 | |||
323 | /** |
||
324 | * @dataProvider instanceProvider |
||
325 | */ |
||
326 | public function testRemoveReferenceHash( ReferenceList $references ) { |
||
327 | $references->removeReferenceHash( '~=[,,_,,]:3' ); |
||
328 | |||
329 | $hashes = array(); |
||
330 | |||
331 | /** |
||
332 | * @var Reference $reference |
||
333 | */ |
||
334 | foreach ( $references as $reference ) { |
||
335 | $hashes[] = $reference->getHash(); |
||
344 | |||
345 | public function testRemoveReferenceHashRemovesIdenticalObjects() { |
||
353 | |||
354 | public function testRemoveReferenceHashDoesNotRemoveCopies() { |
||
364 | |||
365 | public function testRemoveReferenceHashUpdatesIndexes() { |
||
374 | |||
375 | public function testGivenOneSnak_addNewReferenceAddsSnak() { |
||
382 | |||
383 | public function testGivenMultipleSnaks_addNewReferenceAddsThem() { |
||
394 | |||
395 | public function testGivenAnArrayOfSnaks_addNewReferenceAddsThem() { |
||
396 | $references = new ReferenceList(); |
||
406 | |||
407 | public function testAddNewReferenceDoesNotIgnoreIdenticalObjects() { |
||
414 | |||
415 | public function testAddNewReferenceDoesNotIgnoreCopies() { |
||
422 | |||
423 | public function testGivenNoneSnak_addNewReferenceThrowsException() { |
||
429 | |||
430 | public function testEmptySerializationStability() { |
||
434 | |||
435 | public function testSerializationStability() { |
||
446 | |||
447 | public function testSerializeUnserializeRoundtrip() { |
||
458 | |||
459 | public function testUnserializeCreatesNonIdenticalClones() { |
||
470 | |||
471 | public function testGivenEmptyList_isEmpty() { |
||
475 | |||
476 | public function testGivenNonEmptyList_isNotEmpty() { |
||
482 | |||
483 | } |
||
484 |
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.