Completed
Push — master ( e42624...c35825 )
by Harry
9s
created

CsvParserTest::parseFailureData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Unit\Format\Parser;
15
16
use Graze\DataFile\Format\CsvFormat;
17
use Graze\DataFile\Format\CsvFormatInterface;
18
use Graze\DataFile\Format\Parser\CsvParser;
19
use Graze\DataFile\Test\TestCase;
20
use GuzzleHttp\Psr7\Stream;
21
use Mockery as m;
22
use Psr\Http\Message\StreamInterface;
23
use RuntimeException;
24
25
class CsvParserTest extends TestCase
26
{
27
    /**
28
     * @dataProvider parseLineData
29
     *
30
     * @param CsvFormatInterface $format
31
     * @param string             $line
32
     * @param array              $expected
33
     */
34 View Code Duplication
    public function testParse(CsvFormatInterface $format, $line, array $expected)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $parser = new CsvParser($format);
37
38
        $iterator = $parser->parse($this->createStream($line));
39
        $actual = iterator_to_array($iterator);
40
        $actual = array_values(array_map('iterator_to_array', $actual));
41
42
        static::assertEquals($expected, $actual);
43
    }
44
45
    /**
46
     * @param string $string
47
     *
48
     * @return StreamInterface
49
     */
50 View Code Duplication
    protected function createStream($string)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $stream = fopen('php://memory', 'r+');
53
        fwrite($stream, $string);
54
        rewind($stream);
55
        return new Stream($stream);
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function parseLineData()
62
    {
63
        return [
64
            [
65
                new CsvFormat([CsvFormat::OPTION_HEADER_ROW => 0]),
66
                '"a","b",c,1,true,0.2,false,", enclosed","\\n \\r stuff",\\N',
67
                [['a', 'b', 'c', '1', 'true', '0.2', 'false', ', enclosed', "n r stuff", null]],
68
            ],
69
            [
70
                new CsvFormat([
71
                    CsvFormat::OPTION_DELIMITER       => '|',
72
                    CsvFormat::OPTION_ESCAPE          => '~',
73
                    CsvFormat::OPTION_QUOTE_CHARACTER => '`',
74
                    CsvFormat::OPTION_NULL_OUTPUT     => 'null',
75
                    CsvFormat::OPTION_HEADER_ROW      => 0,
76
                    CsvFormat::OPTION_DOUBLE_QUOTE    => true,
77
                ]),
78
                '`string`|`other,thing`|some stuff|escaped ~\\n|``` all the `` quotes `|null',
79
                [['string', 'other,thing', 'some stuff', 'escaped \n', '` all the ` quotes ', null]],
80
            ],
81
            [
82
                new CsvFormat([
83
                    CsvFormat::OPTION_QUOTE_CHARACTER => "\\",
84
                    CsvFormat::OPTION_DELIMITER       => '|',
85
                    CsvFormat::OPTION_ESCAPE          => "\\",
86
                    CsvFormat::OPTION_HEADER_ROW      => 0,
87
                ]),
88
                'a|b|c|d\\|e',
89
                [['a', 'b', 'c', 'd|e']],
90
            ],
91
            [
92
                new CsvFormat([
93
                    CsvFormat::OPTION_HEADER_ROW   => 1,
94
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
95
                ]),
96
                file_get_contents(__DIR__ . '/../../../fixtures/csv_file.csv'),
97
                [
98
                    [
99
                        'id'     => '0',
100
                        'name'   => 'my name',
101
                        'things' => 'i like " quotes',
102
                        'stuff'  => 'question?',
103
                    ],
104
                    [
105
                        'id'     => '1',
106
                        'name'   => 'your name',
107
                        'things' => 'potatoes! ' . "\n" . 'and stuff',
108
                        'stuff'  => 'think',
109
                    ],
110
                    [
111
                        'id'     => '2',
112
                        'name'   => 'your , nice',
113
                        'things' => 'fish"',
114
                        'stuff'  => '","',
115
                    ],
116
                ],
117
            ],
118
            [
119
                new CsvFormat([
120
                    CsvFormat::OPTION_DATA_START   => 1,
121
                    CsvFormat::OPTION_LIMIT        => 2,
122
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
123
                ]),
124
                file_get_contents(__DIR__ . '/../../../fixtures/csv_file.csv'),
125
                [
126
                    ['id', 'name', 'things', 'stuff'],
127
                    ['0', 'my name', 'i like " quotes', 'question?'],
128
                ],
129
            ],
130
            [
131
                new CsvFormat([
132
                    CsvFormat::OPTION_DATA_START   => 4,
133
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
134
                ]),
135
                file_get_contents(__DIR__ . '/../../../fixtures/csv_file.csv'),
136
                [
137
                    ['2', 'your , nice', 'fish"', '","'],
138
                ],
139
            ],
140
            [
141
                new CsvFormat([
142
                    CsvFormat::OPTION_HEADER_ROW   => 1,
143
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
144
                ]),
145
                "\n" . '"1","2","3","4","5"',
146
                [
147
                    ['1', '2', '3', '4', '5'],
148
                ],
149
            ],
150
        ];
151
    }
152
153
    /**
154
     * @dataProvider parseFailureData
155
     *
156
     * @param CsvFormatInterface $format
157
     * @param string             $line
158
     * @param string             $expected
159
     */
160 View Code Duplication
    public function testParseFailure(CsvFormatInterface $format, $line, $expected)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        $parser = new CsvParser($format);
163
164
        $iterator = $parser->parse($this->createStream($line));
165
166
        static::expectException($expected);
167
        $actual = iterator_to_array($iterator);
168
        $actual = array_values(array_map('iterator_to_array', $actual));
169
170
        static::assertEquals($expected, $actual);
171
    }
172
173
    /**
174
     * @return array
175
     */
176
    public function parseFailureData()
177
    {
178
        return [
179
            [
180
                new CsvFormat([
181
                    CsvFormat::OPTION_HEADER_ROW   => 1,
182
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
183
                ]),
184
                '"id","fish","cake"' . "\n" . '"1","name","thing","too many"',
185
                RuntimeException::class,
186
            ],
187
        ];
188
    }
189
}
190