|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
The MIT License (MIT) |
|
4
|
|
|
|
|
5
|
|
|
Copyright (c) 2015 PortPHP |
|
6
|
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
in the Software without restriction, including without limitation the rights |
|
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
furnished to do so, subject to the following conditions: |
|
13
|
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
copies or substantial portions of the Software. |
|
16
|
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
namespace Port\Excel\Tests; |
|
26
|
|
|
|
|
27
|
|
|
use Port\Excel\ExcelReader; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritDoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
class ExcelReaderTest extends \PHPUnit_Framework_TestCase |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritDoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function setUp() |
|
38
|
|
|
{ |
|
39
|
|
|
if (!extension_loaded('zip')) { |
|
40
|
|
|
$this->markTestSkipped(); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testCountWithHeaders() |
|
48
|
|
|
{ |
|
49
|
|
|
$file = new \SplFileObject(__DIR__.'/fixtures/data_column_headers.xlsx'); |
|
50
|
|
|
$reader = new ExcelReader($file, 0); |
|
51
|
|
|
$this->assertEquals(3, $reader->count()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testCountWithoutHeaders() |
|
58
|
|
|
{ |
|
59
|
|
|
$file = new \SplFileObject(__DIR__.'/fixtures/data_no_column_headers.xls'); |
|
60
|
|
|
$reader = new ExcelReader($file); |
|
61
|
|
|
$this->assertEquals(3, $reader->count()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testCustomColumnHeadersWithHeaders() |
|
68
|
|
|
{ |
|
69
|
|
|
$file = new \SplFileObject(__DIR__.'/fixtures/data_column_headers.xlsx'); |
|
70
|
|
|
$reader = new ExcelReader($file, 0); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertEquals( |
|
73
|
|
|
array( |
|
74
|
|
|
'id', |
|
75
|
|
|
'number', |
|
76
|
|
|
'description', |
|
77
|
|
|
), |
|
78
|
|
|
$reader->getColumnHeaders() |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$reader->setColumnHeaders( |
|
82
|
|
|
array( |
|
83
|
|
|
'id2', |
|
84
|
|
|
'number2', |
|
85
|
|
|
'description2', |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertEquals( |
|
90
|
|
|
array( |
|
91
|
|
|
'id2', |
|
92
|
|
|
'number2', |
|
93
|
|
|
'description2', |
|
94
|
|
|
), |
|
95
|
|
|
$reader->getColumnHeaders() |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
// TODO: Check if row 0 should return the header row if headers are enabled. |
|
99
|
|
|
// Row 0 returns the header row as data and indexes. |
|
100
|
|
|
$row = $reader->getRow(0); |
|
101
|
|
|
$this->assertEquals( |
|
102
|
|
|
array( |
|
103
|
|
|
'id2' => 'id', |
|
104
|
|
|
'number2' => 'number', |
|
105
|
|
|
'description2' => 'description', |
|
106
|
|
|
), |
|
107
|
|
|
$row |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
$row = $reader->getRow(3); |
|
111
|
|
|
$this->assertEquals( |
|
112
|
|
|
array( |
|
113
|
|
|
'id2' => 7.0, |
|
114
|
|
|
'number2' => 7890.0, |
|
115
|
|
|
'description2' => 'Some more info', |
|
116
|
|
|
), |
|
117
|
|
|
$row |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
$row = $reader->getRow(1); |
|
121
|
|
|
$this->assertEquals( |
|
122
|
|
|
array( |
|
123
|
|
|
'id2' => 50.0, |
|
124
|
|
|
'number2' => 123.0, |
|
125
|
|
|
'description2' => 'Description', |
|
126
|
|
|
), |
|
127
|
|
|
$row |
|
128
|
|
|
); |
|
129
|
|
|
|
|
130
|
|
|
$row = $reader->getRow(2); |
|
131
|
|
|
$this->assertEquals( |
|
132
|
|
|
array( |
|
133
|
|
|
'id2' => 6.0, |
|
134
|
|
|
'number2' => 456.0, |
|
135
|
|
|
'description2' => 'Another description', |
|
136
|
|
|
), |
|
137
|
|
|
$row |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* |
|
143
|
|
|
*/ |
|
144
|
|
|
public function testCustomColumnHeadersWithoutHeaders() |
|
145
|
|
|
{ |
|
146
|
|
|
$file = new \SplFileObject(__DIR__.'/fixtures/data_no_column_headers.xls'); |
|
147
|
|
|
$reader = new ExcelReader($file); |
|
148
|
|
|
|
|
149
|
|
|
$reader->setColumnHeaders( |
|
150
|
|
|
array( |
|
151
|
|
|
'id', |
|
152
|
|
|
'number', |
|
153
|
|
|
'description', |
|
154
|
|
|
) |
|
155
|
|
|
); |
|
156
|
|
|
|
|
157
|
|
|
$row = $reader->getRow(2); |
|
158
|
|
|
$this->assertEquals( |
|
159
|
|
|
array( |
|
160
|
|
|
'id' => 7.0, |
|
161
|
|
|
'number' => 7890.0, |
|
162
|
|
|
'description' => 'Some more info', |
|
163
|
|
|
), |
|
164
|
|
|
$row |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
$row = $reader->getRow(0); |
|
168
|
|
|
$this->assertEquals( |
|
169
|
|
|
array( |
|
170
|
|
|
'id' => 50.0, |
|
171
|
|
|
'number' => 123.0, |
|
172
|
|
|
'description' => 'Description', |
|
173
|
|
|
), |
|
174
|
|
|
$row |
|
175
|
|
|
); |
|
176
|
|
|
|
|
177
|
|
|
$row = $reader->getRow(1); |
|
178
|
|
|
$this->assertEquals( |
|
179
|
|
|
array( |
|
180
|
|
|
'id' => 6.0, |
|
181
|
|
|
'number' => 456.0, |
|
182
|
|
|
'description' => 'Another description', |
|
183
|
|
|
), |
|
184
|
|
|
$row |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* |
|
190
|
|
|
*/ |
|
191
|
|
|
public function testIterateWithHeaders() |
|
192
|
|
|
{ |
|
193
|
|
|
$file = new \SplFileObject(__DIR__.'/fixtures/data_column_headers.xlsx'); |
|
194
|
|
|
$reader = new ExcelReader($file, 0); |
|
195
|
|
|
|
|
196
|
|
|
$actualData = array(); |
|
197
|
|
|
$expectedData = array( |
|
198
|
|
|
array( |
|
199
|
|
|
'id' => 50.0, |
|
200
|
|
|
'number' => 123.0, |
|
201
|
|
|
'description' => 'Description', |
|
202
|
|
|
), |
|
203
|
|
|
array( |
|
204
|
|
|
'id' => 6.0, |
|
205
|
|
|
'number' => 456.0, |
|
206
|
|
|
'description' => 'Another description', |
|
207
|
|
|
), |
|
208
|
|
|
array( |
|
209
|
|
|
'id' => 7.0, |
|
210
|
|
|
'number' => 7890.0, |
|
211
|
|
|
'description' => 'Some more info', |
|
212
|
|
|
), |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
foreach ($reader as $row) { |
|
216
|
|
|
$actualData[] = $row; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$this->assertEquals($expectedData, $actualData); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* |
|
224
|
|
|
*/ |
|
225
|
|
|
public function testIterateWithoutHeaders() |
|
226
|
|
|
{ |
|
227
|
|
|
$file = new \SplFileObject(__DIR__.'/fixtures/data_no_column_headers.xls'); |
|
228
|
|
|
$reader = new ExcelReader($file); |
|
229
|
|
|
|
|
230
|
|
|
$actualData = array(); |
|
231
|
|
|
$expectedData = array( |
|
232
|
|
|
array(50.0, 123.0, "Description"), |
|
233
|
|
|
array(6.0, 456.0, 'Another description'), |
|
234
|
|
|
array(7.0, 7890.0, 'Some more info'), |
|
235
|
|
|
); |
|
236
|
|
|
|
|
237
|
|
|
foreach ($reader as $row) { |
|
238
|
|
|
$actualData[] = $row; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
$this->assertEquals($expectedData, $actualData); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* |
|
246
|
|
|
*/ |
|
247
|
|
|
public function testMaxRowNumb() |
|
248
|
|
|
{ |
|
249
|
|
|
$file = new \SplFileObject(__DIR__.'/fixtures/data_no_column_headers.xls'); |
|
250
|
|
|
$reader = new ExcelReader($file, null, null, null, 1000); |
|
|
|
|
|
|
251
|
|
|
$this->assertEquals(3, $reader->count()); |
|
252
|
|
|
|
|
253
|
|
|
// Without $maxRows, this faulty file causes OOM because of an extremely |
|
254
|
|
|
//high last row number |
|
255
|
|
|
$file = new \SplFileObject(__DIR__.'/fixtures/data_extreme_last_row.xlsx'); |
|
256
|
|
|
|
|
257
|
|
|
$max = 5; |
|
258
|
|
|
$reader = new ExcelReader($file, null, null, null, $max); |
|
|
|
|
|
|
259
|
|
|
$this->assertEquals($max, $reader->count()); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* |
|
264
|
|
|
*/ |
|
265
|
|
|
public function testMultiSheet() |
|
266
|
|
|
{ |
|
267
|
|
|
$file = new \SplFileObject(__DIR__.'/fixtures/data_multi_sheet.xls'); |
|
268
|
|
|
$sheet1reader = new ExcelReader($file, null, 0); |
|
269
|
|
|
$this->assertEquals(3, $sheet1reader->count()); |
|
270
|
|
|
|
|
271
|
|
|
$sheet2reader = new ExcelReader($file, null, 1); |
|
272
|
|
|
$this->assertEquals(2, $sheet2reader->count()); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* |
|
277
|
|
|
*/ |
|
278
|
|
|
public function testSeekWithHeaders() |
|
279
|
|
|
{ |
|
280
|
|
|
$file = new \SplFileObject(__DIR__.'/fixtures/data_column_headers.xlsx'); |
|
281
|
|
|
$reader = new ExcelReader($file, 0); |
|
282
|
|
|
|
|
283
|
|
|
// TODO: Check if row 0 should return the header row if headers are enabled. |
|
284
|
|
|
// Row 0 returns the header row as data and indexes. |
|
285
|
|
|
$row = $reader->getRow(0); |
|
286
|
|
|
$this->assertEquals( |
|
287
|
|
|
array( |
|
288
|
|
|
'id' => 'id', |
|
289
|
|
|
'number' => 'number', |
|
290
|
|
|
'description' => 'description', |
|
291
|
|
|
), |
|
292
|
|
|
$row |
|
293
|
|
|
); |
|
294
|
|
|
$this->assertEquals(0, $reader->key()); |
|
295
|
|
|
|
|
296
|
|
|
$row = $reader->getRow(3); |
|
297
|
|
|
$this->assertEquals( |
|
298
|
|
|
array( |
|
299
|
|
|
'id' => 7.0, |
|
300
|
|
|
'number' => 7890.0, |
|
301
|
|
|
'description' => 'Some more info', |
|
302
|
|
|
), |
|
303
|
|
|
$row |
|
304
|
|
|
); |
|
305
|
|
|
$this->assertEquals(3, $reader->key()); |
|
306
|
|
|
|
|
307
|
|
|
$row = $reader->getRow(1); |
|
308
|
|
|
$this->assertEquals( |
|
309
|
|
|
array( |
|
310
|
|
|
'id' => 50.0, |
|
311
|
|
|
'number' => 123.0, |
|
312
|
|
|
'description' => 'Description', |
|
313
|
|
|
), |
|
314
|
|
|
$row |
|
315
|
|
|
); |
|
316
|
|
|
$this->assertEquals(1, $reader->key()); |
|
317
|
|
|
|
|
318
|
|
|
$row = $reader->getRow(2); |
|
319
|
|
|
$this->assertEquals( |
|
320
|
|
|
array( |
|
321
|
|
|
'id' => 6.0, |
|
322
|
|
|
'number' => 456.0, |
|
323
|
|
|
'description' => 'Another description', |
|
324
|
|
|
), |
|
325
|
|
|
$row |
|
326
|
|
|
); |
|
327
|
|
|
$this->assertEquals(2, $reader->key()); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* |
|
332
|
|
|
*/ |
|
333
|
|
|
public function testSeekWithoutHeaders() |
|
334
|
|
|
{ |
|
335
|
|
|
$file = new \SplFileObject(__DIR__.'/fixtures/data_no_column_headers.xls'); |
|
336
|
|
|
$reader = new ExcelReader($file); |
|
337
|
|
|
|
|
338
|
|
|
$row = $reader->getRow(2); |
|
339
|
|
|
$this->assertEquals( |
|
340
|
|
|
array( |
|
341
|
|
|
7.0, |
|
342
|
|
|
7890.0, |
|
343
|
|
|
'Some more info', |
|
344
|
|
|
), |
|
345
|
|
|
$row |
|
346
|
|
|
); |
|
347
|
|
|
|
|
348
|
|
|
$row = $reader->getRow(0); |
|
349
|
|
|
$this->assertEquals( |
|
350
|
|
|
array( |
|
351
|
|
|
50.0, |
|
352
|
|
|
123.0, |
|
353
|
|
|
'Description', |
|
354
|
|
|
), |
|
355
|
|
|
$row |
|
356
|
|
|
); |
|
357
|
|
|
|
|
358
|
|
|
$row = $reader->getRow(1); |
|
359
|
|
|
$this->assertEquals( |
|
360
|
|
|
array( |
|
361
|
|
|
6.0, |
|
362
|
|
|
456.0, |
|
363
|
|
|
'Another description', |
|
364
|
|
|
), |
|
365
|
|
|
$row |
|
366
|
|
|
); |
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: