|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace itertools; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Stream\Stream; |
|
6
|
|
|
use PHPUnit_Framework_TestCase; |
|
7
|
|
|
use SplFileInfo; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class FileCsvIteratorTest extends PHPUnit_Framework_TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** @test */ |
|
13
|
|
|
public function testFileCsvIteratorWithHeaderCombinedHeader() |
|
14
|
|
|
{ |
|
15
|
|
|
$fp = $this->getMemoryFileHandle(<<<EOF |
|
16
|
|
|
"", "cat1", "", "cat2" |
|
17
|
|
|
"col1", "col2", "col3", "col4" |
|
18
|
|
|
"a11", "a12", "a13", "a14" |
|
19
|
|
|
EOF |
|
20
|
|
|
); |
|
21
|
|
|
|
|
22
|
|
|
$data = iterator_to_array(new FileCsvIterator($fp, array('combineFirstNRowsAsHeader' => 2))); |
|
23
|
|
|
$this->assertEquals(1, count($data)); |
|
24
|
|
|
$this->assertEquals('a11', $data[0]['col1']); |
|
25
|
|
|
$this->assertEquals('a12', $data[0]['cat1 col2']); |
|
26
|
|
|
$this->assertEquals('a13', $data[0]['cat1 col3']); |
|
27
|
|
|
$this->assertEquals('a14', $data[0]['cat2 col4']); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** @test */ |
|
31
|
|
|
public function testFileCsvIteratorWithHeader() |
|
32
|
|
|
{ |
|
33
|
|
|
$fp = $this->getMemoryFileHandle(<<<EOF |
|
34
|
|
|
"col1", "col2" |
|
35
|
|
|
"a11", "a12" |
|
36
|
|
|
"a21", "a22" |
|
37
|
|
|
EOF |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
$data = iterator_to_array(new FileCsvIterator($fp)); |
|
41
|
|
|
$this->assertEquals(2, count($data)); |
|
42
|
|
|
$this->assertEquals('a22', $data[1]['col2']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** @test */ |
|
46
|
|
|
public function testFileCsvIteratorWithHeaderAndNoContent() |
|
47
|
|
|
{ |
|
48
|
|
|
$fp = $this->getMemoryFileHandle(<<<EOF |
|
49
|
|
|
"col1", "col2" |
|
50
|
|
|
EOF |
|
51
|
|
|
); |
|
52
|
|
|
$data = iterator_to_array(new FileCsvIterator($fp)); |
|
53
|
|
|
$this->assertEquals(0, count($data)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** @test */ |
|
57
|
|
|
public function testFileCsvIteratorWithHeaderAndNoData() |
|
58
|
|
|
{ |
|
59
|
|
|
$fp = $this->getMemoryFileHandle(<<<EOF |
|
60
|
|
|
EOF |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$data = iterator_to_array(new FileCsvIterator($fp)); |
|
64
|
|
|
$this->assertEquals(0, count($data)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** @test */ |
|
68
|
|
|
public function testFileCsvIteratorFromSplFileInfo() |
|
69
|
|
|
{ |
|
70
|
|
|
$data = iterator_to_array(new FileCsvIterator(new SplFileInfo(__DIR__ . '/testdata/testcsv.csv'))); |
|
71
|
|
|
$this->assertCount(2, $data); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** @test */ |
|
75
|
|
|
public function testFileCsvIteratorWithoutHeader() |
|
76
|
|
|
{ |
|
77
|
|
|
$fp = $this->getMemoryFileHandle(<<<EOF |
|
78
|
|
|
"col1", "col2" |
|
79
|
|
|
"a11", "a12" |
|
80
|
|
|
EOF |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$data = iterator_to_array(new FileCsvIterator($fp, array('hasHeader' => false))); |
|
84
|
|
|
$this->assertEquals(2, count($data)); |
|
85
|
|
|
$this->assertEquals('col1', $data[0][0]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** @test */ |
|
89
|
|
|
public function testFileCsvIteratorWithReplacedHeader() |
|
90
|
|
|
{ |
|
91
|
|
|
$fp = $this->getMemoryFileHandle(<<<EOF |
|
92
|
|
|
"col1", "col2" |
|
93
|
|
|
"a11", "a12" |
|
94
|
|
|
EOF |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
$data = iterator_to_array(new FileCsvIterator($fp, array('hasHeader' => true, 'header' => array(1, 'bla')))); |
|
98
|
|
|
$this->assertEquals(1, count($data)); |
|
99
|
|
|
$this->assertEquals('a11', $data[0][1]); |
|
100
|
|
|
$this->assertEquals('a12', $data[0]['bla']); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** @test */ |
|
104
|
|
|
public function testFileCsvIteratorWithEnclosureSameAsDelimiter() |
|
105
|
|
|
{ |
|
106
|
|
|
$fp = $this->getMemoryFileHandle(<<<EOF |
|
107
|
|
|
col1,col2 |
|
108
|
|
|
a11,a12 |
|
109
|
|
|
"a2""1",a22 |
|
110
|
|
|
EOF |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
$data = iterator_to_array(new FileCsvIterator($fp, array('escape' => '"', 'enclosure' => '"'))); |
|
114
|
|
|
$this->assertEquals(2, count($data)); |
|
115
|
|
|
$this->assertEquals('a11', $data[0]['col1']); |
|
116
|
|
|
$this->assertEquals('a2"1', $data[1]['col1']); |
|
117
|
|
|
$this->assertEquals('a22', $data[1]['col2']); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @test |
|
122
|
|
|
* @expectedException InvalidArgumentException |
|
123
|
|
|
*/ |
|
124
|
|
|
public function testFileCsvIteratorWithInvalidArguments() |
|
125
|
|
|
{ |
|
126
|
|
|
new FileCsvIterator(1); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** @test */ |
|
130
|
|
|
public function testClosingHandle() |
|
131
|
|
|
{ |
|
132
|
|
|
$lines = new FileCsvIterator(__FILE__); |
|
133
|
|
|
$lines->__destruct(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @test |
|
138
|
|
|
* @expectedException InvalidArgumentException |
|
139
|
|
|
*/ |
|
140
|
|
|
public function testFileCsvIteratorWithNonExistingFilePath() |
|
141
|
|
|
{ |
|
142
|
|
|
new FileCsvIterator(uniqid()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @test |
|
147
|
|
|
* @expectedException InvalidArgumentException |
|
148
|
|
|
*/ |
|
149
|
|
|
public function testFileCsvIteratorWithInvalidOptions() |
|
150
|
|
|
{ |
|
151
|
|
|
new FileCsvIterator(__FILE__, array('unknownOption' => true)); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** @test */ |
|
155
|
|
|
public function testFileCsvWithCustomCharacterEncoding() |
|
156
|
|
|
{ |
|
157
|
|
|
$data = iterator_to_array(new FileCsvIterator(__DIR__ . '/testdata/testcsv-latin1.csv', array('fromEncoding' => 'ISO-8859-1'))); |
|
158
|
|
|
$this->assertEquals("Josephin\xc3\xa8", $data[0]['name']); |
|
159
|
|
|
$this->assertEquals("Albr\xc3\xa9ne", $data[1]['name']); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function testFileCsvIteratorWithStreamInterface() |
|
163
|
|
|
{ |
|
164
|
|
|
$tmpFile = tmpfile(); |
|
165
|
|
|
fwrite($tmpFile, <<<EOF |
|
166
|
|
|
"col1", "col2" |
|
167
|
|
|
"a11", "a12" |
|
168
|
|
|
"a21", "a22" |
|
169
|
|
|
EOF |
|
170
|
|
|
); |
|
171
|
|
|
rewind($tmpFile); |
|
172
|
|
|
$stream = new Stream($tmpFile); |
|
173
|
|
|
|
|
174
|
|
|
$fileCsvIterator = new FileCsvIterator($stream); |
|
175
|
|
|
unset($stream); |
|
176
|
|
|
$data = iterator_to_array($fileCsvIterator); |
|
177
|
|
|
$this->assertCount(2, $data); |
|
178
|
|
|
$this->assertEquals('a22', $data[1]['col2']); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
protected function getMemoryFileHandle($content) |
|
182
|
|
|
{ |
|
183
|
|
|
$fp = fopen('php://memory', 'rw'); |
|
184
|
|
|
fwrite($fp, $content); |
|
185
|
|
|
rewind($fp); |
|
186
|
|
|
return $fp; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
|