Completed
Push — master ( 033934...d222fa )
by Harry
11s
created

ParserTest::parseData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 81
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 0
loc 81
rs 8.8076
cc 1
eloc 37
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
namespace Graze\CsvToken\Test\Integration;
4
5
use Graze\CsvToken\Csv\CsvConfiguration;
6
use Graze\CsvToken\Csv\CsvConfigurationInterface;
7
use Graze\CsvToken\Parser;
8
use Graze\CsvToken\Test\TestCase;
9
use Graze\CsvToken\Tokeniser\StringTokeniser;
10
use Graze\CsvToken\ValueParser\BoolValueParser;
11
use Graze\CsvToken\ValueParser\NumberValueParser;
12
use RuntimeException;
13
14
class ParserTest extends TestCase
15
{
16
    /**
17
     * @dataProvider parseData
18
     *
19
     * @param CsvConfigurationInterface $config
20
     * @param string                    $csv
21
     * @param array                     $valueParsers
22
     * @param array                     $expected
23
     */
24
    public function testParse(CsvConfigurationInterface $config, $csv, array $valueParsers, array $expected)
25
    {
26
        $tokeniser = new StringTokeniser($config, $csv);
27
        $parser = new Parser($valueParsers);
28
29
        $results = iterator_to_array($parser->parse($tokeniser->getTokens()));
30
31
        $results = array_map('iterator_to_array', $results);
32
33
        static::assertEquals($expected, $results);
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function parseData()
40
    {
41
        return [
42
            [
43
                new CsvConfiguration(),
44
                '"some",\\N,"new' . "\n" . 'line",with\\' . "\n" . 'escaped,"in\\' . "\n" . 'quotes","\\\\"',
45
                [],
46
                [
47
                    ['some', null, "new\nline", "with\nescaped", "in\nquotes", '\\'],
48
                ],
49
            ],
50
            [
51
                new CsvConfiguration([
52
                    CsvConfiguration::OPTION_DOUBLE_QUOTE => true,
53
                ]),
54
                '"end""","""start","""both""","",""""',
55
                [],
56
                [['end"', '"start', '"both"', '', '"']],
57
            ],
58
            [
59
                new CsvConfiguration([
60
                    CsvConfiguration::OPTION_DELIMITER    => '|',
61
                    CsvConfiguration::OPTION_QUOTE        => "'",
62
                    CsvConfiguration::OPTION_ESCAPE       => '\\',
63
                    CsvConfiguration::OPTION_DOUBLE_QUOTE => true,
64
                    CsvConfiguration::OPTION_NEW_LINE     => '---',
65
                    CsvConfiguration::OPTION_NULL         => '\\N',
66
                ]),
67
                "'some'|text|'\\'here'|\\N|'with''quotes'---'another'|'line'",
68
                [],
69
                [
70
                    ['some', 'text', "'here", null, "with'quotes"],
71
                    ['another', 'line'],
72
                ],
73
            ],
74
            [
75
                new CsvConfiguration([
76
                    CsvConfiguration::OPTION_NULL => 'null',
77
                ]),
78
                '"text",1.2,false,true,12,2.3e-34,-2341,null,pants',
79
                [
80
                    new BoolValueParser(),
81
                    new NumberValueParser(),
82
                ],
83
                [
84
                    ['text', 1.2, false, true, 12, 2.3e-34, -2341, null, 'pants'],
85
                ],
86
            ],
87
            [
88
                new CsvConfiguration(),
89
                '',
90
                [],
91
                [],
92
            ],
93
            [
94
                new CsvConfiguration(),
95
                'text\\Nthing,\\Nstart,end\\N,\\N,"\\N"',
96
                [],
97
                [
98
                    ['text\\Nthing', '\\Nstart', 'end\\N', null, 'N'],
99
                ],
100
            ],
101
            [
102
                new CsvConfiguration(),
103
                "한국말\n조선말,한국말",
104
                [],
105
                [
106
                    ['한국말'],
107
                    ['조선말', '한국말'],
108
                ],
109
            ],
110
            [
111
                new CsvConfiguration(),
112
                '"1","2","3"' . "\n",
113
                [],
114
                [
115
                    ['1', '2', '3'],
116
                ],
117
            ],
118
        ];
119
    }
120
121
    /**
122
     * @dataProvider parseExceptionsData
123
     *
124
     * @param string $csv
125
     * @param string $exception
126
     */
127
    public function testParseExceptions($csv, $exception)
128
    {
129
        $tokeniser = new StringTokeniser(new CsvConfiguration(), $csv);
130
        $parser = new Parser();
131
132
        static::expectException($exception);
133
134
        iterator_to_array($parser->parse($tokeniser->getTokens()));
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    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...
141
    {
142
        return [
143
            ['"string"stuff,things', RuntimeException::class], // extra text after a closing quote
144
            ['"string', RuntimeException::class], // no closing quote
145
        ];
146
    }
147
}
148