Failed Conditions
Pull Request — master (#6)
by Chad
02:38
created

tests/ReaderTest.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    /**
18
     * Verify basic usage of Reader.
19
     *
20
     * @test
21
     * @covers ::next
22
     * @covers ::current
23
     * @covers ::key
24
     * @covers ::valid
25
     * @covers ::rewind
26
     * @dataProvider getReaders()
27
     *
28
     * @param Reader $reader The Reader instance to use in the test.
29
     *
30
     * @return void
31
     */
32
    public function basicUsage(Reader $reader)
33
    {
34
        $expected = [
35
            [
36
                'id' => 'bk101',
37
                'author' => 'Gambardella, Matthew',
38
                'title' => 'XML Developer\'s Guide',
39
                'genre' => 'Computer',
40
                'price' => '44.95',
41
                'publish_date' => '2000-10-01',
42
                'description' => 'An in-depth look at creating applications with XML.',
43
            ],
44
            [
45
                'id' => 'bk102',
46
                'author' => 'Ralls, Kim',
47
                'title' => 'Midnight Rain',
48
                'genre' => 'Fantasy',
49
                'price' => '5.95',
50
                'publish_date' => '2000-12-16',
51
                'description' => 'A former architect battles corporate zombies and an evil sorceress.',
52
            ],
53
            [
54
                'id' => 'bk103',
55
                'author' => 'Corets, Eva',
56
                'title' => 'Maeve Ascendant',
57
                'genre' => 'Fantasy',
58
                'price' => '5.95',
59
                'publish_date' => '2000-11-17',
60
                'description' => 'Young survivors lay the foundation for a new society in England.',
61
            ],
62
        ];
63
64
        foreach ($reader as $key => $row) {
65
            $this->assertSame($expected[$key], $row);
66
        }
67
    }
68
69
    /**
70
     * Data provider for basic usage test
71
     *
72
     * @return array
73
     */
74
    public function getReaders()
75
    {
76
        $headers = ['id', 'author', 'title', 'genre', 'price', 'publish_date', 'description'];
77
        return [
78
            [new Reader(__DIR__ . '/_files/basic.csv')],
79
            [new Reader(__DIR__ . '/_files/basic.csv', $headers)],
80
            [new Reader(__DIR__ . '/_files/no_headers.csv', $headers)],
81
            [new Reader(__DIR__ . '/_files/pipe_delimited.txt', $headers, '|')],
82
            [new Reader(__DIR__ . '/_files/tab_delimited.txt', $headers, "\t")],
83
        ];
84
    }
85
86
    /**
87
     * Verify parameter checks for $file in __construct().
88
     *
89
     * @param mixed $file The file parameter to check.
90
     *
91
     * @test
92
     * @covers ::__construct
93
     * @expectedException \InvalidArgumentException
94
     * @expectedExceptionMessage $file must be a string containing a full path to a readable delimited file
95
     * @dataProvider getFiles
96
     *
97
     * @return void
98
     */
99
    public function constructInvalidFileParam($file)
100
    {
101
        $reader = new Reader($file);
0 ignored issues
show
$reader is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
102
    }
103
104
    /**
105
     * Data provider for constructInvalidFileParam() test.
106
     *
107
     * @return array
108
     */
109
    public function getFiles()
110
    {
111
        chmod(__DIR__ . '/_files/not_readable.csv', 0220);
112
        return [
113
            [__DIR__ . '/_files/not_readable.csv'],
114
            [__DIR__ . '/_files/doesnotexist.csv'],
115
        ];
116
    }
117
118
    /**
119
     * Verify behavior of __construct with an invalid delimiter.
120
     *
121
     * @test
122
     * @covers ::__construct
123
     * @expectedException \InvalidArgumentException
124
     * @expectedExceptionMessage $delimiter must be a single character string
125
     *
126
     * @return void
127
     */
128
    public function constructInvalidDelimiter()
129
    {
130
        new Reader(__DIR__ . '/_files/basic.csv', null, 'too long');
131
    }
132
133
    /**
134
     * Verify behavior of __construct with an invalid enclosure.
135
     *
136
     * @test
137
     * @covers ::__construct
138
     * @expectedException \InvalidArgumentException
139
     * @expectedExceptionMessage $enclosure must be a single character string
140
     *
141
     * @return void
142
     */
143
    public function constructInvalidEnclosure()
144
    {
145
        new Reader(__DIR__ . '/_files/basic.csv', null, ',', '"""');
146
    }
147
148
    /**
149
     * Verify behavior of __construct with an invalid escapeChar.
150
     *
151
     * @test
152
     * @covers ::__construct
153
     * @expectedException \InvalidArgumentException
154
     * @expectedExceptionMessage $escapeChar must be a single character string
155
     *
156
     * @return void
157
     */
158
    public function constructInvalidEscapeChar()
159
    {
160
        new Reader(__DIR__ . '/_files/basic.csv', null, ',', '"', 'abc');
161
    }
162
163
    /**
164
     * Verify behaviour of consecutive rewind().
165
     *
166
     * @test
167
     * @covers ::rewind
168
     *
169
     * @return void
170
     */
171
    public function consecutiveRewind()
172
    {
173
        $reader = new Reader(__DIR__ . '/_files/basic.csv');
174
        $count = 0;
175
        foreach ($reader as $row) {
176
            $count++;
177
        }
178
179
        $reader->rewind();
180
        $reader->rewind();
181
        $this->assertSame(0, $reader->key());
182
    }
183
184
    /**
185
     * Verify basic behaviour of current().
186
     *
187
     * @test
188
     * @covers ::current
189
     *
190
     * @return void
191
     */
192
    public function current()
193
    {
194
        $reader = new Reader(__DIR__ . '/_files/basic.csv');
195
        $this->assertSame(
196
            [
197
                'id' => 'bk101',
198
                'author' => 'Gambardella, Matthew',
199
                'title' => 'XML Developer\'s Guide',
200
                'genre' => 'Computer',
201
                'price' => '44.95',
202
                'publish_date' => '2000-10-01',
203
                'description' => 'An in-depth look at creating applications with XML.',
204
            ],
205
            $reader->current()
206
        );
207
    }
208
209
    /**
210
     * Verify behavior of Reader with an empty file
211
     *
212
     * @test
213
     * @covers ::next
214
     * @covers ::current
215
     * @covers ::key
216
     * @covers ::valid
217
     * @covers ::rewind
218
     * @dataProvider getEmptyFiles
219
     *
220
     * @param Reader $reader The reader instance to use in the tests.
221
     *
222
     * @return void
223
     */
224
    public function emptyFiles(Reader $reader)
225
    {
226
        $total = 0;
227
228
        $reader->rewind();
229
        while ($reader->valid()) {
230
            $total++;
231
            $reader->next();
232
        }
233
234
        $this->assertSame(0, $total);
235
    }
236
237
    /**
238
     * Data provider for empty file test.
239
     *
240
     * @return array
241
     */
242
    public function getEmptyFiles()
243
    {
244
        $headers = ['id', 'author', 'title', 'genre', 'price', 'publish_date', 'description'];
245
        return [
246
            [new Reader(__DIR__ . '/_files/empty.csv')],
247
            [new Reader(__DIR__ . '/_files/headers_only.csv')],
248
            [new Reader(__DIR__ . '/_files/headers_only.csv', $headers)],
249
        ];
250
    }
251
252
    /**
253
     * @test
254
     * @covers ::getFilePath
255
     */
256
    public function getFilePath()
257
    {
258
         $reader = new Reader(__DIR__ . '/_files/basic.csv');
259
         $this->assertSame(__DIR__ . '/_files/basic.csv', $reader->getFilePath());
260
    }
261
}
262