1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Csv; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class CsvTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @dataProvider providerDelimiterDetection |
13
|
|
|
* |
14
|
|
|
* @param string $filename |
15
|
|
|
* @param string $expectedDelimiter |
16
|
|
|
* @param string $cell |
17
|
|
|
* @param float|int|string $expectedValue |
18
|
|
|
*/ |
19
|
|
|
public function testDelimiterDetection($filename, $expectedDelimiter, $cell, $expectedValue): void |
20
|
|
|
{ |
21
|
|
|
$reader = new Csv(); |
22
|
|
|
self::assertNull($reader->getDelimiter()); |
23
|
|
|
|
24
|
|
|
$spreadsheet = $reader->load($filename); |
25
|
|
|
|
26
|
|
|
self::assertSame($expectedDelimiter, $reader->getDelimiter(), 'should be able to infer the delimiter'); |
27
|
|
|
|
28
|
|
|
$actual = $spreadsheet->getActiveSheet()->getCell($cell)->getValue(); |
29
|
|
|
self::assertSame($expectedValue, $actual, 'should be able to retrieve correct value'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function providerDelimiterDetection() |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
[ |
36
|
|
|
'tests/data/Reader/CSV/enclosure.csv', |
37
|
|
|
',', |
38
|
|
|
'C4', |
39
|
|
|
'username2', |
40
|
|
|
], |
41
|
|
|
[ |
42
|
|
|
'tests/data/Reader/CSV/semicolon_separated.csv', |
43
|
|
|
';', |
44
|
|
|
'C2', |
45
|
|
|
'25,5', |
46
|
|
|
], |
47
|
|
|
[ |
48
|
|
|
'tests/data/Reader/CSV/line_break_in_enclosure.csv', |
49
|
|
|
',', |
50
|
|
|
'A3', |
51
|
|
|
'Test', |
52
|
|
|
], |
53
|
|
|
[ |
54
|
|
|
'tests/data/Reader/CSV/line_break_in_enclosure_with_escaped_quotes.csv', |
55
|
|
|
',', |
56
|
|
|
'A3', |
57
|
|
|
'Test', |
58
|
|
|
], |
59
|
|
|
[ |
60
|
|
|
'tests/data/Reader/HTML/csv_with_angle_bracket.csv', |
61
|
|
|
',', |
62
|
|
|
'B1', |
63
|
|
|
'Number of items with weight <= 50kg', |
64
|
|
|
], |
65
|
|
|
[ |
66
|
|
|
'samples/Reader/sampleData/example1.csv', |
67
|
|
|
',', |
68
|
|
|
'I4', |
69
|
|
|
'100%', |
70
|
|
|
], |
71
|
|
|
[ |
72
|
|
|
'samples/Reader/sampleData/example2.csv', |
73
|
|
|
',', |
74
|
|
|
'D8', |
75
|
|
|
-58.373161, |
76
|
|
|
], |
77
|
|
|
[ |
78
|
|
|
'tests/data/Reader/CSV/empty.csv', |
79
|
|
|
',', |
80
|
|
|
'A1', |
81
|
|
|
null, |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
'tests/data/Reader/CSV/no_delimiter.csv', |
85
|
|
|
',', |
86
|
|
|
'A1', |
87
|
|
|
'SingleLine', |
88
|
|
|
], |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @dataProvider providerCanLoad |
94
|
|
|
* |
95
|
|
|
* @param bool $expected |
96
|
|
|
* @param string $filename |
97
|
|
|
*/ |
98
|
|
|
public function testCanLoad($expected, $filename): void |
99
|
|
|
{ |
100
|
|
|
$reader = new Csv(); |
101
|
|
|
self::assertSame($expected, $reader->canRead($filename)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function providerCanLoad() |
105
|
|
|
{ |
106
|
|
|
return [ |
107
|
|
|
[false, 'tests/data/Reader/Ods/data.ods'], |
108
|
|
|
[false, 'tests/data/Reader/Xml/WithoutStyle.xml'], |
109
|
|
|
[true, 'tests/data/Reader/CSV/enclosure.csv'], |
110
|
|
|
[true, 'tests/data/Reader/CSV/semicolon_separated.csv'], |
111
|
|
|
[true, 'tests/data/Reader/CSV/contains_html.csv'], |
112
|
|
|
[true, 'tests/data/Reader/CSV/csv_without_extension'], |
113
|
|
|
[true, 'tests/data/Reader/HTML/csv_with_angle_bracket.csv'], |
114
|
|
|
[true, 'tests/data/Reader/CSV/empty.csv'], |
115
|
|
|
[true, 'samples/Reader/sampleData/example1.csv'], |
116
|
|
|
[true, 'samples/Reader/sampleData/example2.csv'], |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testEscapeCharacters(): void |
121
|
|
|
{ |
122
|
|
|
$reader = (new Csv())->setEscapeCharacter('"'); |
123
|
|
|
$worksheet = $reader->load('tests/data/Reader/CSV/backslash.csv') |
124
|
|
|
->getActiveSheet(); |
125
|
|
|
|
126
|
|
|
$expected = [ |
127
|
|
|
['field 1', 'field 2\\'], |
128
|
|
|
['field 3\\', 'field 4'], |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
self::assertSame('"', $reader->getEscapeCharacter()); |
132
|
|
|
self::assertSame($expected, $worksheet->toArray()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @dataProvider providerEncodings |
137
|
|
|
* |
138
|
|
|
* @param string $filename |
139
|
|
|
* @param string $encoding |
140
|
|
|
*/ |
141
|
|
|
public function testEncodings($filename, $encoding): void |
142
|
|
|
{ |
143
|
|
|
$reader = new Csv(); |
144
|
|
|
$reader->setInputEncoding($encoding); |
145
|
|
|
$spreadsheet = $reader->load($filename); |
146
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
147
|
|
|
self::assertEquals('Å', $sheet->getCell('A1')->getValue()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testInvalidWorkSheetInfo(): void |
151
|
|
|
{ |
152
|
|
|
$this->expectException(ReaderException::class); |
153
|
|
|
$reader = new Csv(); |
154
|
|
|
$reader->listWorksheetInfo(''); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @dataProvider providerEncodings |
159
|
|
|
* |
160
|
|
|
* @param string $filename |
161
|
|
|
* @param string $encoding |
162
|
|
|
*/ |
163
|
|
|
public function testWorkSheetInfo($filename, $encoding): void |
164
|
|
|
{ |
165
|
|
|
$reader = new Csv(); |
166
|
|
|
$reader->setInputEncoding($encoding); |
167
|
|
|
$info = $reader->listWorksheetInfo($filename); |
168
|
|
|
self::assertEquals('Worksheet', $info[0]['worksheetName']); |
169
|
|
|
self::assertEquals('B', $info[0]['lastColumnLetter']); |
170
|
|
|
self::assertEquals(1, $info[0]['lastColumnIndex']); |
171
|
|
|
self::assertEquals(2, $info[0]['totalRows']); |
172
|
|
|
self::assertEquals(2, $info[0]['totalColumns']); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function providerEncodings() |
176
|
|
|
{ |
177
|
|
|
return [ |
178
|
|
|
['tests/data/Reader/CSV/encoding.iso88591.csv', 'ISO-8859-1'], |
179
|
|
|
['tests/data/Reader/CSV/encoding.utf8.csv', 'UTF-8'], |
180
|
|
|
['tests/data/Reader/CSV/encoding.utf8bom.csv', 'UTF-8'], |
181
|
|
|
['tests/data/Reader/CSV/encoding.utf16be.csv', 'UTF-16BE'], |
182
|
|
|
['tests/data/Reader/CSV/encoding.utf16le.csv', 'UTF-16LE'], |
183
|
|
|
['tests/data/Reader/CSV/encoding.utf32be.csv', 'UTF-32BE'], |
184
|
|
|
['tests/data/Reader/CSV/encoding.utf32le.csv', 'UTF-32LE'], |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testUtf16LineBreak(): void |
189
|
|
|
{ |
190
|
|
|
$reader = new Csv(); |
191
|
|
|
$reader->setInputEncoding('UTF-16BE'); |
192
|
|
|
$spreadsheet = $reader->load('tests/data/Reader/CSV/utf16be.line_break_in_enclosure.csv'); |
193
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
194
|
|
|
$expected = <<<EOF |
195
|
|
|
This is a test |
196
|
|
|
with line breaks |
197
|
|
|
that breaks the |
198
|
|
|
delimiters |
199
|
|
|
EOF; |
200
|
|
|
self::assertEquals($expected, $sheet->getCell('B3')->getValue()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function testLineBreakEscape(): void |
204
|
|
|
{ |
205
|
|
|
$reader = new Csv(); |
206
|
|
|
$spreadsheet = $reader->load('tests/data/Reader/CSV/line_break_in_enclosure_with_escaped_quotes.csv'); |
207
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
208
|
|
|
$expected = <<<EOF |
209
|
|
|
This is a "test csv file" |
210
|
|
|
with both "line breaks" |
211
|
|
|
and "escaped |
212
|
|
|
quotes" that breaks |
213
|
|
|
the delimiters |
214
|
|
|
EOF; |
215
|
|
|
self::assertEquals($expected, $sheet->getCell('B3')->getValue()); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testUtf32LineBreakEscape(): void |
219
|
|
|
{ |
220
|
|
|
$reader = new Csv(); |
221
|
|
|
$reader->setInputEncoding('UTF-32LE'); |
222
|
|
|
$spreadsheet = $reader->load('tests/data/Reader/CSV/line_break_escaped_32le.csv'); |
223
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
224
|
|
|
$expected = <<<EOF |
225
|
|
|
This is a "test csv file" |
226
|
|
|
with both "line breaks" |
227
|
|
|
and "escaped |
228
|
|
|
quotes" that breaks |
229
|
|
|
the delimiters |
230
|
|
|
EOF; |
231
|
|
|
self::assertEquals($expected, $sheet->getCell('B3')->getValue()); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function testSeparatorLine(): void |
235
|
|
|
{ |
236
|
|
|
$reader = new Csv(); |
237
|
|
|
$reader->setSheetIndex(3); |
238
|
|
|
$spreadsheet = $reader->load('tests/data/Reader/CSV/sep.csv'); |
239
|
|
|
self::assertEquals(';', $reader->getDelimiter()); |
240
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
241
|
|
|
self::assertEquals(3, $reader->getSheetIndex()); |
242
|
|
|
self::assertEquals(3, $spreadsheet->getActiveSheetIndex()); |
243
|
|
|
self::assertEquals('A', $sheet->getCell('A1')->getValue()); |
244
|
|
|
self::assertEquals(1, $sheet->getCell('B1')->getValue()); |
245
|
|
|
self::assertEquals(2, $sheet->getCell('A2')->getValue()); |
246
|
|
|
self::assertEquals(3, $sheet->getCell('B2')->getValue()); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function testDefaultSettings(): void |
250
|
|
|
{ |
251
|
|
|
$reader = new Csv(); |
252
|
|
|
self::assertEquals('UTF-8', $reader->getInputEncoding()); |
253
|
|
|
self::assertEquals('"', $reader->getEnclosure()); |
254
|
|
|
$reader->setEnclosure('\''); |
255
|
|
|
self::assertEquals('\'', $reader->getEnclosure()); |
256
|
|
|
$reader->setEnclosure(''); |
257
|
|
|
self::assertEquals('"', $reader->getEnclosure()); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function testReadEmptyFileName(): void |
261
|
|
|
{ |
262
|
|
|
$this->expectException(ReaderException::class); |
263
|
|
|
$reader = new Csv(); |
264
|
|
|
$filename = ''; |
265
|
|
|
$reader->load($filename); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function testReadNonexistentFileName(): void |
269
|
|
|
{ |
270
|
|
|
$this->expectException(ReaderException::class); |
271
|
|
|
$reader = new Csv(); |
272
|
|
|
$reader->load('tests/data/Reader/CSV/encoding.utf8.csvxxx'); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|