Complex classes like AliasGroupListTest 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 AliasGroupListTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class AliasGroupListTest extends PHPUnit_Framework_TestCase { |
||
| 17 | |||
| 18 | public function testIsEmpty() { |
||
| 25 | |||
| 26 | public function testGivenNoTerms_sizeIsZero() { |
||
| 30 | |||
| 31 | public function testGivenTwoTerms_countReturnsTwo() { |
||
| 36 | |||
| 37 | private function getTwoGroups() { |
||
| 43 | |||
| 44 | public function testGivenTwoGroups_listContainsThem() { |
||
| 51 | |||
| 52 | public function testGivenGroupsWithTheSameLanguage_onlyTheLastOnesAreRetained() { |
||
| 53 | $array = [ |
||
| 54 | new AliasGroup( 'en', [ 'foo' ] ), |
||
| 55 | new AliasGroup( 'en', [ 'bar' ] ), |
||
| 56 | |||
| 57 | new AliasGroup( 'de', [ 'baz' ] ), |
||
| 58 | |||
| 59 | new AliasGroup( 'nl', [ 'bah' ] ), |
||
| 60 | new AliasGroup( 'nl', [ 'blah' ] ), |
||
| 61 | new AliasGroup( 'nl', [ 'spam' ] ), |
||
| 62 | ]; |
||
| 63 | |||
| 64 | $list = new AliasGroupList( $array ); |
||
| 65 | |||
| 66 | $this->assertEquals( |
||
| 67 | [ |
||
| 68 | 'en' => new AliasGroup( 'en', [ 'bar' ] ), |
||
| 69 | 'de' => new AliasGroup( 'de', [ 'baz' ] ), |
||
| 70 | 'nl' => new AliasGroup( 'nl', [ 'spam' ] ), |
||
| 71 | ], |
||
| 72 | iterator_to_array( $list ) |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function testCanIterateOverList() { |
||
| 89 | |||
| 90 | public function testGivenNonAliasGroups_constructorThrowsException() { |
||
| 94 | |||
| 95 | public function testGivenSetLanguageCode_getByLanguageReturnsGroup() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @dataProvider invalidLanguageCodeProvider |
||
| 109 | */ |
||
| 110 | public function testGivenInvalidLanguageCode_getByLanguageThrowsException( $languageCode ) { |
||
| 111 | $list = new AliasGroupList(); |
||
| 112 | $this->setExpectedException( 'OutOfBoundsException' ); |
||
| 113 | $list->getByLanguage( $languageCode ); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function testGivenNonSetLanguageCode_getByLanguageThrowsException() { |
||
| 122 | |||
| 123 | public function testGivenGroupForNewLanguage_setGroupAddsGroup() { |
||
| 134 | |||
| 135 | public function testGivenLabelForExistingLanguage_setLabelReplacesLabel() { |
||
| 145 | |||
| 146 | public function testGivenNotSetLanguage_removeByLanguageIsNoOp() { |
||
| 154 | |||
| 155 | public function testGivenSetLanguage_removeByLanguageRemovesIt() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @dataProvider invalidLanguageCodeProvider |
||
| 165 | */ |
||
| 166 | public function testGivenInvalidLanguageCode_removeByLanguageIsNoOp( $languageCode ) { |
||
| 167 | $list = new AliasGroupList( [ new AliasGroup( 'en', [ 'foo' ] ) ] ); |
||
| 168 | $list->removeByLanguage( $languageCode ); |
||
| 169 | $this->assertFalse( $list->isEmpty() ); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function testGivenEmptyGroups_constructorRemovesThem() { |
||
| 188 | |||
| 189 | public function testGivenEmptyGroup_setGroupRemovesGroup() { |
||
| 201 | |||
| 202 | public function testEmptyListEqualsEmptyList() { |
||
| 206 | |||
| 207 | public function testFilledListEqualsItself() { |
||
| 216 | |||
| 217 | public function testDifferentListsDoNotEqual() { |
||
| 233 | |||
| 234 | public function testGivenNonAliasGroupList_equalsReturnsFalse() { |
||
| 239 | |||
| 240 | public function testGivenListsThatOnlyDifferInOrder_equalsReturnsTrue() { |
||
| 253 | |||
| 254 | public function testGivenNonSetLanguageGroup_hasAliasGroupReturnsFalse() { |
||
| 258 | |||
| 259 | public function testGivenMismatchingGroup_hasAliasGroupReturnsFalse() { |
||
| 263 | |||
| 264 | public function testGivenMatchingGroup_hasAliasGroupReturnsTrue() { |
||
| 268 | |||
| 269 | public function testGivenNonSetLanguageGroup_hasGroupForLanguageReturnsFalse() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @dataProvider invalidLanguageCodeProvider |
||
| 276 | */ |
||
| 277 | public function testGivenInvalidLanguageCode_hasGroupForLanguageReturnsFalse( $languageCode ) { |
||
| 278 | $list = new AliasGroupList(); |
||
| 279 | $this->assertFalse( $list->hasGroupForLanguage( $languageCode ) ); |
||
| 280 | } |
||
| 281 | |||
| 282 | public function invalidLanguageCodeProvider() { |
||
| 289 | |||
| 290 | public function testGivenMismatchingGroup_hasGroupForLanguageReturnsFalse() { |
||
| 294 | |||
| 295 | public function testGivenMatchingGroup_hasGroupForLanguageReturnsTrue() { |
||
| 299 | |||
| 300 | public function testGivenAliasGroupArgs_setGroupTextsSetsAliasGroup() { |
||
| 310 | |||
| 311 | public function testGivenInvalidLanguageCode_setGroupTextsThrowsException() { |
||
| 317 | |||
| 318 | public function testGivenInvalidAliases_setGroupTextsThrowsException() { |
||
| 324 | |||
| 325 | public function testToArray() { |
||
| 336 | |||
| 337 | public function testGivenEmptyList_getWithLanguagesReturnsEmptyList() { |
||
| 342 | |||
| 343 | public function testGivenNoLanguages_getWithLanguagesReturnsEmptyList() { |
||
| 350 | |||
| 351 | public function testGivenAllLanguages_getWithLanguagesReturnsFullList() { |
||
| 358 | |||
| 359 | public function testGivenSomeLanguages_getWithLanguagesReturnsPartialList() { |
||
| 372 | |||
| 373 | public function testToTextArray() { |
||
| 385 | |||
| 386 | public function testClear() { |
||
| 387 | $list = new AliasGroupList(); |
||
| 388 | $list->setAliasesForLanguage( 'en', [ 'foo', 'baz' ] ); |
||
| 389 | $list->setAliasesForLanguage( 'de', [ 'bar' ] ); |
||
| 395 | |||
| 396 | } |
||
| 397 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: