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::assertTrue($definition->hasHeaders(), "Headers should be on by default"); |
39
|
|
|
static::assertEquals(1, $definition->getHeaders(), "Headers should be 1 by default"); |
40
|
|
|
static::assertEquals("\n", $definition->getLineTerminator(), "Line terminator should be '\\n'"); |
41
|
|
|
static::assertEquals('\\', $definition->getEscapeCharacter(), "Default escape character should be '\\'"); |
42
|
|
|
static::assertEquals(-1, $definition->getLimit(), "Default limit should be -1"); |
43
|
|
|
static::assertEquals(false, $definition->isDoubleQuote(), "Double quote should be off by default"); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testAssigningOptionsModifiesTheDefinition() |
47
|
|
|
{ |
48
|
|
|
$definition = new CsvFormat([ |
49
|
|
|
'delimiter' => "\t", |
50
|
|
|
'quoteCharacter' => '', |
51
|
|
|
'nullOutput' => '', |
52
|
|
|
'headers' => 0, |
53
|
|
|
'lineTerminator' => "----", |
54
|
|
|
'escape' => '"', |
55
|
|
|
'limit' => 2, |
56
|
|
|
'doubleQuote' => true, |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
static::assertEquals("\t", $definition->getDelimiter(), "Delimiter should be set to '\\t' (tab)"); |
60
|
|
|
static::assertEquals('', $definition->getQuoteCharacter(), "Quote character should be blank"); |
61
|
|
|
static::assertFalse($definition->hasQuotes(), "Quoting should be off"); |
62
|
|
|
static::assertEquals('', $definition->getNullOutput(), "Null character should be '' (blank)'"); |
63
|
|
|
static::assertFalse($definition->hasHeaders(), "Headers should be off"); |
64
|
|
|
static::assertEquals("----", $definition->getLineTerminator(), "Line terminator should be '----'"); |
65
|
|
|
static::assertEquals('"', $definition->getEscapeCharacter(), 'Escape Character should be "'); |
66
|
|
|
static::assertEquals(2, $definition->getLimit(), 'Limit should be 2'); |
67
|
|
|
static::assertEquals(true, $definition->isDoubleQuote(), 'double quote should be on'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testSettingProperties() |
71
|
|
|
{ |
72
|
|
|
$definition = new CsvFormat(); |
73
|
|
|
|
74
|
|
|
static::assertEquals(',', $definition->getDelimiter(), "Default Delimiter should be ','"); |
75
|
|
|
static::assertEquals('"', $definition->getQuoteCharacter(), "Default quote character should be \""); |
76
|
|
|
static::assertTrue($definition->hasQuotes(), "Quoting should be on by default"); |
77
|
|
|
static::assertEquals('\\N', $definition->getNullOutput(), "Null character should be '\\N'"); |
78
|
|
|
static::assertTrue($definition->hasHeaders(), "Headers should be on by default"); |
79
|
|
|
static::assertEquals("\n", $definition->getLineTerminator(), "Line terminator should be '\\n'"); |
80
|
|
|
static::assertEquals('\\', $definition->getEscapeCharacter(), "Default escape character should be '\\'"); |
81
|
|
|
static::assertEquals(-1, $definition->getLimit(), "Default limit should be -1"); |
82
|
|
|
static::assertEquals(false, $definition->isDoubleQuote(), "Double quote should be off by default"); |
83
|
|
|
|
84
|
|
|
static::assertSame($definition, $definition->setDelimiter("\t"), "SetDelimiter should be fluent"); |
85
|
|
|
static::assertEquals("\t", $definition->getDelimiter(), "Delimiter should be set to '\\t' (tab)"); |
86
|
|
|
static::assertSame($definition, $definition->setQuoteCharacter(''), "setQuoteCharacter should be fluent"); |
87
|
|
|
static::assertEquals('', $definition->getQuoteCharacter(), "Quote character should be blank"); |
88
|
|
|
static::assertFalse($definition->hasQuotes(), "Quoting should be off"); |
89
|
|
|
static::assertSame($definition, $definition->setNullOutput(''), "setNullOutput should be fluent"); |
90
|
|
|
static::assertEquals('', $definition->getNullOutput(), "Null character should be '' (blank)'"); |
91
|
|
|
static::assertSame($definition, $definition->setHeaders(0), "setHeaders should be fluent"); |
92
|
|
|
static::assertFalse($definition->hasHeaders(), "Headers should be off"); |
93
|
|
|
static::assertEquals(0, $definition->getHeaders(), "Headers should be set to 0"); |
94
|
|
|
static::assertSame($definition, $definition->setLineTerminator('----'), "setLineTerminator should be fluent"); |
95
|
|
|
static::assertEquals("----", $definition->getLineTerminator(), "Line terminator should be '----'"); |
96
|
|
|
static::assertSame($definition, $definition->setEscapeCharacter('"'), "Set escape character should be fluent"); |
97
|
|
|
static::assertEquals('"', $definition->getEscapeCharacter(), "Escape character should be modified"); |
98
|
|
|
static::assertSame($definition, $definition->setLimit(3), "setLimit should be fluent"); |
99
|
|
|
static::assertEquals(3, $definition->getLimit(), "Limit should be modified"); |
100
|
|
|
static::assertSame($definition, $definition->setDoubleQuote(true), 'setDoubleQuote should be fluent'); |
101
|
|
|
static::assertTrue($definition->isDoubleQuote(), 'isDoubleQuote should be true'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|