1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/csv-token |
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/csv-token/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/csv-token |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\CsvToken\Test\Unit\Format; |
15
|
|
|
|
16
|
|
|
use Graze\CsvToken\Csv\Bom; |
17
|
|
|
use Graze\CsvToken\Csv\CsvConfiguration; |
18
|
|
|
use Graze\CsvToken\Csv\CsvConfigurationInterface; |
19
|
|
|
use Graze\CsvToken\Test\TestCase; |
20
|
|
|
|
21
|
|
|
class CsvConfigurationTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
public function testImplementsInterface() |
24
|
|
|
{ |
25
|
|
|
$definition = new CsvConfiguration(); |
26
|
|
|
|
27
|
|
|
static::assertInstanceOf(CsvConfigurationInterface::class, $definition); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testDefaultsAreAssignedWhenNoOptionsSupplied() |
31
|
|
|
{ |
32
|
|
|
$definition = new CsvConfiguration(); |
33
|
|
|
|
34
|
|
|
static::assertEquals(',', $definition->getDelimiter(), "Default Delimiter should be ','"); |
35
|
|
|
static::assertEquals('"', $definition->getQuote(), "Default quote character should be \""); |
36
|
|
|
static::assertEquals('\\N', $definition->getNullValue(), "Null character should be '\\N'"); |
37
|
|
|
static::assertEquals( |
38
|
|
|
["\n", "\r", "\r\n"], |
39
|
|
|
$definition->getNewLines(), |
40
|
|
|
"Line terminator should be ['\\n','\\r','\\r\\n']" |
41
|
|
|
); |
42
|
|
|
static::assertEquals('\\', $definition->getEscape(), "Default escape character should be '\\'"); |
43
|
|
|
static::assertEquals(false, $definition->useDoubleQuotes(), "Double quote should be off by default"); |
44
|
|
|
static::assertEquals( |
45
|
|
|
[ |
46
|
|
|
Bom::BOM_UTF8, |
47
|
|
|
Bom::BOM_UTF16_BE, |
48
|
|
|
Bom::BOM_UTF16_LE, |
49
|
|
|
Bom::BOM_UTF32_BE, |
50
|
|
|
Bom::BOM_UTF32_LE, |
51
|
|
|
], |
52
|
|
|
$definition->getBoms(), |
53
|
|
|
"By default BOM should be set to all BOMs" |
54
|
|
|
); |
55
|
|
|
static::assertEquals('UTF-8', $definition->getEncoding(), "Default encoding should be 'UTF-8'"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testAssigningOptionsModifiesTheDefinition() |
59
|
|
|
{ |
60
|
|
|
$definition = new CsvConfiguration([ |
61
|
|
|
'delimiter' => "\t", |
62
|
|
|
'quote' => '', |
63
|
|
|
'null' => '', |
64
|
|
|
'newLines' => ["----"], |
65
|
|
|
'escape' => '"', |
66
|
|
|
'doubleQuote' => true, |
67
|
|
|
'encoding' => 'UTF-16LE', |
68
|
|
|
'boms' => [Bom::BOM_UTF16_LE], |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
static::assertEquals("\t", $definition->getDelimiter(), "Delimiter should be set to '\\t' (tab)"); |
72
|
|
|
static::assertEquals('', $definition->getQuote(), "Quote character should be blank"); |
73
|
|
|
static::assertEquals('', $definition->getNullValue(), "Null character should be '' (blank)'"); |
74
|
|
|
static::assertEquals(["----"], $definition->getNewLines(), "Line terminator should be '----'"); |
75
|
|
|
static::assertEquals('"', $definition->getEscape(), 'Escape Character should be "'); |
76
|
|
|
static::assertEquals(true, $definition->useDoubleQuotes(), 'double quote should be on'); |
77
|
|
|
static::assertEquals('UTF-16LE', $definition->getEncoding(), "encoding should be 'UTF-16LE'"); |
78
|
|
|
static::assertEquals([Bom::BOM_UTF16_LE], $definition->getBoms(), "Boms should be [Bom::BOM_UTF16_LE]"); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|