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() { |
||
19 | $list = new AliasGroupList(); |
||
20 | $this->assertTrue( $list->isEmpty() ); |
||
21 | |||
22 | $list = new AliasGroupList( array( new AliasGroup( 'en', array( 'foo' ) ) ) ); |
||
23 | $this->assertFalse( $list->isEmpty() ); |
||
24 | } |
||
25 | |||
26 | public function testGivenNoTerms_sizeIsZero() { |
||
30 | |||
31 | public function testGivenTwoTerms_countReturnsTwo() { |
||
32 | $list = new AliasGroupList( $this->getTwoGroups() ); |
||
33 | |||
34 | $this->assertCount( 2, $list ); |
||
35 | } |
||
36 | |||
37 | private function getTwoGroups() { |
||
38 | return array( |
||
39 | 'en' => new AliasGroup( 'en', array( 'foo' ) ), |
||
40 | 'de' => new AliasGroup( 'de', array( 'bar', 'baz' ) ), |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | public function testGivenTwoGroups_listContainsThem() { |
||
45 | $array = $this->getTwoGroups(); |
||
46 | |||
47 | $list = new AliasGroupList( $array ); |
||
48 | |||
49 | $this->assertEquals( $array, iterator_to_array( $list ) ); |
||
50 | } |
||
51 | |||
52 | public function testGivenGroupsWithTheSameLanguage_onlyTheLastOnesAreRetained() { |
||
53 | $array = array( |
||
54 | new AliasGroup( 'en', array( 'foo' ) ), |
||
55 | new AliasGroup( 'en', array( 'bar' ) ), |
||
56 | |||
57 | new AliasGroup( 'de', array( 'baz' ) ), |
||
58 | |||
59 | new AliasGroup( 'nl', array( 'bah' ) ), |
||
60 | new AliasGroup( 'nl', array( 'blah' ) ), |
||
61 | new AliasGroup( 'nl', array( 'spam' ) ), |
||
62 | ); |
||
63 | |||
64 | $list = new AliasGroupList( $array ); |
||
65 | |||
66 | $this->assertEquals( |
||
67 | array( |
||
68 | 'en' => new AliasGroup( 'en', array( 'bar' ) ), |
||
69 | 'de' => new AliasGroup( 'de', array( 'baz' ) ), |
||
70 | 'nl' => new AliasGroup( 'nl', array( 'spam' ) ), |
||
71 | ), |
||
72 | iterator_to_array( $list ) |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | public function testCanIterateOverList() { |
||
77 | $group = new AliasGroup( 'en', array( 'foo' ) ); |
||
78 | |||
79 | $list = new AliasGroupList( array( $group ) ); |
||
80 | |||
81 | /** |
||
82 | * @var AliasGroup $aliasGroup |
||
83 | */ |
||
84 | foreach ( $list as $key => $aliasGroup ) { |
||
85 | $this->assertEquals( $group, $aliasGroup ); |
||
86 | $this->assertEquals( $aliasGroup->getLanguageCode(), $key ); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | public function testGivenNonAliasGroups_constructorThrowsException() { |
||
94 | |||
95 | public function testGivenSetLanguageCode_getByLanguageReturnsGroup() { |
||
96 | $enGroup = new AliasGroup( 'en', array( 'foo' ) ); |
||
97 | |||
98 | $list = new AliasGroupList( array( |
||
99 | new AliasGroup( 'de' ), |
||
100 | $enGroup, |
||
101 | new AliasGroup( 'nl' ), |
||
102 | ) ); |
||
103 | |||
104 | $this->assertEquals( $enGroup, $list->getByLanguage( 'en' ) ); |
||
105 | } |
||
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() { |
||
117 | $list = new AliasGroupList(); |
||
118 | |||
119 | $this->setExpectedException( 'OutOfBoundsException' ); |
||
120 | $list->getByLanguage( 'en' ); |
||
121 | } |
||
122 | |||
123 | public function testGivenGroupForNewLanguage_setGroupAddsGroup() { |
||
124 | $enGroup = new AliasGroup( 'en', array( 'foo', 'bar' ) ); |
||
125 | $deGroup = new AliasGroup( 'de', array( 'baz', 'bah' ) ); |
||
126 | |||
127 | $list = new AliasGroupList( array( $enGroup ) ); |
||
128 | $expectedList = new AliasGroupList( array( $enGroup, $deGroup ) ); |
||
129 | |||
130 | $list->setGroup( $deGroup ); |
||
131 | |||
132 | $this->assertEquals( $expectedList, $list ); |
||
133 | } |
||
134 | |||
135 | public function testGivenLabelForExistingLanguage_setLabelReplacesLabel() { |
||
136 | $enGroup = new AliasGroup( 'en', array( 'foo', 'bar' ) ); |
||
137 | $newEnGroup = new AliasGroup( 'en', array( 'foo', 'bar', 'bah' ) ); |
||
138 | |||
139 | $list = new AliasGroupList( array( $enGroup ) ); |
||
140 | $expectedList = new AliasGroupList( array( $newEnGroup ) ); |
||
141 | |||
142 | $list->setGroup( $newEnGroup ); |
||
143 | $this->assertEquals( $expectedList, $list ); |
||
144 | } |
||
145 | |||
146 | public function testGivenNotSetLanguage_removeByLanguageIsNoOp() { |
||
147 | $list = new AliasGroupList( array( new AliasGroup( 'en', array( 'foo', 'bar' ) ) ) ); |
||
148 | $originalList = clone $list; |
||
149 | |||
150 | $list->removeByLanguage( 'de' ); |
||
151 | |||
152 | $this->assertEquals( $originalList, $list ); |
||
153 | } |
||
154 | |||
155 | public function testGivenSetLanguage_removeByLanguageRemovesIt() { |
||
156 | $list = new AliasGroupList( array( new AliasGroup( 'en', array( 'foo', 'bar' ) ) ) ); |
||
157 | |||
158 | $list->removeByLanguage( 'en' ); |
||
159 | |||
160 | $this->assertEquals( new AliasGroupList(), $list ); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @dataProvider invalidLanguageCodeProvider |
||
165 | */ |
||
166 | public function testGivenInvalidLanguageCode_removeByLanguageIsNoOp( $languageCode ) { |
||
167 | $list = new AliasGroupList( array( new AliasGroup( 'en', array( 'foo' ) ) ) ); |
||
168 | $list->removeByLanguage( $languageCode ); |
||
169 | $this->assertFalse( $list->isEmpty() ); |
||
170 | } |
||
171 | |||
172 | public function testGivenEmptyGroups_constructorRemovesThem() { |
||
173 | $enGroup = new AliasGroup( 'en', array( 'foo' ) ); |
||
174 | |||
175 | $list = new AliasGroupList( array( |
||
176 | new AliasGroup( 'de' ), |
||
177 | $enGroup, |
||
178 | new AliasGroup( 'en' ), |
||
179 | new AliasGroup( 'nl' ), |
||
180 | ) ); |
||
181 | |||
182 | $expectedList = new AliasGroupList( array( |
||
183 | new AliasGroup( 'en' ), |
||
184 | ) ); |
||
185 | |||
186 | $this->assertEquals( $expectedList, $list ); |
||
187 | } |
||
188 | |||
189 | public function testGivenEmptyGroup_setGroupRemovesGroup() { |
||
190 | $list = new AliasGroupList( array( |
||
191 | new AliasGroup( 'en', array( 'foo' ) ), |
||
192 | ) ); |
||
193 | |||
194 | $expectedList = new AliasGroupList(); |
||
195 | |||
196 | $list->setGroup( new AliasGroup( 'en' ) ); |
||
197 | $list->setGroup( new AliasGroup( 'de' ) ); |
||
198 | |||
199 | $this->assertEquals( $expectedList, $list ); |
||
200 | } |
||
201 | |||
202 | public function testEmptyListEqualsEmptyList() { |
||
206 | |||
207 | public function testFilledListEqualsItself() { |
||
208 | $list = new AliasGroupList( array( |
||
209 | new AliasGroup( 'en', array( 'foo' ) ), |
||
210 | new AliasGroup( 'de', array( 'bar' ) ), |
||
211 | ) ); |
||
212 | |||
213 | $this->assertTrue( $list->equals( $list ) ); |
||
214 | $this->assertTrue( $list->equals( clone $list ) ); |
||
215 | } |
||
216 | |||
217 | public function testDifferentListsDoNotEqual() { |
||
218 | $list = new AliasGroupList( array( |
||
219 | new AliasGroup( 'en', array( 'foo' ) ), |
||
220 | new AliasGroup( 'de', array( 'bar' ) ), |
||
221 | ) ); |
||
222 | |||
223 | $this->assertFalse( $list->equals( new AliasGroupList() ) ); |
||
224 | |||
225 | $this->assertFalse( $list->equals( |
||
226 | new AliasGroupList( array( |
||
227 | new AliasGroup( 'en', array( 'foo' ) ), |
||
228 | new AliasGroup( 'de', array( 'bar' ) ), |
||
229 | new AliasGroup( 'nl', array( 'baz' ) ), |
||
230 | ) ) |
||
231 | ) ); |
||
232 | } |
||
233 | |||
234 | public function testGivenNonAliasGroupList_equalsReturnsFalse() { |
||
235 | $list = new AliasGroupList(); |
||
236 | $this->assertFalse( $list->equals( null ) ); |
||
237 | $this->assertFalse( $list->equals( new \stdClass() ) ); |
||
238 | } |
||
239 | |||
240 | public function testGivenListsThatOnlyDifferInOrder_equalsReturnsTrue() { |
||
241 | $list = new AliasGroupList( array( |
||
242 | new AliasGroup( 'en', array( 'foo' ) ), |
||
243 | new AliasGroup( 'de', array( 'bar' ) ), |
||
244 | ) ); |
||
245 | |||
246 | $this->assertTrue( $list->equals( |
||
247 | new AliasGroupList( array( |
||
248 | new AliasGroup( 'de', array( 'bar' ) ), |
||
249 | new AliasGroup( 'en', array( 'foo' ) ), |
||
250 | ) ) |
||
251 | ) ); |
||
252 | } |
||
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() { |
||
283 | return array( |
||
284 | array( null ), |
||
285 | array( 21 ), |
||
286 | array( '' ), |
||
287 | ); |
||
288 | } |
||
289 | |||
290 | public function testGivenMismatchingGroup_hasGroupForLanguageReturnsFalse() { |
||
291 | $list = new AliasGroupList( array( new AliasGroup( 'en', array( 'cats' ) ) ) ); |
||
292 | $this->assertFalse( $list->hasGroupForLanguage( 'de' ) ); |
||
293 | } |
||
294 | |||
295 | public function testGivenMatchingGroup_hasGroupForLanguageReturnsTrue() { |
||
296 | $list = new AliasGroupList( array( new AliasGroup( 'en', array( 'kittens' ) ) ) ); |
||
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 | } |
||
387 |
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.