Total Complexity | 44 |
Total Lines | 340 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CoordinateTest 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.
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 CoordinateTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class CoordinateTest extends TestCase |
||
10 | { |
||
11 | /** |
||
12 | * @dataProvider providerColumnString |
||
13 | * |
||
14 | * @param mixed $expectedResult |
||
15 | * @param mixed $string |
||
16 | */ |
||
17 | public function testColumnIndexFromString($expectedResult, $string) |
||
18 | { |
||
19 | $columnIndex = Coordinate::columnIndexFromString($string); |
||
20 | self::assertEquals($expectedResult, $columnIndex); |
||
21 | |||
22 | $stringBack = Coordinate::stringFromColumnIndex($columnIndex); |
||
23 | self::assertEquals($stringBack, $string, 'should be able to get the original input with opposite method'); |
||
24 | } |
||
25 | |||
26 | public function providerColumnString() |
||
29 | } |
||
30 | |||
31 | public function testColumnIndexFromStringTooLong() |
||
32 | { |
||
33 | $cellAddress = 'ABCD'; |
||
34 | |||
35 | try { |
||
36 | Coordinate::columnIndexFromString($cellAddress); |
||
37 | } catch (\Exception $e) { |
||
38 | self::assertInstanceOf(Exception::class, $e); |
||
39 | self::assertEquals($e->getMessage(), 'Column string index can not be longer than 3 characters'); |
||
40 | |||
41 | return; |
||
42 | } |
||
43 | $this->fail('An expected exception has not been raised.'); |
||
44 | } |
||
45 | |||
46 | public function testColumnIndexFromStringTooShort() |
||
47 | { |
||
48 | $cellAddress = ''; |
||
49 | |||
50 | try { |
||
51 | Coordinate::columnIndexFromString($cellAddress); |
||
52 | } catch (\Exception $e) { |
||
53 | self::assertInstanceOf(Exception::class, $e); |
||
54 | self::assertEquals($e->getMessage(), 'Column string index can not be empty'); |
||
55 | |||
56 | return; |
||
57 | } |
||
58 | $this->fail('An expected exception has not been raised.'); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @dataProvider providerColumnIndex |
||
63 | * |
||
64 | * @param mixed $expectedResult |
||
65 | * @param int $columnIndex |
||
66 | */ |
||
67 | public function testStringFromColumnIndex($expectedResult, $columnIndex) |
||
68 | { |
||
69 | $string = Coordinate::stringFromColumnIndex($columnIndex); |
||
70 | self::assertEquals($expectedResult, $string); |
||
71 | |||
72 | $columnIndexBack = Coordinate::columnIndexFromString($string); |
||
73 | self::assertEquals($columnIndexBack, $columnIndex, 'should be able to get the original input with opposite method'); |
||
74 | } |
||
75 | |||
76 | public function providerColumnIndex() |
||
77 | { |
||
78 | return require 'data/ColumnIndex.php'; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @dataProvider providerCoordinates |
||
83 | * |
||
84 | * @param mixed $expectedResult |
||
85 | */ |
||
86 | public function testCoordinateFromString($expectedResult, ...$args) |
||
87 | { |
||
88 | $result = Coordinate::coordinateFromString(...$args); |
||
1 ignored issue
–
show
|
|||
89 | self::assertEquals($expectedResult, $result); |
||
90 | } |
||
91 | |||
92 | public function providerCoordinates() |
||
93 | { |
||
94 | return require 'data/CellCoordinates.php'; |
||
95 | } |
||
96 | |||
97 | public function testCoordinateFromStringWithRangeAddress() |
||
98 | { |
||
99 | $cellAddress = 'A1:AI2012'; |
||
100 | |||
101 | try { |
||
102 | Coordinate::coordinateFromString($cellAddress); |
||
103 | } catch (\Exception $e) { |
||
104 | self::assertInstanceOf(Exception::class, $e); |
||
105 | self::assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells'); |
||
106 | |||
107 | return; |
||
108 | } |
||
109 | $this->fail('An expected exception has not been raised.'); |
||
110 | } |
||
111 | |||
112 | public function testCoordinateFromStringWithEmptyAddress() |
||
113 | { |
||
114 | $cellAddress = ''; |
||
115 | |||
116 | try { |
||
117 | Coordinate::coordinateFromString($cellAddress); |
||
118 | } catch (\Exception $e) { |
||
119 | self::assertInstanceOf(Exception::class, $e); |
||
120 | self::assertEquals($e->getMessage(), 'Cell coordinate can not be zero-length string'); |
||
121 | |||
122 | return; |
||
123 | } |
||
124 | $this->fail('An expected exception has not been raised.'); |
||
125 | } |
||
126 | |||
127 | public function testCoordinateFromStringWithInvalidAddress() |
||
128 | { |
||
129 | $cellAddress = 'AI'; |
||
130 | |||
131 | try { |
||
132 | Coordinate::coordinateFromString($cellAddress); |
||
133 | } catch (\Exception $e) { |
||
134 | self::assertInstanceOf(Exception::class, $e); |
||
135 | self::assertEquals($e->getMessage(), 'Invalid cell coordinate ' . $cellAddress); |
||
136 | |||
137 | return; |
||
138 | } |
||
139 | $this->fail('An expected exception has not been raised.'); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @dataProvider providerAbsoluteCoordinates |
||
144 | * |
||
145 | * @param mixed $expectedResult |
||
146 | */ |
||
147 | public function testAbsoluteCoordinateFromString($expectedResult, ...$args) |
||
148 | { |
||
149 | $result = Coordinate::absoluteCoordinate(...$args); |
||
1 ignored issue
–
show
|
|||
150 | self::assertEquals($expectedResult, $result); |
||
151 | } |
||
152 | |||
153 | public function providerAbsoluteCoordinates() |
||
154 | { |
||
155 | return require 'data/CellAbsoluteCoordinate.php'; |
||
156 | } |
||
157 | |||
158 | public function testAbsoluteCoordinateFromStringWithRangeAddress() |
||
159 | { |
||
160 | $cellAddress = 'A1:AI2012'; |
||
161 | |||
162 | try { |
||
163 | Coordinate::absoluteCoordinate($cellAddress); |
||
164 | } catch (\Exception $e) { |
||
165 | self::assertInstanceOf(Exception::class, $e); |
||
166 | self::assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells'); |
||
167 | |||
168 | return; |
||
169 | } |
||
170 | $this->fail('An expected exception has not been raised.'); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @dataProvider providerAbsoluteReferences |
||
175 | * |
||
176 | * @param mixed $expectedResult |
||
177 | */ |
||
178 | public function testAbsoluteReferenceFromString($expectedResult, ...$args) |
||
179 | { |
||
180 | $result = Coordinate::absoluteReference(...$args); |
||
1 ignored issue
–
show
|
|||
181 | self::assertEquals($expectedResult, $result); |
||
182 | } |
||
183 | |||
184 | public function providerAbsoluteReferences() |
||
185 | { |
||
186 | return require 'data/CellAbsoluteReference.php'; |
||
187 | } |
||
188 | |||
189 | public function testAbsoluteReferenceFromStringWithRangeAddress() |
||
190 | { |
||
191 | $cellAddress = 'A1:AI2012'; |
||
192 | |||
193 | try { |
||
194 | Coordinate::absoluteReference($cellAddress); |
||
195 | } catch (\Exception $e) { |
||
196 | self::assertInstanceOf(Exception::class, $e); |
||
197 | self::assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells'); |
||
198 | |||
199 | return; |
||
200 | } |
||
201 | $this->fail('An expected exception has not been raised.'); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @dataProvider providerSplitRange |
||
206 | * |
||
207 | * @param mixed $expectedResult |
||
208 | */ |
||
209 | public function testSplitRange($expectedResult, ...$args) |
||
210 | { |
||
211 | $result = Coordinate::splitRange(...$args); |
||
1 ignored issue
–
show
|
|||
212 | foreach ($result as $key => $split) { |
||
213 | if (!is_array($expectedResult[$key])) { |
||
214 | self::assertEquals($expectedResult[$key], $split[0]); |
||
215 | } else { |
||
216 | self::assertEquals($expectedResult[$key], $split); |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | |||
221 | public function providerSplitRange() |
||
222 | { |
||
223 | return require 'data/CellSplitRange.php'; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @dataProvider providerBuildRange |
||
228 | * |
||
229 | * @param mixed $expectedResult |
||
230 | */ |
||
231 | public function testBuildRange($expectedResult, ...$args) |
||
232 | { |
||
233 | $result = Coordinate::buildRange(...$args); |
||
1 ignored issue
–
show
|
|||
234 | self::assertEquals($expectedResult, $result); |
||
235 | } |
||
236 | |||
237 | public function providerBuildRange() |
||
238 | { |
||
239 | return require 'data/CellBuildRange.php'; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @expectedException \TypeError |
||
244 | */ |
||
245 | public function testBuildRangeInvalid() |
||
246 | { |
||
247 | if (PHP_MAJOR_VERSION < 7) { |
||
248 | $this->markTestSkipped('Cannot catch type hinting error with PHP 5.6'); |
||
249 | } |
||
250 | |||
251 | $cellRange = ''; |
||
252 | Coordinate::buildRange($cellRange); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @dataProvider providerRangeBoundaries |
||
257 | * |
||
258 | * @param mixed $expectedResult |
||
259 | */ |
||
260 | public function testRangeBoundaries($expectedResult, ...$args) |
||
261 | { |
||
262 | $result = Coordinate::rangeBoundaries(...$args); |
||
1 ignored issue
–
show
|
|||
263 | self::assertEquals($expectedResult, $result); |
||
264 | } |
||
265 | |||
266 | public function providerRangeBoundaries() |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @dataProvider providerRangeDimension |
||
273 | * |
||
274 | * @param mixed $expectedResult |
||
275 | */ |
||
276 | public function testRangeDimension($expectedResult, ...$args) |
||
277 | { |
||
278 | $result = Coordinate::rangeDimension(...$args); |
||
1 ignored issue
–
show
|
|||
279 | self::assertEquals($expectedResult, $result); |
||
280 | } |
||
281 | |||
282 | public function providerRangeDimension() |
||
283 | { |
||
284 | return require 'data/CellRangeDimension.php'; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * @dataProvider providerGetRangeBoundaries |
||
289 | * |
||
290 | * @param mixed $expectedResult |
||
291 | */ |
||
292 | public function testGetRangeBoundaries($expectedResult, ...$args) |
||
293 | { |
||
294 | $result = Coordinate::getRangeBoundaries(...$args); |
||
1 ignored issue
–
show
|
|||
295 | self::assertEquals($expectedResult, $result); |
||
296 | } |
||
297 | |||
298 | public function providerGetRangeBoundaries() |
||
299 | { |
||
300 | return require 'data/CellGetRangeBoundaries.php'; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @dataProvider providerExtractAllCellReferencesInRange |
||
305 | * |
||
306 | * @param mixed $expectedResult |
||
307 | */ |
||
308 | public function testExtractAllCellReferencesInRange($expectedResult, ...$args) |
||
309 | { |
||
310 | $result = Coordinate::extractAllCellReferencesInRange(...$args); |
||
1 ignored issue
–
show
|
|||
311 | self::assertEquals($expectedResult, $result); |
||
312 | } |
||
313 | |||
314 | public function providerExtractAllCellReferencesInRange() |
||
315 | { |
||
316 | return require 'data/CellExtractAllCellReferencesInRange.php'; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @dataProvider providerMergeRangesInCollection |
||
321 | * |
||
322 | * @param mixed $expectedResult |
||
323 | */ |
||
324 | public function testMergeRangesInCollection($expectedResult, ...$args) |
||
328 | } |
||
329 | |||
330 | public function providerMergeRangesInCollection() |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @dataProvider providerCoordinateIsRange |
||
337 | * |
||
338 | * @param mixed $expectedResult |
||
339 | */ |
||
340 | public function testCoordinateIsRange($expectedResult, ...$args) |
||
341 | { |
||
342 | $result = Coordinate::coordinateIsRange(...$args); |
||
1 ignored issue
–
show
|
|||
343 | self::assertEquals($expectedResult, $result); |
||
344 | } |
||
345 | |||
346 | public function providerCoordinateIsRange() |
||
347 | { |
||
348 | return require 'data/CoordinateIsRange.php'; |
||
349 | } |
||
350 | } |
||
351 |