Completed
Push — master ( d12968...2a6550 )
by Harry
02:36
created

ParserTest::testParseExceptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
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
                new CsvConfiguration(),
95
                "한국말\n조선말,한국말",
96
                [],
97
                [
98
                    ['한국말'],
99
                    ['조선말', '한국말'],
100
                ],
101
            ],
102
        ];
103
    }
104
105
    /**
106
     * @dataProvider parseExceptionsData
107
     *
108
     * @param string $csv
109
     * @param string $exception
110
     */
111
    public function testParseExceptions($csv, $exception)
112
    {
113
        $tokeniser = new StringTokeniser(new CsvConfiguration(), $csv);
114
        $parser = new Parser();
115
116
        static::expectException($exception);
117
118
        iterator_to_array($parser->parse($tokeniser->getTokens()));
119
    }
120
121
    /**
122
     * @return array
123
     */
124 View Code Duplication
    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...
125
    {
126
        return [
127
            ['"string",\\', RuntimeException::class],
128
            ['"string"stuff,things', RuntimeException::class],
129
        ];
130
    }
131
}
132