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

CsvFormatterTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 125
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testEncoding() 0 5 1
A getFormattingData() 0 68 1
A handleObjectWithNoToStringMethod() 0 10 1
A testInvoke() 0 6 1
A testRowSeparator() 0 10 1
A testStartAndEndBlocksAreEmpty() 0 7 1
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
    }
111
112
    public function handleObjectWithNoToStringMethod()
113
    {
114
        $object = m::mock();
115
116
        $formatter = new CsvFormatter(new CsvFormat());
117
118
        static::expectException(InvalidArgumentException::class);
119
120
        $formatter->format([$object]);
121
    }
122
123
    public function testInvoke()
124
    {
125
        $formatter = new CsvFormatter(new CsvFormat());
126
127
        static::assertEquals('"test","1","2","4.3"', $formatter(['test', 1, 2, 4.3]));
128
    }
129
130
    public function testRowSeparator()
131
    {
132
        $formatter = new CsvFormatter(new CsvFormat());
133
134
        static::assertEquals("\n", $formatter->getRowSeparator());
135
136
        $formatter = new CsvFormatter(new CsvFormat([CsvFormat::OPTION_LINE_TERMINATOR => '---']));
137
138
        static::assertEquals('---', $formatter->getRowSeparator());
139
    }
140
141
    public function testStartAndEndBlocksAreEmpty()
142
    {
143
        $formatter = new CsvFormatter(new CsvFormat());
144
145
        static::assertEmpty($formatter->getInitialBlock());
146
        static::assertEmpty($formatter->getClosingBlock());
147
    }
148
}
149