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