|
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; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\CsvToken\Csv\Bom; |
|
17
|
|
|
use Graze\DataFile\Format\CsvFormat; |
|
18
|
|
|
use Graze\DataFile\Test\TestCase; |
|
19
|
|
|
use InvalidArgumentException; |
|
20
|
|
|
|
|
21
|
|
|
class CsvFormatTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
public function testImplementsInterface() |
|
24
|
|
|
{ |
|
25
|
|
|
$definition = new CsvFormat(); |
|
26
|
|
|
|
|
27
|
|
|
static::assertInstanceOf('Graze\DataFile\Format\CsvFormatInterface', $definition); |
|
28
|
|
|
|
|
29
|
|
|
static::assertEquals('csv', $definition->getType()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testDefaultsAreAssignedWhenNoOptionsSupplied() |
|
33
|
|
|
{ |
|
34
|
|
|
$definition = new CsvFormat(); |
|
35
|
|
|
|
|
36
|
|
|
static::assertEquals(',', $definition->getDelimiter(), "Default Delimiter should be ','"); |
|
37
|
|
|
static::assertEquals('"', $definition->getQuote(), "Default quote character should be \""); |
|
38
|
|
|
static::assertTrue($definition->hasQuote(), "Quoting should be on by default"); |
|
39
|
|
|
static::assertEquals('\\N', $definition->getNullValue(), "Null character should be '\\N'"); |
|
40
|
|
|
static::assertFalse($definition->hasHeaderRow(), "Headers should be on by default"); |
|
41
|
|
|
static::assertEquals(-1, $definition->getHeaderRow(), "Header row should be -1 by default"); |
|
42
|
|
|
static::assertEquals(1, $definition->getDataStart(), "Get data start should be 1 by default"); |
|
43
|
|
|
static::assertEquals( |
|
44
|
|
|
["\n", "\r", "\r\n"], |
|
45
|
|
|
$definition->getNewLines(), |
|
46
|
|
|
"Line terminator should be ['\\n','\\r','\\r\\n']" |
|
47
|
|
|
); |
|
48
|
|
|
static::assertEquals("\n", $definition->getNewLine(), "Line character should be '\\n'"); |
|
49
|
|
|
static::assertEquals('\\', $definition->getEscape(), "Default escape character should be '\\'"); |
|
50
|
|
|
static::assertTrue($definition->hasEscape()); |
|
51
|
|
|
static::assertEquals(-1, $definition->getLimit(), "Default limit should be -1"); |
|
52
|
|
|
static::assertEquals(false, $definition->useDoubleQuotes(), "Double quote should be off by default"); |
|
53
|
|
|
static::assertNull($definition->getBom(), "Bom should be null by default"); |
|
54
|
|
|
static::assertEquals( |
|
55
|
|
|
[Bom::BOM_UTF8, Bom::BOM_UTF16_BE, Bom::BOM_UTF16_LE, Bom::BOM_UTF32_BE, Bom::BOM_UTF32_LE], |
|
56
|
|
|
$definition->getBoms() |
|
57
|
|
|
); |
|
58
|
|
|
static::assertEquals('UTF-8', $definition->getEncoding(), 'Encoding should be set to UTF-8 by default'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testAssigningOptionsModifiesTheDefinition() |
|
62
|
|
|
{ |
|
63
|
|
|
$definition = new CsvFormat([ |
|
64
|
|
|
'delimiter' => "\t", |
|
65
|
|
|
'quote' => '', |
|
66
|
|
|
'null' => '', |
|
67
|
|
|
'headerRow' => 1, |
|
68
|
|
|
'dataStart' => 5, |
|
69
|
|
|
'newLine' => "----", |
|
70
|
|
|
'escape' => '', |
|
71
|
|
|
'limit' => 2, |
|
72
|
|
|
'doubleQuote' => true, |
|
73
|
|
|
'bom' => Bom::BOM_UTF16_BE, |
|
74
|
|
|
'encoding' => 'UTF-16BE', |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
|
|
static::assertEquals("\t", $definition->getDelimiter(), "Delimiter should be set to '\\t' (tab)"); |
|
78
|
|
|
static::assertEquals('', $definition->getQuote(), "Quote character should be blank"); |
|
79
|
|
|
static::assertFalse($definition->hasQuote(), "Quoting should be off"); |
|
80
|
|
|
static::assertEquals('', $definition->getNullValue(), "Null character should be '' (blank)'"); |
|
81
|
|
|
static::assertTrue($definition->hasHeaderRow(), "Headers should be on"); |
|
82
|
|
|
static::assertEquals(1, $definition->getHeaderRow(), "Header row should be set to 1"); |
|
83
|
|
|
static::assertEquals(5, $definition->getDataStart(), "Data Start should be set to 5"); |
|
84
|
|
|
static::assertEquals("----", $definition->getNewLine(), "Line terminator should be '----'"); |
|
85
|
|
|
static::assertEquals(["----"], $definition->getNewLines(), "Line terminators should be ['----']"); |
|
86
|
|
|
static::assertEquals('', $definition->getEscape(), "Escape Character should be '' (blank)"); |
|
87
|
|
|
static::assertFalse($definition->hasEscape(), "Format should not be marked as not having escape"); |
|
88
|
|
|
static::assertEquals(2, $definition->getLimit(), 'Limit should be 2'); |
|
89
|
|
|
static::assertEquals(true, $definition->useDoubleQuotes(), 'double quote should be on'); |
|
90
|
|
|
static::assertEquals(Bom::BOM_UTF16_BE, $definition->getBom(), 'bom should be set to UTF-16BE'); |
|
91
|
|
|
static::assertEquals([Bom::BOM_UTF16_BE], $definition->getBoms(), 'boms should be set to [UTF-16BE]'); |
|
92
|
|
|
static::assertEquals('UTF-16BE', $definition->getEncoding(), 'Encoding should be set to UTF-16BE'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testSettingProperties() |
|
96
|
|
|
{ |
|
97
|
|
|
$definition = new CsvFormat(); |
|
98
|
|
|
|
|
99
|
|
|
static::assertSame($definition, $definition->setDelimiter("\t"), "SetDelimiter should be fluent"); |
|
100
|
|
|
static::assertEquals("\t", $definition->getDelimiter(), "Delimiter should be set to '\\t' (tab)"); |
|
101
|
|
|
|
|
102
|
|
|
static::assertSame($definition, $definition->setQuote(''), "setQuoteCharacter should be fluent"); |
|
103
|
|
|
static::assertEquals('', $definition->getQuote(), "Quote character should be blank"); |
|
104
|
|
|
static::assertFalse($definition->hasQuote(), "Quoting should be off"); |
|
105
|
|
|
|
|
106
|
|
|
static::assertSame($definition, $definition->setNullValue(''), "setNullOutput should be fluent"); |
|
107
|
|
|
static::assertEquals('', $definition->getNullValue(), "Null character should be '' (blank)'"); |
|
108
|
|
|
|
|
109
|
|
|
static::assertSame($definition, $definition->setHeaderRow(1), "setHeaders should be fluent"); |
|
110
|
|
|
static::assertTrue($definition->hasHeaderRow(), "Headers should be on"); |
|
111
|
|
|
static::assertEquals(1, $definition->getHeaderRow(), "Headers should be set to 1"); |
|
112
|
|
|
|
|
113
|
|
|
static::assertSame($definition, $definition->setDataStart(2), "setDataStart should be fluent"); |
|
114
|
|
|
static::assertEquals(2, $definition->getDataStart(), "Data Start should be 2"); |
|
115
|
|
|
|
|
116
|
|
|
static::assertSame($definition, $definition->setNewLine('----'), "setLineTerminator should be fluent"); |
|
117
|
|
|
static::assertEquals("----", $definition->getNewLine(), "Line terminator should be '----'"); |
|
118
|
|
|
static::assertEquals(["----"], $definition->getNewLines(), "Line terminator should be '----'"); |
|
119
|
|
|
|
|
120
|
|
|
static::assertSame($definition, $definition->setNewLine(['----', '+++']), "setLineTerminator should be fluent"); |
|
121
|
|
|
static::assertEquals("----", $definition->getNewLine(), "Line terminator should be '----'"); |
|
122
|
|
|
static::assertEquals(["----", "+++"], $definition->getNewLines(), "Line terminator should be ['----','+++']"); |
|
123
|
|
|
|
|
124
|
|
|
static::assertSame($definition, $definition->setEscape('"'), "Set escape character should be fluent"); |
|
125
|
|
|
static::assertEquals('"', $definition->getEscape(), "Escape character should be modified"); |
|
126
|
|
|
static::assertTrue($definition->hasEscape(), "Format should have an escape character"); |
|
127
|
|
|
|
|
128
|
|
|
static::assertSame($definition, $definition->setEscape(''), "Set escape character should be fluent"); |
|
129
|
|
|
static::assertEquals('', $definition->getEscape(), "Escape character should be modified"); |
|
130
|
|
|
static::assertFalse($definition->hasEscape(), "Format should not have an escape character"); |
|
131
|
|
|
|
|
132
|
|
|
static::assertSame($definition, $definition->setLimit(3), "setLimit should be fluent"); |
|
133
|
|
|
static::assertEquals(3, $definition->getLimit(), "Limit should be modified"); |
|
134
|
|
|
|
|
135
|
|
|
static::assertSame($definition, $definition->setDoubleQuote(true), 'setDoubleQuote should be fluent'); |
|
136
|
|
|
static::assertTrue($definition->useDoubleQuotes(), 'isDoubleQuote should be true'); |
|
137
|
|
|
|
|
138
|
|
|
static::assertSame($definition, $definition->setBom(Bom::BOM_UTF32_BE), 'setBom should be fluent'); |
|
139
|
|
|
static::assertEquals(Bom::BOM_UTF32_BE, $definition->getBom(), 'Bom should be set to the UTF32BE BOM'); |
|
140
|
|
|
static::assertEquals([Bom::BOM_UTF32_BE], $definition->getBoms(), 'Bom should be set to the UTF32BE BOM'); |
|
141
|
|
|
static::assertEquals( |
|
142
|
|
|
'UTF-32BE', |
|
143
|
|
|
$definition->getEncoding(), |
|
144
|
|
|
'getEncoding should be modified after setting the BOM' |
|
145
|
|
|
); |
|
146
|
|
|
|
|
147
|
|
|
static::assertSame($definition, $definition->setBom([Bom::BOM_UTF16_BE, Bom::BOM_UTF16_LE])); |
|
148
|
|
|
static::assertEquals(Bom::BOM_UTF16_BE, $definition->getBom(), 'Bom should be set to UTF16BE for writing'); |
|
149
|
|
|
static::assertEquals( |
|
150
|
|
|
[Bom::BOM_UTF16_BE, Bom::BOM_UTF16_LE], |
|
151
|
|
|
$definition->getBoms(), |
|
152
|
|
|
'Boms should be set to both UTF16 BOMs' |
|
153
|
|
|
); |
|
154
|
|
|
static::assertEquals( |
|
155
|
|
|
'UTF-16BE', |
|
156
|
|
|
$definition->getEncoding(), |
|
157
|
|
|
'getEncoding should be modified after setting the BOM as an array' |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
// reset |
|
161
|
|
|
$definition->setBom(null); |
|
162
|
|
|
static::assertEquals(null, $definition->getBom(), 'Bom should be reset to null'); |
|
163
|
|
|
static::assertEquals( |
|
164
|
|
|
[Bom::BOM_UTF8, Bom::BOM_UTF16_BE, Bom::BOM_UTF16_LE, Bom::BOM_UTF32_BE, Bom::BOM_UTF32_LE], |
|
165
|
|
|
$definition->getBoms(), |
|
166
|
|
|
'Bom should be reset to null' |
|
167
|
|
|
); |
|
168
|
|
|
static::assertEquals( |
|
169
|
|
|
'UTF-8', |
|
170
|
|
|
$definition->getEncoding(), |
|
171
|
|
|
'The encoding should be reset when no BOM is present' |
|
172
|
|
|
); |
|
173
|
|
|
|
|
174
|
|
|
static::assertSame($definition, $definition->setEncoding('UTF-16'), 'setEncoding should be fluent'); |
|
175
|
|
|
static::assertEquals('UTF-16', $definition->getEncoding(), 'The encoding should be set to UTF-16'); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function testSettingHeaderRowToLargerThanDataStartWillModifyDataStart() |
|
179
|
|
|
{ |
|
180
|
|
|
$definition = new CsvFormat(); |
|
181
|
|
|
static::assertEquals(-1, $definition->getHeaderRow()); |
|
182
|
|
|
static::assertEquals(1, $definition->getDataStart()); |
|
183
|
|
|
|
|
184
|
|
|
$definition->setHeaderRow(2); |
|
185
|
|
|
static::assertEquals(2, $definition->getHeaderRow()); |
|
186
|
|
|
static::assertEquals(3, $definition->getDataStart()); |
|
187
|
|
|
|
|
188
|
|
|
$definition->setDataStart(5); |
|
189
|
|
|
static::assertEquals(2, $definition->getHeaderRow()); |
|
190
|
|
|
static::assertEquals(5, $definition->getDataStart()); |
|
191
|
|
|
|
|
192
|
|
|
$definition->setDataStart(1); |
|
193
|
|
|
static::assertEquals(2, $definition->getHeaderRow()); |
|
194
|
|
|
static::assertEquals(3, $definition->getDataStart()); |
|
195
|
|
|
|
|
196
|
|
|
$definition->setHeaderRow(-1); |
|
197
|
|
|
$definition->setDataStart(-1); |
|
198
|
|
|
static::assertEquals(-1, $definition->getHeaderRow()); |
|
199
|
|
|
static::assertEquals(1, $definition->getDataStart()); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function testSettingAnInvalidBomWillThrowAnException() |
|
203
|
|
|
{ |
|
204
|
|
|
$definition = new CsvFormat(); |
|
205
|
|
|
|
|
206
|
|
|
static::expectException(InvalidArgumentException::class); |
|
207
|
|
|
$definition->setBom('INVALID'); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|