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 |
||
15 | class TermListTest extends \PHPUnit_Framework_TestCase { |
||
16 | |||
17 | public function testIsEmpty() { |
||
18 | $list = new TermList(); |
||
19 | $this->assertTrue( $list->isEmpty() ); |
||
20 | |||
21 | $list = new TermList( [ new Term( 'en', 'foo' ) ] ); |
||
22 | $this->assertFalse( $list->isEmpty() ); |
||
23 | } |
||
24 | |||
25 | public function testGivenNoTerms_sizeIsZero() { |
||
26 | $list = new TermList(); |
||
27 | $this->assertCount( 0, $list ); |
||
28 | } |
||
29 | |||
30 | public function testGivenTwoTerms_countReturnsTwo() { |
||
31 | $list = new TermList( $this->getTwoTerms() ); |
||
32 | |||
33 | $this->assertCount( 2, $list ); |
||
34 | } |
||
35 | |||
36 | private function getTwoTerms() { |
||
37 | return [ |
||
38 | 'en' => new Term( 'en', 'foo' ), |
||
39 | 'de' => new Term( 'de', 'bar' ), |
||
40 | ]; |
||
41 | } |
||
42 | |||
43 | public function testGivenTwoTerms_listContainsThem() { |
||
44 | $array = $this->getTwoTerms(); |
||
45 | |||
46 | $list = new TermList( $array ); |
||
47 | |||
48 | $this->assertEquals( $array, iterator_to_array( $list ) ); |
||
49 | } |
||
50 | |||
51 | public function testGivenTermsWithTheSameLanguage_onlyTheLastOnesAreRetained() { |
||
52 | $array = [ |
||
53 | new Term( 'en', 'foo' ), |
||
54 | new Term( 'en', 'bar' ), |
||
55 | |||
56 | new Term( 'de', 'baz' ), |
||
57 | |||
58 | new Term( 'nl', 'bah' ), |
||
59 | new Term( 'nl', 'blah' ), |
||
60 | new Term( 'nl', 'spam' ), |
||
61 | ]; |
||
62 | |||
63 | $list = new TermList( $array ); |
||
64 | |||
65 | $this->assertEquals( |
||
66 | [ |
||
67 | 'en' => new Term( 'en', 'bar' ), |
||
68 | 'de' => new Term( 'de', 'baz' ), |
||
69 | 'nl' => new Term( 'nl', 'spam' ), |
||
70 | ], |
||
71 | iterator_to_array( $list ) |
||
72 | ); |
||
73 | } |
||
74 | |||
75 | public function testGivenNoTerms_toTextArrayReturnsEmptyArray() { |
||
76 | $list = new TermList(); |
||
77 | $this->assertEquals( [], $list->toTextArray() ); |
||
78 | } |
||
79 | |||
80 | public function testGivenTerms_toTextArrayReturnsTermsInFormat() { |
||
81 | $list = new TermList( [ |
||
82 | new Term( 'en', 'foo' ), |
||
83 | new Term( 'de', 'bar' ), |
||
84 | ] ); |
||
85 | |||
86 | $this->assertEquals( |
||
87 | [ |
||
88 | 'en' => 'foo', |
||
89 | 'de' => 'bar', |
||
90 | ], |
||
91 | $list->toTextArray() |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | public function testCanIterateOverList() { |
||
96 | $list = new TermList( [ |
||
97 | new Term( 'en', 'foo' ), |
||
98 | ] ); |
||
99 | |||
100 | foreach ( $list as $key => $term ) { |
||
101 | $this->assertEquals( 'en', $key ); |
||
102 | $this->assertEquals( new Term( 'en', 'foo' ), $term ); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | public function testGivenSetLanguageCode_getByLanguageReturnsGroup() { |
||
107 | $enTerm = new Term( 'en', 'a' ); |
||
108 | |||
109 | $list = new TermList( [ |
||
110 | new Term( 'de', 'b' ), |
||
111 | $enTerm, |
||
112 | new Term( 'nl', 'c' ), |
||
113 | ] ); |
||
114 | |||
115 | $this->assertEquals( $enTerm, $list->getByLanguage( 'en' ) ); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @dataProvider invalidLanguageCodeProvider |
||
120 | */ |
||
121 | public function testGivenInvalidLanguageCode_getByLanguageThrowsException( $languageCode ) { |
||
126 | |||
127 | public function testGivenNonSetLanguageCode_getByLanguageThrowsException() { |
||
128 | $list = new TermList(); |
||
129 | |||
130 | $this->setExpectedException( 'OutOfBoundsException' ); |
||
131 | $list->getByLanguage( 'en' ); |
||
132 | } |
||
133 | |||
134 | public function testHasTermForLanguage() { |
||
135 | $list = new TermList( [ |
||
136 | new Term( 'en', 'foo' ), |
||
137 | new Term( 'de', 'bar' ), |
||
138 | ] ); |
||
139 | |||
140 | $this->assertTrue( $list->hasTermForLanguage( 'en' ) ); |
||
141 | $this->assertTrue( $list->hasTermForLanguage( 'de' ) ); |
||
142 | |||
143 | $this->assertFalse( $list->hasTermForLanguage( 'nl' ) ); |
||
144 | $this->assertFalse( $list->hasTermForLanguage( 'fr' ) ); |
||
145 | |||
146 | $this->assertFalse( $list->hasTermForLanguage( 'EN' ) ); |
||
147 | $this->assertFalse( $list->hasTermForLanguage( ' de ' ) ); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @dataProvider invalidLanguageCodeProvider |
||
152 | */ |
||
153 | public function testGivenInvalidLanguageCode_hasTermForLanguageReturnsFalse( $languageCode ) { |
||
154 | $list = new TermList(); |
||
155 | $this->assertFalse( $list->hasTermForLanguage( $languageCode ) ); |
||
156 | } |
||
157 | |||
158 | public function invalidLanguageCodeProvider() { |
||
159 | return [ |
||
160 | [ null ], |
||
161 | [ 21 ], |
||
162 | [ '' ], |
||
163 | ]; |
||
164 | } |
||
165 | |||
166 | public function testGivenNotSetLanguageCode_removeByLanguageDoesNoOp() { |
||
167 | $list = new TermList( [ |
||
168 | new Term( 'en', 'foo' ), |
||
169 | new Term( 'de', 'bar' ), |
||
170 | ] ); |
||
171 | |||
172 | $list->removeByLanguage( 'nl' ); |
||
173 | |||
174 | $this->assertCount( 2, $list ); |
||
175 | } |
||
176 | |||
177 | public function testGivenSetLanguageCode_removeByLanguageRemovesIt() { |
||
178 | $deTerm = new Term( 'de', 'bar' ); |
||
179 | |||
180 | $list = new TermList( [ |
||
181 | new Term( 'en', 'foo' ), |
||
182 | $deTerm, |
||
183 | ] ); |
||
184 | |||
185 | $list->removeByLanguage( 'en' ); |
||
186 | |||
187 | $this->assertEquals( |
||
188 | [ 'de' => $deTerm ], |
||
189 | iterator_to_array( $list ) |
||
190 | ); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @dataProvider invalidLanguageCodeProvider |
||
195 | */ |
||
196 | public function testGivenInvalidLanguageCode_removeByLanguageDoesNoOp( $languageCode ) { |
||
201 | |||
202 | public function testGivenTermForNewLanguage_setTermAddsTerm() { |
||
203 | $enTerm = new Term( 'en', 'foo' ); |
||
204 | $deTerm = new Term( 'de', 'bar' ); |
||
205 | |||
206 | $list = new TermList( [ $enTerm ] ); |
||
207 | $expectedList = new TermList( [ $enTerm, $deTerm ] ); |
||
208 | |||
209 | $list->setTerm( $deTerm ); |
||
210 | |||
211 | $this->assertEquals( $expectedList, $list ); |
||
212 | } |
||
213 | |||
214 | public function testGivenTermForExistingLanguage_setTermReplacesTerm() { |
||
215 | $enTerm = new Term( 'en', 'foo' ); |
||
224 | |||
225 | public function testEmptyListEqualsEmptyList() { |
||
229 | |||
230 | public function testFilledListEqualsItself() { |
||
239 | |||
240 | public function testDifferentListsDoNotEqual() { |
||
269 | |||
270 | public function testGivenNonTermList_equalsReturnsFalse() { |
||
275 | |||
276 | public function testGivenListsThatOnlyDifferInOrder_equalsReturnsTrue() { |
||
289 | |||
290 | public function testGivenNonSetLanguageTerm_hasTermReturnsFalse() { |
||
294 | |||
295 | public function testGivenMismatchingTerm_hasTermReturnsFalse() { |
||
299 | |||
300 | public function testGivenMatchingTerm_hasTermReturnsTrue() { |
||
304 | |||
305 | public function testGivenValidArgs_setTermTextSetsTerm() { |
||
312 | |||
313 | public function testGivenInvalidLanguageCode_setTermTextThrowsException() { |
||
319 | |||
320 | public function testGivenInvalidTermText_setTermTextThrowsException() { |
||
326 | |||
327 | public function testGivenEmptyList_getWithLanguagesReturnsEmptyList() { |
||
332 | |||
333 | public function testGivenNoLanguages_getWithLanguagesReturnsEmptyList() { |
||
340 | |||
341 | public function testGivenAllLanguages_getWithLanguagesReturnsFullList() { |
||
348 | |||
349 | public function testGivenSomeLanguages_getWithLanguagesReturnsPartialList() { |
||
362 | |||
363 | public function testGivenEmptyTerms_constructorOnlyAddsNonEmptyTerms() { |
||
364 | $list = new TermList( [ |
||
365 | new Term( 'en', 'foo' ), |
||
379 | |||
380 | public function testGivenEmptyTerm_setTermDoesNotAddIt() { |
||
386 | |||
387 | public function testGivenEmptyTerm_setTermRemovesExistingOne() { |
||
398 | |||
399 | public function testClear() { |
||
408 | |||
409 | } |
||
410 |