Completed
Push — master ( d97897...85851c )
by Harry
03:38
created

CsvFormatterTest::testEncoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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