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