1 | <?php |
||
17 | final class ReaderTest extends TestCase |
||
18 | { |
||
19 | private $unreadableFilePath; |
||
20 | |||
21 | public function setUp() |
||
27 | |||
28 | public function tearDown() |
||
32 | |||
33 | /** |
||
34 | * Verify basic usage of Reader. |
||
35 | * |
||
36 | * @test |
||
37 | * @covers ::next |
||
38 | * @covers ::current |
||
39 | * @covers ::key |
||
40 | * @covers ::valid |
||
41 | * @covers ::rewind |
||
42 | * @dataProvider getReaders() |
||
43 | * |
||
44 | * @param Reader $reader The Reader instance to use in the test. |
||
45 | * |
||
46 | * @return void |
||
47 | */ |
||
48 | public function basicUsage(Reader $reader) |
||
84 | |||
85 | /** |
||
86 | * @test |
||
87 | */ |
||
88 | public function readNoHeaders() |
||
89 | { |
||
90 | $expected = [ |
||
91 | [ |
||
92 | 'bk101', |
||
93 | 'Gambardella, Matthew', |
||
94 | 'XML Developer\'s Guide', |
||
95 | 'Computer', |
||
96 | '44.95', |
||
97 | '2000-10-01', |
||
98 | 'An in-depth look at creating applications with XML.', |
||
99 | ], |
||
100 | [ |
||
101 | 'bk102', |
||
102 | 'Ralls, Kim', |
||
103 | 'Midnight Rain', |
||
104 | 'Fantasy', |
||
105 | '5.95', |
||
106 | '2000-12-16', |
||
107 | 'A former architect battles corporate zombies and an evil sorceress.', |
||
108 | ], |
||
109 | [ |
||
110 | 'bk103', |
||
111 | 'Corets, Eva', |
||
112 | 'Maeve Ascendant', |
||
113 | 'Fantasy', |
||
114 | '5.95', |
||
115 | '2000-11-17', |
||
116 | 'Young survivors lay the foundation for a new society in England.', |
||
117 | ], |
||
118 | ]; |
||
119 | |||
120 | $reader = new Reader(__DIR__ . '/_files/no_headers.csv', HeaderStrategy::none()); |
||
121 | foreach ($reader as $key => $row) { |
||
122 | $this->assertSame($expected[$key], $row); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Data provider for basic usage test |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | public function getReaders() |
||
132 | { |
||
133 | $headers = ['id', 'author', 'title', 'genre', 'price', 'publish_date', 'description']; |
||
134 | return [ |
||
135 | [new Reader(__DIR__ . '/_files/basic.csv')], |
||
136 | [new Reader(__DIR__ . '/_files/basic.csv', HeaderStrategy::provide($headers))], |
||
137 | [new Reader(__DIR__ . '/_files/no_headers.csv', HeaderStrategy::provide($headers))], |
||
138 | [ |
||
139 | new Reader( |
||
140 | __DIR__ . '/_files/pipe_delimited.txt', |
||
141 | HeaderStrategy::provide($headers), |
||
142 | new CsvOptions('|') |
||
143 | ) |
||
144 | ], |
||
145 | [ |
||
146 | new Reader( |
||
147 | __DIR__ . '/_files/tab_delimited.txt', |
||
148 | HeaderStrategy::provide($headers), |
||
149 | new CsvOptions("\t") |
||
150 | ) |
||
151 | ], |
||
152 | ]; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Verify parameter checks for $file in __construct(). |
||
157 | * |
||
158 | * @param mixed $file The file parameter to check. |
||
159 | * |
||
160 | * @test |
||
161 | * @covers ::__construct |
||
162 | * @expectedException \InvalidArgumentException |
||
163 | * @expectedExceptionMessage $file must be a string containing a full path to a readable delimited file |
||
164 | * @dataProvider getFiles |
||
165 | * |
||
166 | * @return void |
||
167 | */ |
||
168 | public function constructInvalidFileParam($file) |
||
172 | |||
173 | /** |
||
174 | * Data provider for constructInvalidFileParam() test. |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | public function getFiles() |
||
187 | |||
188 | /** |
||
189 | * Verify behaviour of consecutive rewind(). |
||
190 | * |
||
191 | * @test |
||
192 | * @covers ::rewind |
||
193 | * |
||
194 | * @return void |
||
195 | */ |
||
196 | public function consecutiveRewind() |
||
208 | |||
209 | /** |
||
210 | * Verify basic behaviour of current(). |
||
211 | * |
||
212 | * @test |
||
213 | * @covers ::current |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | public function current() |
||
218 | { |
||
219 | $reader = new Reader(__DIR__ . '/_files/basic.csv'); |
||
220 | $this->assertSame( |
||
221 | [ |
||
222 | 'id' => 'bk101', |
||
223 | 'author' => 'Gambardella, Matthew', |
||
224 | 'title' => 'XML Developer\'s Guide', |
||
225 | 'genre' => 'Computer', |
||
226 | 'price' => '44.95', |
||
227 | 'publish_date' => '2000-10-01', |
||
228 | 'description' => 'An in-depth look at creating applications with XML.', |
||
229 | ], |
||
230 | $reader->current() |
||
231 | ); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Verify behavior of Reader with an empty file |
||
236 | * |
||
237 | * @test |
||
238 | * @covers ::next |
||
239 | * @covers ::current |
||
240 | * @covers ::key |
||
241 | * @covers ::valid |
||
242 | * @covers ::rewind |
||
243 | * @dataProvider getEmptyFiles |
||
244 | * |
||
245 | * @param Reader $reader The reader instance to use in the tests. |
||
246 | * |
||
247 | * @return void |
||
248 | */ |
||
249 | public function emptyFiles(Reader $reader) |
||
261 | |||
262 | /** |
||
263 | * Data provider for empty file test. |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | public function getEmptyFiles() |
||
276 | } |
||
277 |
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.