1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSpreadsheetTests; |
4
|
|
|
|
5
|
|
|
use PhpSpreadsheet\Cell; |
6
|
|
|
use PhpSpreadsheet\Exception; |
7
|
|
|
|
8
|
|
|
class CellTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @dataProvider providerColumnString |
12
|
|
|
*/ |
13
|
|
|
public function testColumnIndexFromString() |
14
|
|
|
{ |
15
|
|
|
$args = func_get_args(); |
16
|
|
|
$expectedResult = array_pop($args); |
17
|
|
|
$result = call_user_func_array([Cell::class, 'columnIndexFromString'], $args); |
18
|
|
|
$this->assertEquals($expectedResult, $result); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function providerColumnString() |
22
|
|
|
{ |
23
|
|
|
return require 'data/ColumnString.php'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
public function testColumnIndexFromStringTooLong() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$cellAddress = 'ABCD'; |
29
|
|
|
try { |
30
|
|
|
$result = call_user_func([Cell::class, 'columnIndexFromString'], $cellAddress); |
|
|
|
|
31
|
|
|
} catch (\Exception $e) { |
32
|
|
|
$this->assertInstanceOf(Exception::class, $e); |
33
|
|
|
$this->assertEquals($e->getMessage(), 'Column string index can not be longer than 3 characters'); |
34
|
|
|
|
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
$this->fail('An expected exception has not been raised.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function testColumnIndexFromStringTooShort() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$cellAddress = ''; |
43
|
|
|
try { |
44
|
|
|
$result = call_user_func([Cell::class, 'columnIndexFromString'], $cellAddress); |
|
|
|
|
45
|
|
|
} catch (\Exception $e) { |
46
|
|
|
$this->assertInstanceOf(Exception::class, $e); |
47
|
|
|
$this->assertEquals($e->getMessage(), 'Column string index can not be empty'); |
48
|
|
|
|
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
$this->fail('An expected exception has not been raised.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @dataProvider providerColumnIndex |
56
|
|
|
*/ |
57
|
|
|
public function testStringFromColumnIndex() |
58
|
|
|
{ |
59
|
|
|
$args = func_get_args(); |
60
|
|
|
$expectedResult = array_pop($args); |
61
|
|
|
$result = call_user_func_array([Cell::class, 'stringFromColumnIndex'], $args); |
62
|
|
|
$this->assertEquals($expectedResult, $result); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function providerColumnIndex() |
66
|
|
|
{ |
67
|
|
|
return require 'data/ColumnIndex.php'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @dataProvider providerCoordinates |
72
|
|
|
*/ |
73
|
|
|
public function testCoordinateFromString() |
74
|
|
|
{ |
75
|
|
|
$args = func_get_args(); |
76
|
|
|
$expectedResult = array_pop($args); |
77
|
|
|
$result = call_user_func_array([Cell::class, 'coordinateFromString'], $args); |
78
|
|
|
$this->assertEquals($expectedResult, $result); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function providerCoordinates() |
82
|
|
|
{ |
83
|
|
|
return require 'data/CellCoordinates.php'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
public function testCoordinateFromStringWithRangeAddress() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$cellAddress = 'A1:AI2012'; |
89
|
|
|
try { |
90
|
|
|
$result = call_user_func([Cell::class, 'coordinateFromString'], $cellAddress); |
|
|
|
|
91
|
|
|
} catch (\Exception $e) { |
92
|
|
|
$this->assertInstanceOf(Exception::class, $e); |
93
|
|
|
$this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells'); |
94
|
|
|
|
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
$this->fail('An expected exception has not been raised.'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function testCoordinateFromStringWithEmptyAddress() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$cellAddress = ''; |
103
|
|
|
try { |
104
|
|
|
$result = call_user_func([Cell::class, 'coordinateFromString'], $cellAddress); |
|
|
|
|
105
|
|
|
} catch (\Exception $e) { |
106
|
|
|
$this->assertInstanceOf(Exception::class, $e); |
107
|
|
|
$this->assertEquals($e->getMessage(), 'Cell coordinate can not be zero-length string'); |
108
|
|
|
|
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
$this->fail('An expected exception has not been raised.'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
View Code Duplication |
public function testCoordinateFromStringWithInvalidAddress() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$cellAddress = 'AI'; |
117
|
|
|
try { |
118
|
|
|
$result = call_user_func([Cell::class, 'coordinateFromString'], $cellAddress); |
|
|
|
|
119
|
|
|
} catch (\Exception $e) { |
120
|
|
|
$this->assertInstanceOf(Exception::class, $e); |
121
|
|
|
$this->assertEquals($e->getMessage(), 'Invalid cell coordinate ' . $cellAddress); |
122
|
|
|
|
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
$this->fail('An expected exception has not been raised.'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @dataProvider providerAbsoluteCoordinates |
130
|
|
|
*/ |
131
|
|
|
public function testAbsoluteCoordinateFromString() |
132
|
|
|
{ |
133
|
|
|
$args = func_get_args(); |
134
|
|
|
$expectedResult = array_pop($args); |
135
|
|
|
$result = call_user_func_array([Cell::class, 'absoluteCoordinate'], $args); |
136
|
|
|
$this->assertEquals($expectedResult, $result); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function providerAbsoluteCoordinates() |
140
|
|
|
{ |
141
|
|
|
return require 'data/CellAbsoluteCoordinate.php'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
View Code Duplication |
public function testAbsoluteCoordinateFromStringWithRangeAddress() |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$cellAddress = 'A1:AI2012'; |
147
|
|
|
try { |
148
|
|
|
$result = call_user_func([Cell::class, 'absoluteCoordinate'], $cellAddress); |
|
|
|
|
149
|
|
|
} catch (\Exception $e) { |
150
|
|
|
$this->assertInstanceOf(Exception::class, $e); |
151
|
|
|
$this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells'); |
152
|
|
|
|
153
|
|
|
return; |
154
|
|
|
} |
155
|
|
|
$this->fail('An expected exception has not been raised.'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @dataProvider providerAbsoluteReferences |
160
|
|
|
*/ |
161
|
|
|
public function testAbsoluteReferenceFromString() |
162
|
|
|
{ |
163
|
|
|
$args = func_get_args(); |
164
|
|
|
$expectedResult = array_pop($args); |
165
|
|
|
$result = call_user_func_array([Cell::class, 'absoluteReference'], $args); |
166
|
|
|
$this->assertEquals($expectedResult, $result); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function providerAbsoluteReferences() |
170
|
|
|
{ |
171
|
|
|
return require 'data/CellAbsoluteReference.php'; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
View Code Duplication |
public function testAbsoluteReferenceFromStringWithRangeAddress() |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$cellAddress = 'A1:AI2012'; |
177
|
|
|
try { |
178
|
|
|
$result = call_user_func([Cell::class, 'absoluteReference'], $cellAddress); |
|
|
|
|
179
|
|
|
} catch (\Exception $e) { |
180
|
|
|
$this->assertInstanceOf(Exception::class, $e); |
181
|
|
|
$this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells'); |
182
|
|
|
|
183
|
|
|
return; |
184
|
|
|
} |
185
|
|
|
$this->fail('An expected exception has not been raised.'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @dataProvider providerSplitRange |
190
|
|
|
*/ |
191
|
|
|
public function testSplitRange() |
192
|
|
|
{ |
193
|
|
|
$args = func_get_args(); |
194
|
|
|
$expectedResult = array_pop($args); |
195
|
|
|
$result = call_user_func_array([Cell::class, 'splitRange'], $args); |
196
|
|
|
foreach ($result as $key => $split) { |
197
|
|
|
if (!is_array($expectedResult[$key])) { |
198
|
|
|
$this->assertEquals($expectedResult[$key], $split[0]); |
199
|
|
|
} else { |
200
|
|
|
$this->assertEquals($expectedResult[$key], $split); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function providerSplitRange() |
206
|
|
|
{ |
207
|
|
|
return require 'data/CellSplitRange.php'; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @dataProvider providerBuildRange |
212
|
|
|
*/ |
213
|
|
|
public function testBuildRange() |
214
|
|
|
{ |
215
|
|
|
$args = func_get_args(); |
216
|
|
|
$expectedResult = array_pop($args); |
217
|
|
|
$result = call_user_func_array([Cell::class, 'buildRange'], $args); |
218
|
|
|
$this->assertEquals($expectedResult, $result); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function providerBuildRange() |
222
|
|
|
{ |
223
|
|
|
return require 'data/CellBuildRange.php'; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
View Code Duplication |
public function testBuildRangeInvalid() |
|
|
|
|
227
|
|
|
{ |
228
|
|
|
$cellRange = ''; |
229
|
|
|
try { |
230
|
|
|
$result = call_user_func([Cell::class, 'buildRange'], $cellRange); |
|
|
|
|
231
|
|
|
} catch (\Exception $e) { |
232
|
|
|
$this->assertInstanceOf(Exception::class, $e); |
233
|
|
|
$this->assertEquals($e->getMessage(), 'Range does not contain any information'); |
234
|
|
|
|
235
|
|
|
return; |
236
|
|
|
} |
237
|
|
|
$this->fail('An expected exception has not been raised.'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @dataProvider providerRangeBoundaries |
242
|
|
|
*/ |
243
|
|
|
public function testRangeBoundaries() |
244
|
|
|
{ |
245
|
|
|
$args = func_get_args(); |
246
|
|
|
$expectedResult = array_pop($args); |
247
|
|
|
$result = call_user_func_array([Cell::class, 'rangeBoundaries'], $args); |
248
|
|
|
$this->assertEquals($expectedResult, $result); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function providerRangeBoundaries() |
252
|
|
|
{ |
253
|
|
|
return require 'data/CellRangeBoundaries.php'; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @dataProvider providerRangeDimension |
258
|
|
|
*/ |
259
|
|
|
public function testRangeDimension() |
260
|
|
|
{ |
261
|
|
|
$args = func_get_args(); |
262
|
|
|
$expectedResult = array_pop($args); |
263
|
|
|
$result = call_user_func_array([Cell::class, 'rangeDimension'], $args); |
264
|
|
|
$this->assertEquals($expectedResult, $result); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function providerRangeDimension() |
268
|
|
|
{ |
269
|
|
|
return require 'data/CellRangeDimension.php'; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @dataProvider providerGetRangeBoundaries |
274
|
|
|
*/ |
275
|
|
|
public function testGetRangeBoundaries() |
276
|
|
|
{ |
277
|
|
|
$args = func_get_args(); |
278
|
|
|
$expectedResult = array_pop($args); |
279
|
|
|
$result = call_user_func_array([Cell::class, 'getRangeBoundaries'], $args); |
280
|
|
|
$this->assertEquals($expectedResult, $result); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function providerGetRangeBoundaries() |
284
|
|
|
{ |
285
|
|
|
return require 'data/CellGetRangeBoundaries.php'; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @dataProvider providerExtractAllCellReferencesInRange |
290
|
|
|
*/ |
291
|
|
|
public function testExtractAllCellReferencesInRange() |
292
|
|
|
{ |
293
|
|
|
$args = func_get_args(); |
294
|
|
|
$expectedResult = array_pop($args); |
295
|
|
|
$result = call_user_func_array([Cell::class, 'extractAllCellReferencesInRange'], $args); |
296
|
|
|
$this->assertEquals($expectedResult, $result); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function providerExtractAllCellReferencesInRange() |
300
|
|
|
{ |
301
|
|
|
return require 'data/CellExtractAllCellReferencesInRange.php'; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.