Complex classes like EntityTest 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 EntityTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | abstract class EntityTest extends \PHPUnit_Framework_TestCase { |
||
25 | |||
26 | /** |
||
27 | * @since 0.1 |
||
28 | * |
||
29 | * @return Entity |
||
30 | */ |
||
31 | protected abstract function getNewEmpty(); |
||
32 | |||
33 | public function labelProvider() { |
||
34 | return array( |
||
35 | array( 'en', 'spam' ), |
||
36 | array( 'en', 'spam', 'spam' ), |
||
37 | array( 'de', 'foo bar baz' ), |
||
38 | ); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @dataProvider labelProvider |
||
43 | * @param string $languageCode |
||
44 | * @param string $labelText |
||
45 | * @param string $moarText |
||
46 | */ |
||
47 | public function testSetLabel( $languageCode, $labelText, $moarText = 'ohi there' ) { |
||
48 | $entity = $this->getNewEmpty(); |
||
49 | |||
50 | $entity->setLabel( $languageCode, $labelText ); |
||
|
|||
51 | |||
52 | $this->assertEquals( $labelText, $entity->getLabel( $languageCode ) ); |
||
53 | |||
54 | $entity->setLabel( $languageCode, $moarText ); |
||
55 | |||
56 | $this->assertEquals( $moarText, $entity->getLabel( $languageCode ) ); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @dataProvider labelProvider |
||
61 | * @param string $languageCode |
||
62 | * @param string $labelText |
||
63 | */ |
||
64 | public function testGetLabel( $languageCode, $labelText ) { |
||
65 | $entity = $this->getNewEmpty(); |
||
66 | |||
67 | $this->assertFalse( $entity->getLabel( $languageCode ) ); |
||
68 | |||
69 | $entity->setLabel( $languageCode, $labelText ); |
||
70 | |||
71 | $this->assertEquals( $labelText, $entity->getLabel( $languageCode ) ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @dataProvider labelProvider |
||
76 | * @param string $languageCode |
||
77 | * @param string $labelText |
||
78 | */ |
||
79 | public function testRemoveLabel( $languageCode, $labelText ) { |
||
80 | $entity = $this->getNewEmpty(); |
||
81 | $entity->setLabel( $languageCode, $labelText ); |
||
82 | $entity->removeLabel( $languageCode ); |
||
83 | $this->assertFalse( $entity->getLabel( $languageCode ) ); |
||
84 | } |
||
85 | |||
86 | public function descriptionProvider() { |
||
87 | return array( |
||
88 | array( 'en', 'spam' ), |
||
89 | array( 'en', 'spam', 'spam' ), |
||
90 | array( 'de', 'foo bar baz' ), |
||
91 | ); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @dataProvider descriptionProvider |
||
96 | * @param string $languageCode |
||
97 | * @param string $description |
||
98 | * @param string $moarText |
||
99 | */ |
||
100 | public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) { |
||
101 | $entity = $this->getNewEmpty(); |
||
102 | |||
103 | $entity->setDescription( $languageCode, $description ); |
||
104 | |||
105 | $this->assertEquals( $description, $entity->getDescription( $languageCode ) ); |
||
106 | |||
107 | $entity->setDescription( $languageCode, $moarText ); |
||
108 | |||
109 | $this->assertEquals( $moarText, $entity->getDescription( $languageCode ) ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @dataProvider descriptionProvider |
||
114 | * @param string $languageCode |
||
115 | * @param string $description |
||
116 | */ |
||
117 | public function testGetDescription( $languageCode, $description ) { |
||
118 | $entity = $this->getNewEmpty(); |
||
119 | |||
120 | $this->assertFalse( $entity->getDescription( $languageCode ) ); |
||
121 | |||
122 | $entity->setDescription( $languageCode, $description ); |
||
123 | |||
124 | $this->assertEquals( $description, $entity->getDescription( $languageCode ) ); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @dataProvider descriptionProvider |
||
129 | * @param string $languageCode |
||
130 | * @param string $description |
||
131 | */ |
||
132 | public function testRemoveDescription( $languageCode, $description ) { |
||
133 | $entity = $this->getNewEmpty(); |
||
134 | $entity->setDescription( $languageCode, $description ); |
||
135 | $entity->removeDescription( $languageCode ); |
||
136 | $this->assertFalse( $entity->getDescription( $languageCode ) ); |
||
137 | } |
||
138 | |||
139 | public function aliasesProvider() { |
||
140 | return array( |
||
141 | array( array( |
||
142 | 'en' => array( array( 'spam' ) ) |
||
143 | ) ), |
||
144 | array( array( |
||
145 | 'en' => array( array( 'foo', 'bar', 'baz' ) ) |
||
146 | ) ), |
||
147 | array( array( |
||
148 | 'en' => array( array( 'foo', 'bar' ), array( 'baz', 'spam' ) ) |
||
149 | ) ), |
||
150 | array( array( |
||
151 | 'en' => array( array( 'foo', 'bar', 'baz' ) ), |
||
152 | 'de' => array( array( 'foobar' ), array( 'baz' ) ), |
||
153 | ) ), |
||
154 | // with duplicates |
||
155 | array( array( |
||
156 | 'en' => array( array( 'spam', 'ham', 'ham' ) ) |
||
157 | ) ), |
||
158 | array( array( |
||
159 | 'en' => array( array( 'foo', 'bar' ), array( 'bar', 'spam' ) ) |
||
160 | ) ), |
||
161 | ); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @dataProvider aliasesProvider |
||
166 | */ |
||
167 | public function testAddAliases( array $aliasesLists ) { |
||
186 | |||
187 | /** |
||
188 | * @dataProvider aliasesProvider |
||
189 | */ |
||
190 | public function testSetAliases( array $aliasesLists ) { |
||
209 | |||
210 | /** |
||
211 | * @dataProvider aliasesProvider |
||
212 | */ |
||
213 | public function testSetEmptyAlias( array $aliasesLists ) { |
||
214 | $entity = $this->getNewEmpty(); |
||
215 | |||
216 | foreach ( $aliasesLists as $langCode => $aliasesList ) { |
||
217 | foreach ( $aliasesList as $aliases ) { |
||
218 | $entity->setAliases( $langCode, $aliases ); |
||
234 | |||
235 | /** |
||
236 | * @dataProvider aliasesProvider |
||
237 | */ |
||
238 | public function testSetAllAliases( array $aliasGroups ) { |
||
266 | |||
267 | public function testGetAliases() { |
||
278 | |||
279 | public function duplicateAliasesProvider() { |
||
300 | |||
301 | /** |
||
302 | * @dataProvider duplicateAliasesProvider |
||
303 | */ |
||
304 | public function testRemoveAliases( array $aliasesLists ) { |
||
323 | |||
324 | public function instanceProvider() { |
||
359 | |||
360 | /** |
||
361 | * @dataProvider instanceProvider |
||
362 | * @param Entity $entity |
||
363 | */ |
||
364 | public function testCopy( Entity $entity ) { |
||
373 | |||
374 | public function testCopyRetainsLabels() { |
||
385 | |||
386 | /** |
||
387 | * @dataProvider instanceProvider |
||
388 | * @param Entity $entity |
||
389 | */ |
||
390 | public function testSerialize( Entity $entity ) { |
||
400 | |||
401 | public function testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() { |
||
409 | |||
410 | public function testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() { |
||
426 | |||
427 | public function testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() { |
||
449 | |||
450 | public function testGivenEmptyFingerprint_noTermsAreSet() { |
||
456 | |||
457 | private function assertHasNoTerms( Entity $entity ) { |
||
462 | |||
463 | public function testGivenEmptyFingerprint_existingTermsAreRemoved() { |
||
474 | |||
475 | public function testWhenSettingFingerprint_getFingerprintReturnsIt() { |
||
494 | |||
495 | } |
||
496 |
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.