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

ParserTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 104
Duplicated Lines 19.23 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
c 2
b 1
f 0
lcom 0
cbo 6
dl 20
loc 104
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testParse() 0 11 1
A parseData() 0 56 1
A testParseExceptionOnEscapeAsLastBitOfString() 10 10 1
A testParserExceptionOnStringOutsideOfQuotes() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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