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() { |
||
| 256 | |||
| 257 | public function testAddReferenceAtIndexZero() { |
||
| 268 | |||
| 269 | public function testAddReferenceAtNegativeIndex() { |
||
| 276 | |||
| 277 | public function testGivenEmptyReference_addReferenceDoesNotAdd() { |
||
| 288 | |||
| 289 | public function testGivenEmptyReferenceAndIndex_addReferenceDoesNotAdd() { |
||
| 290 | $reference1 = new Reference( [ new PropertyNoValueSnak( 1 ) ] ); |
||
| 291 | $reference2 = new Reference( [ new PropertyNoValueSnak( 2 ) ] ); |
||
| 292 | $emptyReference = new Reference( [] ); |
||
| 293 | |||
| 294 | $references = new ReferenceList( [ $reference1, $reference2 ] ); |
||
| 295 | $references->addReference( $emptyReference, 0 ); |
||
| 296 | |||
| 297 | $expectedList = new ReferenceList( [ $reference1, $reference2 ] ); |
||
| 298 | $this->assertSameReferenceOrder( $expectedList, $references ); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @dataProvider instanceProvider |
||
| 303 | */ |
||
| 304 | public function testIndexOf( ReferenceList $array ) { |
||
| 305 | $this->assertFalse( $array->indexOf( new Reference() ) ); |
||
| 306 | |||
| 307 | $i = 0; |
||
| 308 | foreach ( $array as $reference ) { |
||
| 309 | $this->assertSame( $i++, $array->indexOf( $reference ) ); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | public function testIndexOf_checksForIdentity() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @dataProvider instanceProvider |
||
| 326 | */ |
||
| 327 | public function testEquals( ReferenceList $array ) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @dataProvider instanceProvider |
||
| 334 | */ |
||
| 335 | public function testGetValueHashReturnsString( ReferenceList $array ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @dataProvider instanceProvider |
||
| 341 | */ |
||
| 342 | public function testGetValueHashIsTheSameForClone( ReferenceList $array ) { |
||
| 343 | $copy = unserialize( serialize( $array ) ); |
||
| 344 | $this->assertSame( $array->getValueHash(), $copy->getValueHash() ); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @dataProvider instanceProvider |
||
| 349 | */ |
||
| 350 | public function testHasReferenceHash( ReferenceList $references ) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @dataProvider instanceProvider |
||
| 363 | */ |
||
| 364 | public function testGetReference( ReferenceList $references ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @dataProvider instanceProvider |
||
| 377 | */ |
||
| 378 | public function testRemoveReferenceHash( ReferenceList $references ) { |
||
| 396 | |||
| 397 | public function testRemoveReferenceHashRemovesIdenticalObjects() { |
||
| 405 | |||
| 406 | public function testRemoveReferenceHashDoesNotRemoveCopies() { |
||
| 416 | |||
| 417 | public function testRemoveReferenceHashUpdatesIndexes() { |
||
| 426 | |||
| 427 | public function testGivenOneSnak_addNewReferenceAddsSnak() { |
||
| 434 | |||
| 435 | public function testGivenMultipleSnaks_addNewReferenceAddsThem() { |
||
| 446 | |||
| 447 | public function testGivenAnArrayOfSnaks_addNewReferenceAddsThem() { |
||
| 458 | |||
| 459 | public function testAddNewReferenceDoesNotIgnoreIdenticalObjects() { |
||
| 466 | |||
| 467 | public function testAddNewReferenceDoesNotIgnoreCopies() { |
||
| 474 | |||
| 475 | public function testGivenNoneSnak_addNewReferenceThrowsException() { |
||
| 481 | |||
| 482 | public function testEmptySerializationStability() { |
||
| 486 | |||
| 487 | public function testSerializationStability() { |
||
| 498 | |||
| 499 | public function testSerializeUnserializeRoundtrip() { |
||
| 510 | |||
| 511 | public function testUnserializeCreatesNonIdenticalClones() { |
||
| 522 | |||
| 523 | public function testGivenEmptyList_isEmpty() { |
||
| 527 | |||
| 528 | public function testGivenNonEmptyList_isNotEmpty() { |
||
| 534 | |||
| 535 | } |
||
| 536 |