|
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\DataFile\Format\CsvFormat; |
|
17
|
|
|
use Graze\DataFile\Test\TestCase; |
|
18
|
|
|
|
|
19
|
|
|
class CsvFormatTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function testImplementsInterface() |
|
22
|
|
|
{ |
|
23
|
|
|
$definition = new CsvFormat(); |
|
24
|
|
|
|
|
25
|
|
|
static::assertInstanceOf('Graze\DataFile\Format\CsvFormatInterface', $definition); |
|
26
|
|
|
|
|
27
|
|
|
static::assertEquals('csv', $definition->getType()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testDefaultsAreAssignedWhenNoOptionsSupplied() |
|
31
|
|
|
{ |
|
32
|
|
|
$definition = new CsvFormat(); |
|
33
|
|
|
|
|
34
|
|
|
static::assertEquals(',', $definition->getDelimiter(), "Default Delimiter should be ','"); |
|
35
|
|
|
static::assertEquals('"', $definition->getQuoteCharacter(), "Default quote character should be \""); |
|
36
|
|
|
static::assertTrue($definition->hasQuotes(), "Quoting should be on by default"); |
|
37
|
|
|
static::assertEquals('\\N', $definition->getNullOutput(), "Null character should be '\\N'"); |
|
38
|
|
|
static::assertFalse($definition->hasHeaderRow(), "Headers should be on by default"); |
|
39
|
|
|
static::assertEquals(-1, $definition->getHeaderRow(), "Header row should be -1 by default"); |
|
40
|
|
|
static::assertEquals(1, $definition->getDataStart(), "Get data start should be 1 by default"); |
|
41
|
|
|
static::assertEquals("\n", $definition->getLineTerminator(), "Line terminator should be '\\n'"); |
|
42
|
|
|
static::assertEquals('\\', $definition->getEscapeCharacter(), "Default escape character should be '\\'"); |
|
43
|
|
|
static::assertTrue($definition->hasEscapeCharacter()); |
|
44
|
|
|
static::assertEquals(-1, $definition->getLimit(), "Default limit should be -1"); |
|
45
|
|
|
static::assertEquals(false, $definition->isDoubleQuote(), "Double quote should be off by default"); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testAssigningOptionsModifiesTheDefinition() |
|
49
|
|
|
{ |
|
50
|
|
|
$definition = new CsvFormat([ |
|
51
|
|
|
'delimiter' => "\t", |
|
52
|
|
|
'quoteCharacter' => '', |
|
53
|
|
|
'nullOutput' => '', |
|
54
|
|
|
'headerRow' => 1, |
|
55
|
|
|
'dataStart' => 5, |
|
56
|
|
|
'lineTerminator' => "----", |
|
57
|
|
|
'escape' => '', |
|
58
|
|
|
'limit' => 2, |
|
59
|
|
|
'doubleQuote' => true, |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
|
|
static::assertEquals("\t", $definition->getDelimiter(), "Delimiter should be set to '\\t' (tab)"); |
|
63
|
|
|
static::assertEquals('', $definition->getQuoteCharacter(), "Quote character should be blank"); |
|
64
|
|
|
static::assertFalse($definition->hasQuotes(), "Quoting should be off"); |
|
65
|
|
|
static::assertEquals('', $definition->getNullOutput(), "Null character should be '' (blank)'"); |
|
66
|
|
|
static::assertTrue($definition->hasHeaderRow(), "Headers should be on"); |
|
67
|
|
|
static::assertEquals(1, $definition->getHeaderRow(), "Header row should be set to 1"); |
|
68
|
|
|
static::assertEquals(5, $definition->getDataStart(), "Data Start should be set to 5"); |
|
69
|
|
|
static::assertEquals("----", $definition->getLineTerminator(), "Line terminator should be '----'"); |
|
70
|
|
|
static::assertEquals('', $definition->getEscapeCharacter(), "Escape Character should be '' (blank)"); |
|
71
|
|
|
static::assertFalse($definition->hasEscapeCharacter(), "Format should not be marked as not having escape"); |
|
72
|
|
|
static::assertEquals(2, $definition->getLimit(), 'Limit should be 2'); |
|
73
|
|
|
static::assertEquals(true, $definition->isDoubleQuote(), 'double quote should be on'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testSettingProperties() |
|
77
|
|
|
{ |
|
78
|
|
|
$definition = new CsvFormat(); |
|
79
|
|
|
|
|
80
|
|
|
static::assertEquals(',', $definition->getDelimiter(), "Default Delimiter should be ','"); |
|
81
|
|
|
static::assertEquals('"', $definition->getQuoteCharacter(), "Default quote character should be \""); |
|
82
|
|
|
static::assertTrue($definition->hasQuotes(), "Quoting should be on by default"); |
|
83
|
|
|
static::assertEquals('\\N', $definition->getNullOutput(), "Null character should be '\\N'"); |
|
84
|
|
|
static::assertFalse($definition->hasHeaderRow(), "Headers should be off by default"); |
|
85
|
|
|
static::assertEquals(1, $definition->getDataStart(), "Data start should be 1 by default"); |
|
86
|
|
|
static::assertEquals("\n", $definition->getLineTerminator(), "Line terminator should be '\\n'"); |
|
87
|
|
|
static::assertEquals('\\', $definition->getEscapeCharacter(), "Default escape character should be '\\'"); |
|
88
|
|
|
static::assertTrue($definition->hasEscapeCharacter()); |
|
89
|
|
|
static::assertEquals(-1, $definition->getLimit(), "Default limit should be -1"); |
|
90
|
|
|
static::assertEquals(false, $definition->isDoubleQuote(), "Double quote should be off by default"); |
|
91
|
|
|
|
|
92
|
|
|
static::assertSame($definition, $definition->setDelimiter("\t"), "SetDelimiter should be fluent"); |
|
93
|
|
|
static::assertEquals("\t", $definition->getDelimiter(), "Delimiter should be set to '\\t' (tab)"); |
|
94
|
|
|
static::assertSame($definition, $definition->setQuoteCharacter(''), "setQuoteCharacter should be fluent"); |
|
95
|
|
|
static::assertEquals('', $definition->getQuoteCharacter(), "Quote character should be blank"); |
|
96
|
|
|
static::assertFalse($definition->hasQuotes(), "Quoting should be off"); |
|
97
|
|
|
static::assertSame($definition, $definition->setNullOutput(''), "setNullOutput should be fluent"); |
|
98
|
|
|
static::assertEquals('', $definition->getNullOutput(), "Null character should be '' (blank)'"); |
|
99
|
|
|
static::assertSame($definition, $definition->setHeaderRow(1), "setHeaders should be fluent"); |
|
100
|
|
|
static::assertTrue($definition->hasHeaderRow(), "Headers should be on"); |
|
101
|
|
|
static::assertEquals(1, $definition->getHeaderRow(), "Headers should be set to 1"); |
|
102
|
|
|
static::assertSame($definition, $definition->setDataStart(2), "setDataStart should be fluent"); |
|
103
|
|
|
static::assertEquals(2, $definition->getDataStart(), "Data Start should be 2"); |
|
104
|
|
|
static::assertSame($definition, $definition->setLineTerminator('----'), "setLineTerminator should be fluent"); |
|
105
|
|
|
static::assertEquals("----", $definition->getLineTerminator(), "Line terminator should be '----'"); |
|
106
|
|
|
static::assertSame($definition, $definition->setEscapeCharacter('"'), "Set escape character should be fluent"); |
|
107
|
|
|
static::assertEquals('"', $definition->getEscapeCharacter(), "Escape character should be modified"); |
|
108
|
|
|
static::assertTrue($definition->hasEscapeCharacter(), "Format should have an escape character"); |
|
109
|
|
|
static::assertSame($definition, $definition->setEscapeCharacter(''), "Set escape character should be fluent"); |
|
110
|
|
|
static::assertEquals('', $definition->getEscapeCharacter(), "Escape character should be modified"); |
|
111
|
|
|
static::assertFalse($definition->hasEscapeCharacter(), "Format should not have an escape character"); |
|
112
|
|
|
static::assertSame($definition, $definition->setLimit(3), "setLimit should be fluent"); |
|
113
|
|
|
static::assertEquals(3, $definition->getLimit(), "Limit should be modified"); |
|
114
|
|
|
static::assertSame($definition, $definition->setDoubleQuote(true), 'setDoubleQuote should be fluent'); |
|
115
|
|
|
static::assertTrue($definition->isDoubleQuote(), 'isDoubleQuote should be true'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testSettingHeaderRowToLargerThanDataStartWillModifyDataStart() |
|
119
|
|
|
{ |
|
120
|
|
|
$definition = new CsvFormat(); |
|
121
|
|
|
static::assertEquals(-1, $definition->getHeaderRow()); |
|
122
|
|
|
static::assertEquals(1, $definition->getDataStart()); |
|
123
|
|
|
|
|
124
|
|
|
$definition->setHeaderRow(2); |
|
125
|
|
|
static::assertEquals(2, $definition->getHeaderRow()); |
|
126
|
|
|
static::assertEquals(3, $definition->getDataStart()); |
|
127
|
|
|
|
|
128
|
|
|
$definition->setDataStart(5); |
|
129
|
|
|
static::assertEquals(2, $definition->getHeaderRow()); |
|
130
|
|
|
static::assertEquals(5, $definition->getDataStart()); |
|
131
|
|
|
|
|
132
|
|
|
$definition->setDataStart(1); |
|
133
|
|
|
static::assertEquals(2, $definition->getHeaderRow()); |
|
134
|
|
|
static::assertEquals(3, $definition->getDataStart()); |
|
135
|
|
|
|
|
136
|
|
|
$definition->setHeaderRow(-1); |
|
137
|
|
|
$definition->setDataStart(-1); |
|
138
|
|
|
static::assertEquals(-1, $definition->getHeaderRow()); |
|
139
|
|
|
static::assertEquals(1, $definition->getDataStart()); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|