Completed
Pull Request — master (#4)
by Harry
04:07
created

CsvParserTest::parseLineData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 91
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 91
rs 8.518
cc 1
eloc 54
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Helper\CreateStreamTrait;
20
use Graze\DataFile\Test\TestCase;
21
use Mockery as m;
22
use RuntimeException;
23
24
class CsvParserTest extends TestCase
25
{
26
    use CreateStreamTrait;
27
28
    /**
29
     * @dataProvider parseLineData
30
     *
31
     * @param CsvFormatInterface $format
32
     * @param string             $line
33
     * @param array              $expected
34
     */
35 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...
36
    {
37
        $parser = new CsvParser($format);
38
39
        $iterator = $parser->parse($this->createStream($line));
40
        $actual = iterator_to_array($iterator);
41
        $actual = array_values(array_map('iterator_to_array', $actual));
42
43
        static::assertEquals($expected, $actual);
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function parseLineData()
50
    {
51
        return [
52
            [
53
                new CsvFormat([CsvFormat::OPTION_HEADER_ROW => 0]),
54
                '"a","b",c,1,true,0.2,false,", enclosed","\\n \\r stuff",\\N',
55
                [['a', 'b', 'c', '1', 'true', '0.2', 'false', ', enclosed', "n r stuff", null]],
56
            ],
57
            [
58
                new CsvFormat([
59
                    CsvFormat::OPTION_DELIMITER       => '|',
60
                    CsvFormat::OPTION_ESCAPE          => '~',
61
                    CsvFormat::OPTION_QUOTE_CHARACTER => '`',
62
                    CsvFormat::OPTION_NULL_OUTPUT     => 'null',
63
                    CsvFormat::OPTION_HEADER_ROW      => 0,
64
                    CsvFormat::OPTION_DOUBLE_QUOTE    => true,
65
                ]),
66
                '`string`|`other,thing`|some stuff|escaped ~\\n|``` all the `` quotes `|null',
67
                [['string', 'other,thing', 'some stuff', 'escaped \n', '` all the ` quotes ', null]],
68
            ],
69
            [
70
                new CsvFormat([
71
                    CsvFormat::OPTION_QUOTE_CHARACTER => "\\",
72
                    CsvFormat::OPTION_DELIMITER       => '|',
73
                    CsvFormat::OPTION_ESCAPE          => "\\",
74
                    CsvFormat::OPTION_HEADER_ROW      => 0,
75
                ]),
76
                'a|b|c|d\\|e',
77
                [['a', 'b', 'c', 'd|e']],
78
            ],
79
            [
80
                new CsvFormat([
81
                    CsvFormat::OPTION_HEADER_ROW   => 1,
82
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
83
                ]),
84
                file_get_contents(__DIR__ . '/../../../fixtures/csv_file.csv'),
85
                [
86
                    [
87
                        'id'     => '0',
88
                        'name'   => 'my name',
89
                        'things' => 'i like " quotes',
90
                        'stuff'  => 'question?',
91
                    ],
92
                    [
93
                        'id'     => '1',
94
                        'name'   => 'your name',
95
                        'things' => 'potatoes! ' . "\n" . 'and stuff',
96
                        'stuff'  => 'think',
97
                    ],
98
                    [
99
                        'id'     => '2',
100
                        'name'   => 'your , nice',
101
                        'things' => 'fish"',
102
                        'stuff'  => '","',
103
                    ],
104
                ],
105
            ],
106
            [
107
                new CsvFormat([
108
                    CsvFormat::OPTION_DATA_START   => 1,
109
                    CsvFormat::OPTION_LIMIT        => 2,
110
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
111
                ]),
112
                file_get_contents(__DIR__ . '/../../../fixtures/csv_file.csv'),
113
                [
114
                    ['id', 'name', 'things', 'stuff'],
115
                    ['0', 'my name', 'i like " quotes', 'question?'],
116
                ],
117
            ],
118
            [
119
                new CsvFormat([
120
                    CsvFormat::OPTION_DATA_START   => 4,
121
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
122
                ]),
123
                file_get_contents(__DIR__ . '/../../../fixtures/csv_file.csv'),
124
                [
125
                    ['2', 'your , nice', 'fish"', '","'],
126
                ],
127
            ],
128
            [
129
                new CsvFormat([
130
                    CsvFormat::OPTION_HEADER_ROW   => 1,
131
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
132
                ]),
133
                "\n" . '"1","2","3","4","5"',
134
                [
135
                    ['1', '2', '3', '4', '5'],
136
                ],
137
            ],
138
        ];
139
    }
140
141
    /**
142
     * @dataProvider parseFailureData
143
     *
144
     * @param CsvFormatInterface $format
145
     * @param string             $line
146
     * @param string             $expected
147
     */
148 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...
149
    {
150
        $parser = new CsvParser($format);
151
152
        $iterator = $parser->parse($this->createStream($line));
153
154
        static::expectException($expected);
155
        $actual = iterator_to_array($iterator);
156
        $actual = array_values(array_map('iterator_to_array', $actual));
157
158
        static::assertEquals($expected, $actual);
159
    }
160
161
    /**
162
     * @return array
163
     */
164
    public function parseFailureData()
165
    {
166
        return [
167
            [
168
                new CsvFormat([
169
                    CsvFormat::OPTION_HEADER_ROW   => 1,
170
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
171
                ]),
172
                '"id","fish","cake"' . "\n" . '"1","name","thing","too many"',
173
                RuntimeException::class,
174
            ],
175
        ];
176
    }
177
}
178