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 => "NULL", |
83
|
|
|
CsvFormat::OPTION_QUOTE => "'", |
84
|
|
|
CsvFormat::OPTION_NEW_LINE => '---', |
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 => 'null', |
95
|
|
|
CsvFormat::OPTION_QUOTE => '', |
96
|
|
|
CsvFormat::OPTION_NEW_LINE => "\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 => '', |
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
|
|
|
new CsvFormat([ |
130
|
|
|
CsvFormat::OPTION_HEADER_ROW => 2, |
131
|
|
|
]), |
132
|
|
|
['name' => 'text', 'stuff' => 'things', 'cake' => 'pants'], |
133
|
|
|
'"name","stuff","cake"' . "\n" . |
134
|
|
|
'"text","things","pants"', |
135
|
|
|
'header row should print the array keys', |
136
|
|
|
], |
137
|
|
|
[ |
138
|
|
|
new CsvFormat([ |
139
|
|
|
CsvFormat::OPTION_HEADER_ROW => 3, |
140
|
|
|
CsvFormat::OPTION_DATA_START => 6, |
141
|
|
|
]), |
142
|
|
|
['name' => 'text', 'stuff' => 'things', 'cake' => 'pants'], |
143
|
|
|
'"name","stuff","cake"' . "\n\n\n" . |
144
|
|
|
'"text","things","pants"', |
145
|
|
|
'having a sparse start should add spaces before and after the header row', |
146
|
|
|
], |
147
|
|
|
[ |
148
|
|
|
new CsvFormat([ |
149
|
|
|
CsvFormat::OPTION_HEADER_ROW => 1, |
150
|
|
|
]), |
151
|
|
|
['text', 'things,', '"here"'], |
152
|
|
|
'"0","1","2"' . "\n" . '"text","things\,","\"here\""', |
153
|
|
|
'should out put numbers when no keys are provided', |
154
|
|
|
], |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function handleObjectWithNoToStringMethod() |
159
|
|
|
{ |
160
|
|
|
$object = m::mock(); |
161
|
|
|
|
162
|
|
|
$formatter = new CsvFormatter(new CsvFormat()); |
163
|
|
|
|
164
|
|
|
static::expectException(InvalidArgumentException::class); |
165
|
|
|
|
166
|
|
|
$formatter->format([$object]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testInvoke() |
170
|
|
|
{ |
171
|
|
|
$formatter = new CsvFormatter(new CsvFormat()); |
172
|
|
|
|
173
|
|
|
static::assertEquals('"test","1","2","4.3"', $formatter(['test', 1, 2, 4.3])); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testRowSeparator() |
177
|
|
|
{ |
178
|
|
|
$formatter = new CsvFormatter(new CsvFormat()); |
179
|
|
|
|
180
|
|
|
static::assertEquals("\n", $formatter->getRowSeparator()); |
181
|
|
|
|
182
|
|
|
$formatter = new CsvFormatter(new CsvFormat([CsvFormat::OPTION_NEW_LINE => '---'])); |
183
|
|
|
|
184
|
|
|
static::assertEquals('---', $formatter->getRowSeparator()); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testStartAndEndBlocksAreEmpty() |
188
|
|
|
{ |
189
|
|
|
$formatter = new CsvFormatter(new CsvFormat()); |
190
|
|
|
|
191
|
|
|
static::assertEmpty($formatter->getInitialBlock()); |
192
|
|
|
static::assertEmpty($formatter->getClosingBlock()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testStartBlockContainsABom() |
196
|
|
|
{ |
197
|
|
|
$formatter = new CsvFormatter(new CsvFormat([ |
198
|
|
|
CsvFormat::OPTION_BOM => Bom::BOM_UTF16_BE, |
199
|
|
|
])); |
200
|
|
|
|
201
|
|
|
static::assertEquals(Bom::BOM_UTF16_BE, $formatter->getInitialBlock()); |
202
|
|
|
static::assertEmpty($formatter->getClosingBlock()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testStartBlockContainsSpacingWhenUsingHeaders() |
206
|
|
|
{ |
207
|
|
|
$formatter = new CsvFormatter(new CsvFormat([ |
208
|
|
|
CsvFormat::OPTION_HEADER_ROW => 3, |
209
|
|
|
])); |
210
|
|
|
|
211
|
|
|
static::assertEquals("\n\n", $formatter->getInitialBlock()); |
212
|
|
|
static::assertEmpty($formatter->getClosingBlock()); |
213
|
|
|
|
214
|
|
|
$formatter = new CsvFormatter(new CsvFormat([ |
215
|
|
|
CsvFormat::OPTION_DATA_START => 4, |
216
|
|
|
CsvFormat::OPTION_BOM => Bom::BOM_UTF8, |
217
|
|
|
])); |
218
|
|
|
|
219
|
|
|
static::assertEquals(Bom::BOM_UTF8 . "\n\n\n", $formatter->getInitialBlock()); |
220
|
|
|
static::assertEmpty($formatter->getClosingBlock()); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|