Total Complexity | 46 |
Total Lines | 357 |
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() |
||
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() |
||
240 | } |
||
241 | |||
242 | public function testBuildRangeInvalid() |
||
243 | { |
||
244 | $this->expectException(\TypeError::class); |
||
245 | |||
246 | if (PHP_MAJOR_VERSION < 7) { |
||
247 | $this->markTestSkipped('Cannot catch type hinting error with PHP 5.6'); |
||
248 | } |
||
249 | |||
250 | $cellRange = ''; |
||
251 | Coordinate::buildRange($cellRange); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @dataProvider providerRangeBoundaries |
||
256 | * |
||
257 | * @param mixed $expectedResult |
||
258 | */ |
||
259 | public function testRangeBoundaries($expectedResult, ...$args) |
||
260 | { |
||
261 | $result = Coordinate::rangeBoundaries(...$args); |
||
1 ignored issue
–
show
|
|||
262 | self::assertEquals($expectedResult, $result); |
||
263 | } |
||
264 | |||
265 | public function providerRangeBoundaries() |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @dataProvider providerRangeDimension |
||
272 | * |
||
273 | * @param mixed $expectedResult |
||
274 | */ |
||
275 | public function testRangeDimension($expectedResult, ...$args) |
||
276 | { |
||
277 | $result = Coordinate::rangeDimension(...$args); |
||
1 ignored issue
–
show
|
|||
278 | self::assertEquals($expectedResult, $result); |
||
279 | } |
||
280 | |||
281 | public function providerRangeDimension() |
||
282 | { |
||
283 | return require 'data/CellRangeDimension.php'; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @dataProvider providerGetRangeBoundaries |
||
288 | * |
||
289 | * @param mixed $expectedResult |
||
290 | */ |
||
291 | public function testGetRangeBoundaries($expectedResult, ...$args) |
||
292 | { |
||
293 | $result = Coordinate::getRangeBoundaries(...$args); |
||
1 ignored issue
–
show
|
|||
294 | self::assertEquals($expectedResult, $result); |
||
295 | } |
||
296 | |||
297 | public function providerGetRangeBoundaries() |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @dataProvider providerExtractAllCellReferencesInRange |
||
304 | * |
||
305 | * @param mixed $expectedResult |
||
306 | */ |
||
307 | public function testExtractAllCellReferencesInRange($expectedResult, ...$args) |
||
308 | { |
||
309 | $result = Coordinate::extractAllCellReferencesInRange(...$args); |
||
1 ignored issue
–
show
|
|||
310 | self::assertEquals($expectedResult, $result); |
||
311 | } |
||
312 | |||
313 | public function providerExtractAllCellReferencesInRange() |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @dataProvider providerInvalidRange |
||
320 | * |
||
321 | * @param string $range |
||
322 | */ |
||
323 | public function testExtractAllCellReferencesInRangeInvalidRange($range) |
||
324 | { |
||
325 | $this->expectException(Exception::class); |
||
326 | $this->expectExceptionMessage('Invalid range: "' . $range . '"'); |
||
327 | |||
328 | Coordinate::extractAllCellReferencesInRange($range); |
||
329 | } |
||
330 | |||
331 | public function providerInvalidRange() |
||
332 | { |
||
333 | return [['Z1:A1'], ['A4:A1'], ['B1:A1'], ['AA1:Z1']]; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @dataProvider providerMergeRangesInCollection |
||
338 | * |
||
339 | * @param mixed $expectedResult |
||
340 | */ |
||
341 | public function testMergeRangesInCollection($expectedResult, ...$args) |
||
345 | } |
||
346 | |||
347 | public function providerMergeRangesInCollection() |
||
348 | { |
||
349 | return require 'data/CellMergeRangesInCollection.php'; |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @dataProvider providerCoordinateIsRange |
||
354 | * |
||
355 | * @param mixed $expectedResult |
||
356 | */ |
||
357 | public function testCoordinateIsRange($expectedResult, ...$args) |
||
361 | } |
||
362 | |||
363 | public function providerCoordinateIsRange() |
||
366 | } |
||
367 | } |
||
368 |