Completed
Push — master ( e8fccd...9c2126 )
by Harry
02:44
created

ParserTest::parseData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 56
rs 9.7251
cc 1
eloc 26
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_DELIMITER    => '|',
53
                    CsvConfiguration::OPTION_QUOTE        => "'",
54
                    CsvConfiguration::OPTION_ESCAPE       => '\\',
55
                    CsvConfiguration::OPTION_DOUBLE_QUOTE => true,
56
                    CsvConfiguration::OPTION_NEW_LINE     => '---',
57
                    CsvConfiguration::OPTION_NULL         => '\\N',
58
                ]),
59
                "'some'|text|'\\'here'|\\N|'with''quotes'---'another'|'line'",
60
                [],
61
                [
62
                    ['some', 'text', "'here", null, "with'quotes"],
63
                    ['another', 'line'],
64
                ],
65
            ],
66
            [
67
                new CsvConfiguration([
68
                    CsvConfiguration::OPTION_NULL => 'null',
69
                ]),
70
                '"text",1.2,false,true,12,2.3e-34,-2341,null,pants',
71
                [
72
                    new BoolValueParser(),
73
                    new NumberValueParser(),
74
                ],
75
                [
76
                    ['text', 1.2, false, true, 12, 2.3e-34, -2341, null, 'pants'],
77
                ],
78
            ],
79
            [
80
                new CsvConfiguration(),
81
                '',
82
                [],
83
                [[]],
84
            ],
85
            [
86
                new CsvConfiguration(),
87
                'text\\Nthing,\\Nstart,end\\N,\\N,"\\N"',
88
                [],
89
                [
90
                    ['text\\Nthing', '\\Nstart', 'end\\N', null, '\\N'],
91
                ],
92
            ],
93
        ];
94
    }
95
96 View Code Duplication
    public function testParseExceptionOnEscapeAsLastBitOfString()
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...
97
    {
98
        $csv = '"string",\\';
99
        $tokeniser = new StringTokeniser(new CsvConfiguration(), $csv);
100
        $parser = new Parser();
101
102
        static::expectException(RuntimeException::class);
103
104
        iterator_to_array($parser->parse($tokeniser->getTokens()));
105
    }
106
107 View Code Duplication
    public function testParserExceptionOnStringOutsideOfQuotes()
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...
108
    {
109
        $csv = '"string"stuff,things';
110
        $tokeniser = new StringTokeniser(new CsvConfiguration(), $csv);
111
        $parser = new Parser();
112
113
        static::expectException(RuntimeException::class);
114
115
        iterator_to_array($parser->parse($tokeniser->getTokens()));
116
    }
117
}
118