Complex classes like TermListTest 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 TermListTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class TermListTest extends \PHPUnit_Framework_TestCase { |
||
17 | |||
18 | public function testIsEmpty() { |
||
19 | $list = new TermList(); |
||
20 | $this->assertTrue( $list->isEmpty() ); |
||
21 | |||
22 | $list = new TermList( array( new Term( 'en', 'foo' ) ) ); |
||
23 | $this->assertFalse( $list->isEmpty() ); |
||
24 | } |
||
25 | |||
26 | public function testGivenNoTerms_sizeIsZero() { |
||
30 | |||
31 | public function testGivenTwoTerms_countReturnsTwo() { |
||
32 | $list = new TermList( $this->getTwoTerms() ); |
||
33 | |||
34 | $this->assertCount( 2, $list ); |
||
35 | } |
||
36 | |||
37 | private function getTwoTerms() { |
||
38 | return array( |
||
39 | 'en' => new Term( 'en', 'foo' ), |
||
40 | 'de' => new Term( 'de', 'bar' ), |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | public function testGivenTwoTerms_listContainsThem() { |
||
45 | $array = $this->getTwoTerms(); |
||
46 | |||
47 | $list = new TermList( $array ); |
||
48 | |||
49 | $this->assertEquals( $array, iterator_to_array( $list ) ); |
||
50 | } |
||
51 | |||
52 | public function testGivenTermsWithTheSameLanguage_onlyTheLastOnesAreRetained() { |
||
53 | $array = array( |
||
54 | new Term( 'en', 'foo' ), |
||
55 | new Term( 'en', 'bar' ), |
||
56 | |||
57 | new Term( 'de', 'baz' ), |
||
58 | |||
59 | new Term( 'nl', 'bah' ), |
||
60 | new Term( 'nl', 'blah' ), |
||
61 | new Term( 'nl', 'spam' ), |
||
62 | ); |
||
63 | |||
64 | $list = new TermList( $array ); |
||
65 | |||
66 | $this->assertEquals( |
||
67 | array( |
||
68 | 'en' => new Term( 'en', 'bar' ), |
||
69 | 'de' => new Term( 'de', 'baz' ), |
||
70 | 'nl' => new Term( 'nl', 'spam' ), |
||
71 | ), |
||
72 | iterator_to_array( $list ) |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | public function testGivenNoTerms_toTextArrayReturnsEmptyArray() { |
||
80 | |||
81 | public function testGivenTerms_toTextArrayReturnsTermsInFormat() { |
||
82 | $list = new TermList( array( |
||
83 | new Term( 'en', 'foo' ), |
||
84 | new Term( 'de', 'bar' ), |
||
85 | ) ); |
||
86 | |||
87 | $this->assertEquals( |
||
88 | array( |
||
89 | 'en' => 'foo', |
||
90 | 'de' => 'bar', |
||
91 | ), |
||
92 | $list->toTextArray() |
||
93 | ); |
||
94 | } |
||
95 | |||
96 | public function testCanIterateOverList() { |
||
97 | $list = new TermList( array( |
||
98 | new Term( 'en', 'foo' ), |
||
99 | ) ); |
||
100 | |||
101 | foreach ( $list as $key => $term ) { |
||
102 | $this->assertEquals( 'en', $key ); |
||
103 | $this->assertEquals( new Term( 'en', 'foo' ), $term ); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | public function testGivenSetLanguageCode_getByLanguageReturnsGroup() { |
||
108 | $enTerm = new Term( 'en', 'a' ); |
||
109 | |||
110 | $list = new TermList( array( |
||
111 | new Term( 'de', 'b' ), |
||
112 | $enTerm, |
||
113 | new Term( 'nl', 'c' ), |
||
114 | ) ); |
||
115 | |||
116 | $this->assertEquals( $enTerm, $list->getByLanguage( 'en' ) ); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @dataProvider invalidLanguageCodeProvider |
||
121 | * @expectedException InvalidArgumentException |
||
122 | */ |
||
123 | public function testGivenInvalidLanguageCode_getByLanguageThrowsException( $languageCode ) { |
||
127 | |||
128 | public function testGivenNonSetLanguageCode_getByLanguageThrowsException() { |
||
129 | $list = new TermList(); |
||
130 | |||
131 | $this->setExpectedException( 'OutOfBoundsException' ); |
||
|
|||
132 | $list->getByLanguage( 'en' ); |
||
133 | } |
||
134 | |||
135 | public function testHasTermForLanguage() { |
||
136 | $list = new TermList( array( |
||
137 | new Term( 'en', 'foo' ), |
||
138 | new Term( 'de', 'bar' ), |
||
139 | ) ); |
||
140 | |||
141 | $this->assertTrue( $list->hasTermForLanguage( 'en' ) ); |
||
142 | $this->assertTrue( $list->hasTermForLanguage( 'de' ) ); |
||
143 | |||
144 | $this->assertFalse( $list->hasTermForLanguage( 'nl' ) ); |
||
145 | $this->assertFalse( $list->hasTermForLanguage( 'fr' ) ); |
||
146 | |||
147 | $this->assertFalse( $list->hasTermForLanguage( 'EN' ) ); |
||
148 | $this->assertFalse( $list->hasTermForLanguage( ' de ' ) ); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @dataProvider invalidLanguageCodeProvider |
||
153 | * @expectedException InvalidArgumentException |
||
154 | */ |
||
155 | public function testGivenInvalidLanguageCode_hasTermForLanguageThrowsException( $languageCode ) { |
||
159 | |||
160 | public function invalidLanguageCodeProvider() { |
||
161 | return array( |
||
162 | array( null ), |
||
163 | array( 21 ), |
||
164 | array( '' ), |
||
165 | ); |
||
166 | } |
||
167 | |||
168 | public function testGivenNotSetLanguageCode_removeByLanguageDoesNoOp() { |
||
169 | $list = new TermList( array( |
||
170 | new Term( 'en', 'foo' ), |
||
171 | new Term( 'de', 'bar' ), |
||
172 | ) ); |
||
173 | |||
174 | $list->removeByLanguage( 'nl' ); |
||
175 | |||
176 | $this->assertCount( 2, $list ); |
||
177 | } |
||
178 | |||
179 | public function testGivenSetLanguageCode_removeByLanguageRemovesIt() { |
||
180 | $deTerm = new Term( 'de', 'bar' ); |
||
181 | |||
182 | $list = new TermList( array( |
||
183 | new Term( 'en', 'foo' ), |
||
184 | $deTerm, |
||
185 | ) ); |
||
186 | |||
187 | $list->removeByLanguage( 'en' ); |
||
188 | |||
189 | $this->assertEquals( |
||
190 | array( 'de' => $deTerm ), |
||
191 | iterator_to_array( $list ) |
||
192 | ); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @dataProvider invalidLanguageCodeProvider |
||
197 | * @expectedException InvalidArgumentException |
||
198 | */ |
||
199 | public function testGivenInvalidLanguageCode_removeByLanguageThrowsException( $languageCode ) { |
||
203 | |||
204 | public function testGivenTermForNewLanguage_setTermAddsTerm() { |
||
205 | $enTerm = new Term( 'en', 'foo' ); |
||
206 | $deTerm = new Term( 'de', 'bar' ); |
||
207 | |||
208 | $list = new TermList( array( $enTerm ) ); |
||
209 | $expectedList = new TermList( array( $enTerm, $deTerm ) ); |
||
210 | |||
211 | $list->setTerm( $deTerm ); |
||
212 | |||
213 | $this->assertEquals( $expectedList, $list ); |
||
214 | } |
||
215 | |||
216 | public function testGivenTermForExistingLanguage_setTermReplacesTerm() { |
||
217 | $enTerm = new Term( 'en', 'foo' ); |
||
218 | $newEnTerm = new Term( 'en', 'bar' ); |
||
219 | |||
220 | $list = new TermList( array( $enTerm ) ); |
||
221 | $expectedList = new TermList( array( $newEnTerm ) ); |
||
222 | |||
223 | $list->setTerm( $newEnTerm ); |
||
224 | $this->assertEquals( $expectedList, $list ); |
||
225 | } |
||
226 | |||
227 | public function testEmptyListEqualsEmptyList() { |
||
231 | |||
232 | public function testFilledListEqualsItself() { |
||
233 | $list = new TermList( array( |
||
234 | new Term( 'en', 'foo' ), |
||
235 | new Term( 'de', 'bar' ), |
||
236 | ) ); |
||
237 | |||
238 | $this->assertTrue( $list->equals( $list ) ); |
||
239 | $this->assertTrue( $list->equals( clone $list ) ); |
||
240 | } |
||
241 | |||
242 | public function testDifferentListsDoNotEqual() { |
||
243 | $list = new TermList( array( |
||
244 | new Term( 'en', 'foo' ), |
||
245 | new Term( 'de', 'bar' ), |
||
246 | ) ); |
||
247 | |||
248 | $this->assertFalse( $list->equals( new TermList() ) ); |
||
249 | |||
250 | $this->assertFalse( $list->equals( |
||
251 | new TermList( array( |
||
252 | new Term( 'en', 'foo' ), |
||
253 | ) ) |
||
254 | ) ); |
||
255 | |||
256 | $this->assertFalse( $list->equals( |
||
257 | new TermList( array( |
||
258 | new Term( 'en', 'foo' ), |
||
259 | new Term( 'de', 'HAX' ), |
||
260 | ) ) |
||
261 | ) ); |
||
262 | |||
263 | $this->assertFalse( $list->equals( |
||
264 | new TermList( array( |
||
265 | new Term( 'en', 'foo' ), |
||
266 | new Term( 'de', 'bar' ), |
||
267 | new Term( 'nl', 'baz' ), |
||
268 | ) ) |
||
269 | ) ); |
||
270 | } |
||
271 | |||
272 | public function testGivenNonTermList_equalsReturnsFalse() { |
||
273 | $list = new TermList(); |
||
274 | $this->assertFalse( $list->equals( null ) ); |
||
275 | $this->assertFalse( $list->equals( new \stdClass() ) ); |
||
276 | } |
||
277 | |||
278 | public function testGivenListsThatOnlyDifferInOrder_equalsReturnsTrue() { |
||
279 | $list = new TermList( array( |
||
280 | new Term( 'en', 'foo' ), |
||
281 | new Term( 'de', 'bar' ), |
||
282 | ) ); |
||
283 | |||
284 | $this->assertTrue( $list->equals( |
||
285 | new TermList( array( |
||
286 | new Term( 'de', 'bar' ), |
||
287 | new Term( 'en', 'foo' ), |
||
288 | ) ) |
||
289 | ) ); |
||
290 | } |
||
291 | |||
292 | public function testGivenNonSetLanguageTerm_hasTermReturnsFalse() { |
||
296 | |||
297 | public function testGivenMismatchingTerm_hasTermReturnsFalse() { |
||
301 | |||
302 | public function testGivenMatchingTerm_hasTermReturnsTrue() { |
||
306 | |||
307 | public function testGivenValidArgs_setTermTextSetsTerm() { |
||
308 | $list = new TermList(); |
||
309 | |||
310 | $list->setTextForLanguage( 'en', 'kittens' ); |
||
311 | |||
312 | $this->assertTrue( $list->getByLanguage( 'en' )->equals( new Term( 'en', 'kittens' ) ) ); |
||
313 | } |
||
314 | |||
315 | public function testGivenInvalidLanguageCode_setTermTextThrowsException() { |
||
316 | $list = new TermList(); |
||
317 | |||
318 | $this->setExpectedException( 'InvalidArgumentException' ); |
||
319 | $list->setTextForLanguage( null, 'kittens' ); |
||
320 | } |
||
321 | |||
322 | public function testGivenInvalidTermText_setTermTextThrowsException() { |
||
323 | $list = new TermList(); |
||
324 | |||
325 | $this->setExpectedException( 'InvalidArgumentException' ); |
||
326 | $list->setTextForLanguage( 'en', null ); |
||
327 | } |
||
328 | |||
329 | public function testGivenEmptyList_getWithLanguagesReturnsEmptyList() { |
||
334 | |||
335 | public function testGivenNoLanguages_getWithLanguagesReturnsEmptyList() { |
||
342 | |||
343 | public function testGivenAllLanguages_getWithLanguagesReturnsFullList() { |
||
350 | |||
351 | public function testGivenSomeLanguages_getWithLanguagesReturnsPartialList() { |
||
364 | |||
365 | public function testGivenEmptyTerms_constructorOnlyAddsNonEmptyTerms() { |
||
381 | |||
382 | public function testGivenEmptyTerm_setTermDoesNotAddIt() { |
||
388 | |||
389 | public function testGivenEmptyTerm_setTermRemovesExistingOne() { |
||
400 | |||
401 | public function testClear() { |
||
410 | |||
411 | } |
||
412 |
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.