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_falseForMissingReferences() { |
||
| 250 | $referenceList = new ReferenceList(); |
||
| 251 | $reference = new Reference( array( new PropertyNoValueSnak( 1 ) ) ); |
||
| 252 | |||
| 253 | $referenceList->addNewReference( new PropertyNoValueSnak( 1 ) ); |
||
| 254 | |||
| 255 | $this->assertFalse( $referenceList->indexOf( new Reference() ) ); |
||
| 256 | $this->assertFalse( $referenceList->indexOf( $reference ) ); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @dataProvider instanceProvider |
||
| 261 | */ |
||
| 262 | public function testEquals( ReferenceList $array ) { |
||
| 263 | $this->assertTrue( $array->equals( $array ) ); |
||
| 264 | $this->assertFalse( $array->equals( 42 ) ); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @dataProvider instanceProvider |
||
| 269 | */ |
||
| 270 | public function testGetValueHashReturnsString( ReferenceList $array ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @dataProvider instanceProvider |
||
| 276 | */ |
||
| 277 | public function testGetValueHashIsTheSameForClone( ReferenceList $array ) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @dataProvider instanceProvider |
||
| 284 | */ |
||
| 285 | public function testHasReferenceHash( ReferenceList $references ) { |
||
| 286 | $this->assertFalse( $references->hasReferenceHash( '~=[,,_,,]:3' ) ); |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @var Hashable $reference |
||
| 290 | */ |
||
| 291 | foreach ( $references as $reference ) { |
||
| 292 | $this->assertTrue( $references->hasReferenceHash( $reference->getHash() ) ); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @dataProvider instanceProvider |
||
| 298 | */ |
||
| 299 | public function testGetReference( ReferenceList $references ) { |
||
| 300 | $this->assertNull( $references->getReference( '~=[,,_,,]:3' ) ); |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @var Reference $reference |
||
| 304 | */ |
||
| 305 | foreach ( $references as $reference ) { |
||
| 306 | $this->assertTrue( $reference->equals( $references->getReference( $reference->getHash() ) ) ); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @dataProvider instanceProvider |
||
| 312 | */ |
||
| 313 | public function testRemoveReferenceHash( ReferenceList $references ) { |
||
| 331 | |||
| 332 | public function testGivenOneSnak_addNewReferenceAddsSnak() { |
||
| 339 | |||
| 340 | public function testGivenMultipleSnaks_addNewReferenceAddsThem() { |
||
| 351 | |||
| 352 | public function testGivenAnArrayOfSnaks_addNewReferenceAddsThem() { |
||
| 363 | |||
| 364 | public function testGivenNoneSnak_addNewReferenceThrowsException() { |
||
| 370 | |||
| 371 | public function testSerializationStability() { |
||
| 372 | $references = new ReferenceList(); |
||
| 373 | $this->assertSame( 'a:0:{}', $references->serialize() ); |
||
| 374 | } |
||
| 375 | |||
| 376 | public function testSerializeRoundtrip() { |
||
| 389 | |||
| 390 | public function testGivenEmptyList_isEmpty() { |
||
| 394 | |||
| 395 | public function testGivenNonEmptyList_isNotEmpty() { |
||
| 401 | |||
| 402 | } |
||
| 403 |
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: