Completed
Pull Request — master (#3)
by Harry
05:31
created

CsvParserTest::parseLineData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 56
rs 9.7251
cc 1
eloc 35
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/data-file
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/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Unit\Format\Parser;
15
16
use Graze\DataFile\Format\CsvFormat;
17
use Graze\DataFile\Format\CsvFormatInterface;
18
use Graze\DataFile\Format\Parser\CsvParser;
19
use Graze\DataFile\Test\TestCase;
20
use GuzzleHttp\Psr7\Stream;
21
use Mockery as m;
22
use Psr\Http\Message\StreamInterface;
23
24
class CsvParserTest extends TestCase
25
{
26
    /**
27
     * @dataProvider parseLineData
28
     *
29
     * @param CsvFormatInterface $format
30
     * @param string             $line
31
     * @param array              $expected
32
     */
33
    public function testParse(CsvFormatInterface $format, $line, array $expected)
34
    {
35
        $parser = new CsvParser($format);
36
37
        $iterator = $parser->parse($this->createStream($line));
38
        $actual = iterator_to_array($iterator);
39
        $actual = array_values(array_map('iterator_to_array', $actual));
40
41
        static::assertEquals($expected, $actual);
42
    }
43
44
    /**
45
     * @param string $string
46
     *
47
     * @return StreamInterface
48
     */
49 View Code Duplication
    protected function createStream($string)
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...
50
    {
51
        $stream = fopen('php://memory', 'r+');
52
        fwrite($stream, $string);
53
        rewind($stream);
54
        return new Stream($stream);
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function parseLineData()
61
    {
62
        return [
63
            [
64
                new CsvFormat([CsvFormat::OPTION_HEADERS => 0]),
65
                '"a","b",c,1,true,0.2,false,", enclosed","\\n \\r stuff",\\N',
66
                [['a', 'b', 'c', '1', 'true', '0.2', 'false', ', enclosed', "n r stuff", null]],
67
            ],
68
            [
69
                new CsvFormat([
70
                    CsvFormat::OPTION_DELIMITER       => '|',
71
                    CsvFormat::OPTION_ESCAPE          => '~',
72
                    CsvFormat::OPTION_QUOTE_CHARACTER => '`',
73
                    CsvFormat::OPTION_NULL_OUTPUT     => 'null',
74
                    CsvFormat::OPTION_HEADERS         => 0,
75
                    CsvFormat::OPTION_DOUBLE_QUOTE    => true,
76
                ]),
77
                '`string`|`other,thing`|some stuff|escaped ~\\n|``` all the `` quotes `|null',
78
                [['string', 'other,thing', 'some stuff', 'escaped \n', '` all the ` quotes ', null]],
79
            ],
80
            [
81
                new CsvFormat([
82
                    CsvFormat::OPTION_QUOTE_CHARACTER => "\\",
83
                    CsvFormat::OPTION_DELIMITER       => '|',
84
                    CsvFormat::OPTION_ESCAPE          => "\\",
85
                    CsvFormat::OPTION_HEADERS         => 0,
86
                ]),
87
                'a|b|c|d\\|e',
88
                [['a', 'b', 'c', 'd|e']],
89
            ],
90
            [
91
                new CsvFormat([
92
                    CsvFormat::OPTION_HEADERS      => 1,
93
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
94
                ]),
95
                file_get_contents(__DIR__ . '/../../../fixtures/csv_file.csv'),
96
                [
97
                    ['0', 'my name', 'i like " quotes', 'question?'],
98
                    ['1', 'your name', 'potatoes! ' . "\n" . 'and stuff', 'think'],
99
                    ['2', 'your , nice', 'fish"', '","'],
100
                ],
101
            ],
102
            [
103
                new CsvFormat([
104
                    CsvFormat::OPTION_HEADERS      => 0,
105
                    CsvFormat::OPTION_LIMIT        => 2,
106
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
107
                ]),
108
                file_get_contents(__DIR__ . '/../../../fixtures/csv_file.csv'),
109
                [
110
                    ['id', 'name', 'things', 'stuff'],
111
                    ['0', 'my name', 'i like " quotes', 'question?'],
112
                ],
113
            ],
114
        ];
115
    }
116
}
117