ParserTest::parseData()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 114
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 1 Features 1
Metric Value
c 9
b 1
f 1
dl 0
loc 114
rs 8.2857
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/csv-token
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/csv-token/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/csv-token
12
 */
13
14
namespace Graze\CsvToken\Test\Integration;
15
16
use Graze\CsvToken\Csv\CsvConfiguration;
17
use Graze\CsvToken\Csv\CsvConfigurationInterface;
18
use Graze\CsvToken\Parser;
19
use Graze\CsvToken\Test\TestCase;
20
use Graze\CsvToken\Tokeniser\StringTokeniser;
21
use Graze\CsvToken\ValueParser\BoolValueParser;
22
use Graze\CsvToken\ValueParser\NumberValueParser;
23
use RuntimeException;
24
25
class ParserTest extends TestCase
26
{
27
    /**
28
     * @dataProvider parseData
29
     *
30
     * @param CsvConfigurationInterface $config
31
     * @param string                    $csv
32
     * @param array                     $valueParsers
33
     * @param array                     $expected
34
     */
35
    public function testParse(CsvConfigurationInterface $config, $csv, array $valueParsers, array $expected)
36
    {
37
        $tokeniser = new StringTokeniser($config, $csv);
38
        $parser = new Parser($valueParsers);
39
40
        $results = iterator_to_array($parser->parse($tokeniser->getTokens()));
41
42
        static::assertEquals($expected, $results);
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function parseData()
49
    {
50
        return [
51
            [
52
                new CsvConfiguration(),
53
                '"some",\\N,"new' . "\n" . 'line",with\\' . "\n" . 'escaped,"in\\' . "\n" . 'quotes","\\\\"',
54
                [],
55
                [
56
                    ['some', null, "new\nline", "with\nescaped", "in\nquotes", '\\'],
57
                ],
58
            ],
59
            [
60
                new CsvConfiguration([
61
                    CsvConfiguration::OPTION_DOUBLE_QUOTE => true,
62
                ]),
63
                '"end""","""start","""both""","",""""',
64
                [],
65
                [['end"', '"start', '"both"', '', '"']],
66
            ],
67
            [
68
                new CsvConfiguration([
69
                    CsvConfiguration::OPTION_DELIMITER    => '|',
70
                    CsvConfiguration::OPTION_QUOTE        => "'",
71
                    CsvConfiguration::OPTION_ESCAPE       => '\\',
72
                    CsvConfiguration::OPTION_DOUBLE_QUOTE => true,
73
                    CsvConfiguration::OPTION_NEW_LINES    => ['---'],
74
                    CsvConfiguration::OPTION_NULL         => '\\N',
75
                ]),
76
                "'some'|text|'\\'here'|\\N|'with''quotes'---'another'|'line'",
77
                [],
78
                [
79
                    ['some', 'text', "'here", null, "with'quotes"],
80
                    ['another', 'line'],
81
                ],
82
            ],
83
            [
84
                new CsvConfiguration([
85
                    CsvConfiguration::OPTION_NULL => 'null',
86
                ]),
87
                '"text",1.2,false,true,12,2.3e-34,-2341,null,pants',
88
                [
89
                    new BoolValueParser(),
90
                    new NumberValueParser(),
91
                ],
92
                [
93
                    ['text', 1.2, false, true, 12, 2.3e-34, -2341, null, 'pants'],
94
                ],
95
            ],
96
            [
97
                new CsvConfiguration(),
98
                '',
99
                [],
100
                [],
101
            ],
102
            [
103
                new CsvConfiguration(),
104
                'text\\Nthing,\\Nstart,end\\N,\\N,"\\N"',
105
                [],
106
                [
107
                    ['text\\Nthing', '\\Nstart', 'end\\N', null, 'N'],
108
                ],
109
            ],
110
            [
111
                new CsvConfiguration(),
112
                "한국말\n조선말,한국말",
113
                [],
114
                [
115
                    ['한국말'],
116
                    ['조선말', '한국말'],
117
                ],
118
            ],
119
            [
120
                new CsvConfiguration(),
121
                '"1","2","3"' . "\n",
122
                [],
123
                [
124
                    ['1', '2', '3'],
125
                ],
126
            ],
127
            [ // no quote and double quote should do nothing
128
                new CsvConfiguration([
129
                    CsvConfiguration::OPTION_QUOTE        => '',
130
                    CsvConfiguration::OPTION_DOUBLE_QUOTE => true,
131
                ]),
132
                'text,things"here,and\,here',
133
                [],
134
                [
135
                    ['text', 'things"here', 'and,here'],
136
                ],
137
            ],
138
            [
139
                new CsvConfiguration([
140
                    CsvConfiguration::OPTION_ESCAPE => '',
141
                ]),
142
                '"text","here","and\,here"',
143
                [],
144
                [
145
                    ['text', 'here', 'and\,here'],
146
                ],
147
            ],
148
            [
149
                new CsvConfiguration([
150
                    CsvConfiguration::OPTION_DELIMITER    => '|',
151
                    CsvConfiguration::OPTION_ESCAPE       => '~',
152
                    CsvConfiguration::OPTION_QUOTE        => '`',
153
                    CsvConfiguration::OPTION_NULL         => 'null',
154
                    CsvConfiguration::OPTION_DOUBLE_QUOTE => true,
155
                ]),
156
                '`string`|`other,thing`|some stuff|escaped ~\\n|``` all the `` quotes `|null',
157
                [],
158
                [['string', 'other,thing', 'some stuff', 'escaped \n', '` all the ` quotes ', null]],
159
            ],
160
        ];
161
    }
162
163
    /**
164
     * @dataProvider parseExceptionsData
165
     *
166
     * @param string $csv
167
     * @param string $exception
168
     */
169
    public function testParseExceptions($csv, $exception)
170
    {
171
        $tokeniser = new StringTokeniser(new CsvConfiguration(), $csv);
172
        $parser = new Parser();
173
174
        static::expectException($exception);
175
176
        iterator_to_array($parser->parse($tokeniser->getTokens()));
177
    }
178
179
    /**
180
     * @return array
181
     */
182
    public function parseExceptionsData()
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...
183
    {
184
        return [
185
            ['"string"stuff,things', RuntimeException::class], // extra text after a closing quote
186
            ['"string', RuntimeException::class], // no closing quote
187
        ];
188
    }
189
}
190