Completed
Pull Request — master (#6)
by Chad
01:26
created

readerCreatesNumericHeadersIfNoneGiven()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 26
nc 2
nop 0
1
<?php
2
namespace SubjectivePHPTest\Csv;
3
4
use SubjectivePHP\Csv\CsvOptions;
5
use SubjectivePHP\Csv\Reader;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * Unit tests for the SubjectivePHP\Csv\Reader class
10
 *
11
 * @coversDefaultClass \SubjectivePHP\Csv\Reader
12
 * @covers ::__construct
13
 * @covers ::__destruct
14
 * @covers ::<private>
15
 */
16
final class ReaderTest extends TestCase
17
{
18
    /**
19
     */
20
    public function readerCreatesNumericHeadersIfNoneGiven()
21
    {
22
        $reader = new Reader(__DIR__ . '/_files/no_headers.csv', new CsvOptions());
0 ignored issues
show
Documentation introduced by
new \SubjectivePHP\Csv\CsvOptions() is of type object<SubjectivePHP\Csv\CsvOptions>, but the function expects a null|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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