Completed
Pull Request — master (#3)
by Harry
03:17
created

CsvFormatterTest::getFormattingData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 86
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 86
rs 8.6583
cc 1
eloc 59
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\Formatter;
15
16
use DateTime;
17
use Graze\DataFile\Format\CsvFormat;
18
use Graze\DataFile\Format\CsvFormatInterface;
19
use Graze\DataFile\Format\Formatter\CsvFormatter;
20
use Graze\DataFile\Test\TestCase;
21
use InvalidArgumentException;
22
use Mockery as m;
23
24
class CsvFormatterTest extends TestCase
25
{
26
    /**
27
     * @dataProvider getFormattingData
28
     *
29
     * @param CsvFormatInterface $format
30
     * @param array              $row
31
     * @param string             $expected
32
     * @param string             $message
33
     */
34
    public function testEncoding(CsvFormatInterface $format, array $row, $expected, $message = '')
35
    {
36
        $formatter = new CsvFormatter($format);
37
        static::assertEquals($expected, $formatter->format($row), $message);
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function getFormattingData()
44
    {
45
        $format = new CsvFormat();
46
47
        $date = new DateTime();
48
49
        $object = m::mock();
50
        $object->shouldReceive('__toString')
51
               ->andReturn('object');
52
53
        return [
54
            [
55
                $format,
56
                [
57
                    'text',
58
                    1,
59
                    2.5,
60
                    null,
61
                    true,
62
                    "string wit '",
63
                    'string with "',
64
                    'string with \\',
65
                    'string with ,',
66
                    "multi line \n string",
67
                    $object,
68
                ],
69
                '"text","1","2.5",\\N,"1","string wit \'","string with \"","string with \\\\","string with \\,","multi line \\' . "\n" . ' string","object"',
70
                'basic formatting failed',
71
            ],
72
            [
73
                $format,
74
                [$date],
75
                sprintf('"%s"', $date->format('Y-m-d H:i:s')),
76
                'date formatting failed',
77
            ],
78
            [
79
                new CsvFormat([
80
                    CsvFormat::OPTION_DELIMITER       => '|',
81
                    CsvFormat::OPTION_NULL_OUTPUT     => "NULL",
82
                    CsvFormat::OPTION_QUOTE_CHARACTER => "'",
83
                    CsvFormat::OPTION_LINE_TERMINATOR => '---',
84
                    CsvFormat::OPTION_ESCAPE          => '"',
85
                ]),
86
                ['text', 1, 2.5, false, null, '|-', "'", '"'],
87
                "'text'|'1'|'2.5'|'0'|NULL|'\"|-'|'\"''|'\"\"'",
88
                'different options failed',
89
            ],
90
            [
91
                new CsvFormat([
92
                    CsvFormat::OPTION_DELIMITER       => "\t",
93
                    CsvFormat::OPTION_NULL_OUTPUT     => 'null',
94
                    CsvFormat::OPTION_QUOTE_CHARACTER => '',
95
                    CsvFormat::OPTION_LINE_TERMINATOR => "\n",
96
                ]),
97
                ['text', 1, 2.5, false, null, "\t a", ",", "\n"],
98
                "text\t1\t2.5\t0\tnull\t" . '\\' . "\t a\t,\t" . '\\' . "\n",
99
                'tab separated options failed',
100
            ],
101
            [
102
                new CsvFormat([
103
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
104
                ]),
105
                ['text"', 1, 2.5, false, null, "\n \r a", "\t", '""foo""'],
106
                '"text""","1","2.5","0",\\N,"\\' . "\n" . ' \\' . "\r" . ' a","\\' . "\t" . '","""""foo"""""',
107
                'double quotes failed',
108
            ],
109
            [
110
                new CsvFormat([
111
                    CsvFormat::OPTION_ESCAPE       => '',
112
                    CsvFormat::OPTION_DOUBLE_QUOTE => true,
113
                ]),
114
                ['text', '"test', ','],
115
                '"text","""test",","',
116
                'no escape failed',
117
            ],
118
            [
119
                new CsvFormat([
120
                    CsvFormat::OPTION_QUOTE_CHARACTER => '',
121
                    CsvFormat::OPTION_DOUBLE_QUOTE    => true,
122
                ]),
123
                ['text', 'things,', '"here"'],
124
                'text,things\,,"here"',
125
                'blank quote character failed and double quote should do nothing',
126
            ],
127
        ];
128
    }
129
130
    public function handleObjectWithNoToStringMethod()
131
    {
132
        $object = m::mock();
133
134
        $formatter = new CsvFormatter(new CsvFormat());
135
136
        static::expectException(InvalidArgumentException::class);
137
138
        $formatter->format([$object]);
139
    }
140
141
    public function testInvoke()
142
    {
143
        $formatter = new CsvFormatter(new CsvFormat());
144
145
        static::assertEquals('"test","1","2","4.3"', $formatter(['test', 1, 2, 4.3]));
146
    }
147
148
    public function testRowSeparator()
149
    {
150
        $formatter = new CsvFormatter(new CsvFormat());
151
152
        static::assertEquals("\n", $formatter->getRowSeparator());
153
154
        $formatter = new CsvFormatter(new CsvFormat([CsvFormat::OPTION_LINE_TERMINATOR => '---']));
155
156
        static::assertEquals('---', $formatter->getRowSeparator());
157
    }
158
159
    public function testStartAndEndBlocksAreEmpty()
160
    {
161
        $formatter = new CsvFormatter(new CsvFormat());
162
163
        static::assertEmpty($formatter->getInitialBlock());
164
        static::assertEmpty($formatter->getClosingBlock());
165
    }
166
}
167