1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-file |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-file |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Test\Unit\Format\Parser; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Format\CsvFormat; |
17
|
|
|
use Graze\DataFile\Format\CsvFormatInterface; |
18
|
|
|
use Graze\DataFile\Format\Parser\CsvParser; |
19
|
|
|
use Graze\DataFile\Test\Helper\CreateStreamTrait; |
20
|
|
|
use Graze\DataFile\Test\TestCase; |
21
|
|
|
use Mockery as m; |
22
|
|
|
|
23
|
|
|
class CsvParserTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
use CreateStreamTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @dataProvider parseLineData |
29
|
|
|
* |
30
|
|
|
* @param CsvFormatInterface $format |
31
|
|
|
* @param string $line |
32
|
|
|
* @param array $expected |
33
|
|
|
*/ |
34
|
|
|
public function testParse(CsvFormatInterface $format, $line, array $expected) |
35
|
|
|
{ |
36
|
|
|
$parser = new CsvParser($format); |
37
|
|
|
|
38
|
|
|
$iterator = $parser->parse($this->createStream($line)); |
39
|
|
|
$actual = iterator_to_array($iterator); |
40
|
|
|
$actual = array_values(array_map('iterator_to_array', $actual)); |
41
|
|
|
|
42
|
|
|
static::assertEquals($expected, $actual); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function parseLineData() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
[ |
52
|
|
|
new CsvFormat([CsvFormat::OPTION_HEADERS => 0]), |
53
|
|
|
'"a","b",c,1,true,0.2,false,", enclosed","\\n \\r stuff",\\N', |
54
|
|
|
[['a', 'b', 'c', '1', 'true', '0.2', 'false', ', enclosed', "n r stuff", null]], |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
new CsvFormat([ |
58
|
|
|
CsvFormat::OPTION_DELIMITER => '|', |
59
|
|
|
CsvFormat::OPTION_ESCAPE => '~', |
60
|
|
|
CsvFormat::OPTION_QUOTE_CHARACTER => '`', |
61
|
|
|
CsvFormat::OPTION_NULL_OUTPUT => 'null', |
62
|
|
|
CsvFormat::OPTION_HEADERS => 0, |
63
|
|
|
CsvFormat::OPTION_DOUBLE_QUOTE => true, |
64
|
|
|
]), |
65
|
|
|
'`string`|`other,thing`|some stuff|escaped ~\\n|``` all the `` quotes `|null', |
66
|
|
|
[['string', 'other,thing', 'some stuff', 'escaped \n', '` all the ` quotes ', null]], |
67
|
|
|
], |
68
|
|
|
[ |
69
|
|
|
new CsvFormat([ |
70
|
|
|
CsvFormat::OPTION_QUOTE_CHARACTER => "\\", |
71
|
|
|
CsvFormat::OPTION_DELIMITER => '|', |
72
|
|
|
CsvFormat::OPTION_ESCAPE => "\\", |
73
|
|
|
CsvFormat::OPTION_HEADERS => 0, |
74
|
|
|
]), |
75
|
|
|
'a|b|c|d\\|e', |
76
|
|
|
[['a', 'b', 'c', 'd|e']], |
77
|
|
|
], |
78
|
|
|
[ |
79
|
|
|
new CsvFormat([ |
80
|
|
|
CsvFormat::OPTION_HEADERS => 1, |
81
|
|
|
CsvFormat::OPTION_DOUBLE_QUOTE => true, |
82
|
|
|
]), |
83
|
|
|
file_get_contents(__DIR__ . '/../../../fixtures/csv_file.csv'), |
84
|
|
|
[ |
85
|
|
|
['0', 'my name', 'i like " quotes', 'question?'], |
86
|
|
|
['1', 'your name', 'potatoes! ' . "\n" . 'and stuff', 'think'], |
87
|
|
|
['2', 'your , nice', 'fish"', '","'], |
88
|
|
|
], |
89
|
|
|
], |
90
|
|
|
[ |
91
|
|
|
new CsvFormat([ |
92
|
|
|
CsvFormat::OPTION_HEADERS => 0, |
93
|
|
|
CsvFormat::OPTION_LIMIT => 2, |
94
|
|
|
CsvFormat::OPTION_DOUBLE_QUOTE => true, |
95
|
|
|
]), |
96
|
|
|
file_get_contents(__DIR__ . '/../../../fixtures/csv_file.csv'), |
97
|
|
|
[ |
98
|
|
|
['id', 'name', 'things', 'stuff'], |
99
|
|
|
['0', 'my name', 'i like " quotes', 'question?'], |
100
|
|
|
], |
101
|
|
|
], |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|