1
|
|
|
<?php |
2
|
|
|
namespace SubjectivePHPTest\Csv; |
3
|
|
|
|
4
|
|
|
use SubjectivePHP\Csv\CsvOptions; |
5
|
|
|
use SubjectivePHP\Csv\DeriveHeaderStrategy; |
6
|
|
|
use SubjectivePHP\Csv\NoHeaderStrategy; |
7
|
|
|
use SubjectivePHP\Csv\ProvidedHeaderStrategy; |
8
|
|
|
use SubjectivePHP\Csv\Reader; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Unit tests for the SubjectivePHP\Csv\Reader class |
13
|
|
|
* |
14
|
|
|
* @coversDefaultClass \SubjectivePHP\Csv\Reader |
15
|
|
|
* @covers ::__construct |
16
|
|
|
* @covers ::__destruct |
17
|
|
|
* @covers ::<private> |
18
|
|
|
*/ |
19
|
|
|
final class ReaderTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
private $unreadableFilePath; |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->unreadableFilePath = tempnam(sys_get_temp_dir(), 'csv'); |
26
|
|
|
touch($this->unreadableFilePath); |
27
|
|
|
chmod($this->unreadableFilePath, 0220); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function tearDown() |
31
|
|
|
{ |
32
|
|
|
unlink($this->unreadableFilePath); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Verify basic usage of Reader. |
37
|
|
|
* |
38
|
|
|
* @test |
39
|
|
|
* @covers ::next |
40
|
|
|
* @covers ::current |
41
|
|
|
* @covers ::key |
42
|
|
|
* @covers ::valid |
43
|
|
|
* @covers ::rewind |
44
|
|
|
* @dataProvider getReaders() |
45
|
|
|
* |
46
|
|
|
* @param Reader $reader The Reader instance to use in the test. |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function basicUsage(Reader $reader) |
51
|
|
|
{ |
52
|
|
|
$expected = [ |
53
|
|
|
[ |
54
|
|
|
'id' => 'bk101', |
55
|
|
|
'author' => 'Gambardella, Matthew', |
56
|
|
|
'title' => 'XML Developer\'s Guide', |
57
|
|
|
'genre' => 'Computer', |
58
|
|
|
'price' => '44.95', |
59
|
|
|
'publish_date' => '2000-10-01', |
60
|
|
|
'description' => 'An in-depth look at creating applications with XML.', |
61
|
|
|
], |
62
|
|
|
[ |
63
|
|
|
'id' => 'bk102', |
64
|
|
|
'author' => 'Ralls, Kim', |
65
|
|
|
'title' => 'Midnight Rain', |
66
|
|
|
'genre' => 'Fantasy', |
67
|
|
|
'price' => '5.95', |
68
|
|
|
'publish_date' => '2000-12-16', |
69
|
|
|
'description' => 'A former architect battles corporate zombies and an evil sorceress.', |
70
|
|
|
], |
71
|
|
|
[ |
72
|
|
|
'id' => 'bk103', |
73
|
|
|
'author' => 'Corets, Eva', |
74
|
|
|
'title' => 'Maeve Ascendant', |
75
|
|
|
'genre' => 'Fantasy', |
76
|
|
|
'price' => '5.95', |
77
|
|
|
'publish_date' => '2000-11-17', |
78
|
|
|
'description' => 'Young survivors lay the foundation for a new society in England.', |
79
|
|
|
], |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
foreach ($reader as $key => $row) { |
83
|
|
|
$this->assertSame($expected[$key], $row); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @test |
89
|
|
|
*/ |
90
|
|
|
public function readNoHeaders() |
91
|
|
|
{ |
92
|
|
|
$expected = [ |
93
|
|
|
[ |
94
|
|
|
'bk101', |
95
|
|
|
'Gambardella, Matthew', |
96
|
|
|
'XML Developer\'s Guide', |
97
|
|
|
'Computer', |
98
|
|
|
'44.95', |
99
|
|
|
'2000-10-01', |
100
|
|
|
'An in-depth look at creating applications with XML.', |
101
|
|
|
], |
102
|
|
|
[ |
103
|
|
|
'bk102', |
104
|
|
|
'Ralls, Kim', |
105
|
|
|
'Midnight Rain', |
106
|
|
|
'Fantasy', |
107
|
|
|
'5.95', |
108
|
|
|
'2000-12-16', |
109
|
|
|
'A former architect battles corporate zombies and an evil sorceress.', |
110
|
|
|
], |
111
|
|
|
[ |
112
|
|
|
'bk103', |
113
|
|
|
'Corets, Eva', |
114
|
|
|
'Maeve Ascendant', |
115
|
|
|
'Fantasy', |
116
|
|
|
'5.95', |
117
|
|
|
'2000-11-17', |
118
|
|
|
'Young survivors lay the foundation for a new society in England.', |
119
|
|
|
], |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
$reader = new Reader(__DIR__ . '/_files/no_headers.csv', new NoHeaderStrategy()); |
123
|
|
|
foreach ($reader as $key => $row) { |
124
|
|
|
$this->assertSame($expected[$key], $row); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Data provider for basic usage test |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function getReaders() |
134
|
|
|
{ |
135
|
|
|
$headers = ['id', 'author', 'title', 'genre', 'price', 'publish_date', 'description']; |
136
|
|
|
return [ |
137
|
|
|
[new Reader(__DIR__ . '/_files/basic.csv')], |
138
|
|
|
[new Reader(__DIR__ . '/_files/basic.csv', new ProvidedHeaderStrategy($headers))], |
139
|
|
|
[new Reader(__DIR__ . '/_files/no_headers.csv', new ProvidedHeaderStrategy($headers))], |
140
|
|
|
[ |
141
|
|
|
new Reader( |
142
|
|
|
__DIR__ . '/_files/pipe_delimited.txt', |
143
|
|
|
new ProvidedHeaderStrategy($headers), |
144
|
|
|
new CsvOptions('|') |
145
|
|
|
) |
146
|
|
|
], |
147
|
|
|
[ |
148
|
|
|
new Reader( |
149
|
|
|
__DIR__ . '/_files/tab_delimited.txt', |
150
|
|
|
new ProvidedHeaderStrategy($headers), |
151
|
|
|
new CsvOptions("\t") |
152
|
|
|
) |
153
|
|
|
], |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Verify parameter checks for $file in __construct(). |
159
|
|
|
* |
160
|
|
|
* @param mixed $file The file parameter to check. |
161
|
|
|
* |
162
|
|
|
* @test |
163
|
|
|
* @covers ::__construct |
164
|
|
|
* @expectedException \InvalidArgumentException |
165
|
|
|
* @expectedExceptionMessage $file must be a string containing a full path to a readable delimited file |
166
|
|
|
* @dataProvider getFiles |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function constructInvalidFileParam($file) |
171
|
|
|
{ |
172
|
|
|
$reader = new Reader($file); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Data provider for constructInvalidFileParam() test. |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
public function getFiles() |
181
|
|
|
{ |
182
|
|
|
return [ |
183
|
|
|
[__DIR__ . '/_files/not_readable.csv'], |
184
|
|
|
[true], |
185
|
|
|
[null], |
186
|
|
|
[__DIR__ . '/_files/doesnotexist.csv'], |
187
|
|
|
]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Verify behaviour of consecutive rewind(). |
192
|
|
|
* |
193
|
|
|
* @test |
194
|
|
|
* @covers ::rewind |
195
|
|
|
* |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
|
|
public function consecutiveRewind() |
199
|
|
|
{ |
200
|
|
|
$reader = new Reader(__DIR__ . '/_files/basic.csv'); |
201
|
|
|
$count = 0; |
202
|
|
|
foreach ($reader as $row) { |
203
|
|
|
$count++; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$reader->rewind(); |
207
|
|
|
$reader->rewind(); |
208
|
|
|
$this->assertSame(0, $reader->key()); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Verify basic behaviour of current(). |
213
|
|
|
* |
214
|
|
|
* @test |
215
|
|
|
* @covers ::current |
216
|
|
|
* |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public function current() |
220
|
|
|
{ |
221
|
|
|
$reader = new Reader(__DIR__ . '/_files/basic.csv'); |
222
|
|
|
$this->assertSame( |
223
|
|
|
[ |
224
|
|
|
'id' => 'bk101', |
225
|
|
|
'author' => 'Gambardella, Matthew', |
226
|
|
|
'title' => 'XML Developer\'s Guide', |
227
|
|
|
'genre' => 'Computer', |
228
|
|
|
'price' => '44.95', |
229
|
|
|
'publish_date' => '2000-10-01', |
230
|
|
|
'description' => 'An in-depth look at creating applications with XML.', |
231
|
|
|
], |
232
|
|
|
$reader->current() |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Verify behavior of Reader with an empty file |
238
|
|
|
* |
239
|
|
|
* @test |
240
|
|
|
* @covers ::next |
241
|
|
|
* @covers ::current |
242
|
|
|
* @covers ::key |
243
|
|
|
* @covers ::valid |
244
|
|
|
* @covers ::rewind |
245
|
|
|
* @dataProvider getEmptyFiles |
246
|
|
|
* |
247
|
|
|
* @param Reader $reader The reader instance to use in the tests. |
248
|
|
|
* |
249
|
|
|
* @return void |
250
|
|
|
*/ |
251
|
|
|
public function emptyFiles(Reader $reader) |
252
|
|
|
{ |
253
|
|
|
$total = 0; |
254
|
|
|
|
255
|
|
|
$reader->rewind(); |
256
|
|
|
while ($reader->valid()) { |
257
|
|
|
$total++; |
258
|
|
|
$reader->next(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$this->assertSame(0, $total); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Data provider for empty file test. |
266
|
|
|
* |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
public function getEmptyFiles() |
270
|
|
|
{ |
271
|
|
|
$headers = ['id', 'author', 'title', 'genre', 'price', 'publish_date', 'description']; |
272
|
|
|
return [ |
273
|
|
|
[new Reader(__DIR__ . '/_files/empty.csv')], |
274
|
|
|
[new Reader(__DIR__ . '/_files/headers_only.csv')], |
275
|
|
|
[new Reader(__DIR__ . '/_files/headers_only.csv', new ProvidedHeaderStrategy($headers))], |
276
|
|
|
]; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.