testDefaultsAreAssignedWhenNoOptionsSupplied()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
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
use InvalidArgumentException;
21
22
class CsvConfigurationTest extends TestCase
23
{
24
    public function testImplementsInterface()
25
    {
26
        $definition = new CsvConfiguration();
27
28
        static::assertInstanceOf(CsvConfigurationInterface::class, $definition);
29
    }
30
31
    public function testDefaultsAreAssignedWhenNoOptionsSupplied()
32
    {
33
        $definition = new CsvConfiguration();
34
35
        static::assertEquals(',', $definition->getDelimiter(), "Default Delimiter should be ','");
36
        static::assertEquals('"', $definition->getQuote(), "Default quote character should be \"");
37
        static::assertEquals('\\N', $definition->getNullValue(), "Null character should be '\\N'");
38
        static::assertEquals(
39
            ["\n", "\r", "\r\n"],
40
            $definition->getNewLines(),
41
            "Line terminator should be ['\\n','\\r','\\r\\n']"
42
        );
43
        static::assertEquals('\\', $definition->getEscape(), "Default escape character should be '\\'");
44
        static::assertEquals(false, $definition->useDoubleQuotes(), "Double quote should be off by default");
45
        static::assertEquals(
46
            [
47
                Bom::BOM_UTF8,
48
                Bom::BOM_UTF16_BE,
49
                Bom::BOM_UTF16_LE,
50
                Bom::BOM_UTF32_BE,
51
                Bom::BOM_UTF32_LE,
52
            ],
53
            $definition->getBoms(),
54
            "By default BOM should be set to all BOMs"
55
        );
56
        static::assertEquals('UTF-8', $definition->getEncoding(), "Default encoding should be 'UTF-8'");
57
    }
58
59
    public function testAssigningOptionsModifiesTheDefinition()
60
    {
61
        $definition = new CsvConfiguration([
62
            'delimiter'   => "\t",
63
            'quote'       => '',
64
            'null'        => '',
65
            'newLines'    => ["----"],
66
            'escape'      => '"',
67
            'doubleQuote' => true,
68
            'encoding'    => 'UTF-16LE',
69
            'boms'        => [Bom::BOM_UTF16_LE],
70
        ]);
71
72
        static::assertEquals("\t", $definition->getDelimiter(), "Delimiter should be set to '\\t' (tab)");
73
        static::assertEquals('', $definition->getQuote(), "Quote character should be blank");
74
        static::assertEquals('', $definition->getNullValue(), "Null character should be '' (blank)'");
75
        static::assertEquals(["----"], $definition->getNewLines(), "Line terminator should be '----'");
76
        static::assertEquals('"', $definition->getEscape(), 'Escape Character should be "');
77
        static::assertEquals(true, $definition->useDoubleQuotes(), 'double quote should be on');
78
        static::assertEquals('UTF-16LE', $definition->getEncoding(), "encoding should be 'UTF-16LE'");
79
        static::assertEquals([Bom::BOM_UTF16_LE], $definition->getBoms(), "Boms should be [Bom::BOM_UTF16_LE]");
80
    }
81
82
    public function testNewLinesInTheWrongFormatWillThrowAnException()
83
    {
84
        static::expectException(InvalidArgumentException::class);
85
        new CsvConfiguration([
86
            'newLines' => '---',
87
        ]);
88
    }
89
90
    public function testBomsInTheWrongFormatWillThrowAnException()
91
    {
92
        static::expectException(InvalidArgumentException::class);
93
        new CsvConfiguration([
94
            'boms' => '',
95
        ]);
96
    }
97
}
98