-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathCsvReaderTest.php
More file actions
284 lines (232 loc) · 9.33 KB
/
CsvReaderTest.php
File metadata and controls
284 lines (232 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
namespace Ddeboer\DataImport\Tests\Reader;
use Ddeboer\DataImport\Reader\CsvReader;
class CsvReaderTest extends \PHPUnit_Framework_TestCase
{
public function testReadCsvFileWithColumnHeaders()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers.csv');
$csvReader = new CsvReader($file);
$csvReader->setHeaderRowNumber(0);
$this->assertEquals(
array(
'id', 'number', 'description'
),
$csvReader->getFields()
);
foreach ($csvReader as $row) {
$this->assertNotNull($row['id']);
$this->assertNotNull($row['number']);
$this->assertNotNull($row['description']);
}
$this->assertEquals(
array(
'id' => 6,
'number' => '456',
'description' => 'Another description'
),
$csvReader->getRow(2)
);
}
public function testReadCsvFileWithoutColumnHeaders()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_no_column_headers.csv');
$csvReader = new CsvReader($file);
$this->assertEmpty($csvReader->getColumnHeaders());
}
public function testReadCsvFileWithManualColumnHeaders()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_no_column_headers.csv');
$csvReader = new CsvReader($file);
$csvReader->setColumnHeaders(array('id', 'number', 'description'));
foreach ($csvReader as $row) {
$this->assertNotNull($row['id']);
$this->assertNotNull($row['number']);
$this->assertNotNull($row['description']);
}
}
public function testReadCsvFileWithTrailingBlankLines()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_blank_lines.csv');
$csvReader = new CsvReader($file);
$csvReader->setColumnHeaders(array('id', 'number', 'description'));
foreach ($csvReader as $row) {
$this->assertNotNull($row['id']);
$this->assertNotNull($row['number']);
$this->assertNotNull($row['description']);
}
}
public function testCountWithoutHeaders()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_no_column_headers.csv');
$csvReader = new CsvReader($file);
$this->assertEquals(3, $csvReader->count());
}
public function testCountWithHeaders()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers.csv');
$csvReader = new CsvReader($file);
$csvReader->setHeaderRowNumber(0);
$this->assertEquals(3, $csvReader->count(), 'Row count should not include header');
}
public function testCountWithFewerElementsThanColumnHeadersNotStrict()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_fewer_elements_than_column_headers.csv');
$csvReader = new CsvReader($file);
$csvReader->setStrict(false);
$csvReader->setHeaderRowNumber(0);
$this->assertEquals(3, $csvReader->count());
}
public function testCountWithMoreElementsThanColumnHeadersNotStrict()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_more_elements_than_column_headers.csv');
$csvReader = new CsvReader($file);
$csvReader->setStrict(false);
$csvReader->setHeaderRowNumber(0);
$this->assertEquals(3, $csvReader->count());
$this->assertFalse($csvReader->hasErrors());
$this->assertEquals(array(6, 456, 'Another description'), array_values($csvReader->getRow(2)));
}
public function testCountDoesNotMoveFilePointer()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers.csv');
$csvReader = new CsvReader($file);
$csvReader->setHeaderRowNumber(0);
$key_before_count = $csvReader->key();
$csvReader->count();
$key_after_count = $csvReader->key();
$this->assertEquals($key_after_count, $key_before_count);
}
public function testVaryingElementCountWithColumnHeadersNotStrict()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers_varying_element_count.csv');
$csvReader = new CsvReader($file);
$csvReader->setStrict(false);
$csvReader->setHeaderRowNumber(0);
$this->assertEquals(4, $csvReader->count());
$this->assertFalse($csvReader->hasErrors());
}
public function testVaryingElementCountWithoutColumnHeadersNotStrict()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_no_column_headers_varying_element_count.csv');
$csvReader = new CsvReader($file);
$csvReader->setStrict(false);
$csvReader->setColumnHeaders(array('id', 'number', 'description'));
$this->assertEquals(5, $csvReader->count());
$this->assertFalse($csvReader->hasErrors());
}
public function testInvalidCsv()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers_varying_element_count.csv');
$reader = new CsvReader($file);
$reader->setHeaderRowNumber(0);
$this->assertTrue($reader->hasErrors());
$this->assertCount(2, $reader->getErrors());
$errors = $reader->getErrors();
$this->assertEquals(2, key($errors));
$this->assertEquals(array('123', 'test'), current($errors));
next($errors);
$this->assertEquals(3, key($errors));
$this->assertEquals(array('7', '7890', 'Some more info', 'too many columns'), current($errors));
}
public function testLastRowInvalidCsv()
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/data_no_column_headers_varying_element_count.csv');
$reader = new CsvReader($file);
$reader->setColumnHeaders(array('id', 'number', 'description'));
$this->assertTrue($reader->hasErrors());
$this->assertCount(3, $reader->getErrors());
$errors = $reader->getErrors();
$this->assertEquals(1, key($errors));
$this->assertEquals(array('6', 'strictly invalid'), current($errors));
next($errors);
$this->assertEquals(3, key($errors));
$this->assertEquals(array('3','230','Yet more info','Even more info'), current($errors));
next($errors);
$this->assertEquals(4, key($errors));
$this->assertEquals(array('strictly invalid'), current($errors));
}
public function testLineBreaks()
{
$reader = $this->getReader('data_cr_breaks.csv');
$this->assertCount(3, $reader);
}
/**
* @expectedException \Ddeboer\DataImport\Exception\DuplicateHeadersException description
*/
public function testDuplicateHeadersThrowsException()
{
$reader = $this->getReader('data_column_headers_duplicates.csv');
$reader->setHeaderRowNumber(0);
}
public function testDuplicateHeadersIncrement()
{
$reader = $this->getReader('data_column_headers_duplicates.csv');
$reader->setHeaderRowNumber(0, CsvReader::DUPLICATE_HEADERS_INCREMENT);
$reader->rewind();
$current = $reader->current();
$this->assertEquals(
array('id', 'description', 'description1', 'description2', 'details', 'details1', 'last', 'UNKNOWN', 'UNKNOWN1'),
$reader->getColumnHeaders()
);
$this->assertEquals(
array(
'id' => '50',
'description' => 'First',
'description1' => 'Second',
'description2' => 'Third',
'details' => 'Details1',
'details1' => 'Details2',
'last' => 'Last one',
'UNKNOWN' => 'empty1',
'UNKNOWN1' => 'empty2'
),
$current
);
}
public function testDuplicateHeadersMerge()
{
$reader = $this->getReader('data_column_headers_duplicates.csv');
$reader->setHeaderRowNumber(0, CsvReader::DUPLICATE_HEADERS_MERGE);
$reader->rewind();
$current = $reader->current();
$this->assertCount(5, $reader->getColumnHeaders());
$expected = array(
'id' => '50',
'description' => array('First', 'Second', 'Third'),
'details' => array('Details1', 'Details2'),
'last' => 'Last one',
'' => array('empty1', 'empty2')
);
$this->assertEquals($expected, $current);
}
public function testMaximumNesting()
{
if (!function_exists('xdebug_is_enabled')) {
$this->markTestSkipped('xDebug is not installed');
}
$xdebug_start = !xdebug_is_enabled();
if ($xdebug_start) {
xdebug_enable();
}
ini_set('xdebug.max_nesting_level', 200);
$file = new \SplTempFileObject();
for($i = 0; $i < 500; $i++) {
$file->fwrite("1,2,3\n");
}
$reader = new CsvReader($file);
$reader->rewind();
$reader->setStrict(true);
$reader->setColumnHeaders(array('one','two'));
$current = $reader->current();
$this->assertEquals(null, $current);
if ($xdebug_start) {
xdebug_disable();
}
}
protected function getReader($filename)
{
$file = new \SplFileObject(__DIR__.'/../Fixtures/'.$filename);
return new CsvReader($file);
}
}