Passed
Push — master ( 6998a3...224daf )
by Daniel
02:08
created

HeaderTest::testAddColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Csv-Machine package.
5
 *
6
 * (c) Dan McAdams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace RoadBunch\Csv\Tests\Header;
13
14
use PHPUnit\Framework\TestCase;
15
use RoadBunch\Csv\Exception\FormatterException;
16
use RoadBunch\Csv\Exception\InvalidInputArrayException;
17
use RoadBunch\Csv\Formatter\SplitCamelCaseWordsFormatter;
18
use RoadBunch\Csv\Formatter\UnderscoreToSpaceFormatter;
19
use RoadBunch\Csv\Formatter\UpperCaseWordsFormatter;
20
use RoadBunch\Csv\Header\Header;
21
22
/**
23
 * Class HeaderTest
24
 *
25
 * @author  Dan McAdams
26
 * @package RoadBunch\Csv\Tests
27
 */
28
class HeaderTest extends TestCase
29
{
30
    public function testCreateHeader()
31
    {
32
        $header = new Header();
33
        $this->assertNotNull($header);
34
    }
35
36
    public function testGetColumns()
37
    {
38
        $header = new Header();
39
        $this->assertInternalType('array', $header->getFormattedColumns());
40
    }
41
42
    public function testAddColumn()
43
    {
44
        $header = new Header();
45
        $header->addColumn('First Name');
46
47
        $this->assertCount(1, $header->getFormattedColumns());
48
    }
49
50
    public function testAddMultipleColumns()
51
    {
52
        $header = new Header();
53
54
        $this->assertCount(0, $header->getFormattedColumns());
55
56
        $header->addColumn('one');
57
        $this->assertCount(1, $header->getFormattedColumns());
58
59
        $header->addColumn('two');
60
        $this->assertCount(2, $header->getFormattedColumns());
61
62
        $header->addColumn('three');
63
        $this->assertCount(3, $header->getFormattedColumns());
64
    }
65
66
    public function testCreateFromArray()
67
    {
68
        $testHeaderArray = $this->getTestHeaderArray();
69
70
        $header = new Header($testHeaderArray);
71
        $this->assertCount(count($testHeaderArray), $header->getFormattedColumns());
72
73
        $header->addColumn('employee id');
74
        $this->assertCount(count($testHeaderArray) + 1, $header->getFormattedColumns());
75
    }
76
77
    public function testAddFormattersByClassName()
78
    {
79
        $header = new HeaderSpy(['first_name', 'last_name', 'camelCased']);
80
        $header->addFormatter(UnderscoreToSpaceFormatter::class);
81
        $this->assertCount(1, $header->getFormatters());
82
        $header->addFormatter(UpperCaseWordsFormatter::class);
83
        $this->assertCount(2, $header->getFormatters());
84
    }
85
86
    public function testAddFormattersByObject()
87
    {
88
        $header = new HeaderSpy(['first_name', 'last_name', 'camelCased']);
89
        $header->addFormatter(new UnderscoreToSpaceFormatter());
90
        $this->assertCount(1, $header->getFormatters());
91
        $header->addFormatter(new UpperCaseWordsFormatter());
92
        $this->assertCount(2, $header->getFormatters());
93
    }
94
95
    public function testAddInvalidFormatterString()
96
    {
97
        $this->expectException(FormatterException::class);
98
99
        $testHeaderArray = $this->getTestHeaderArray();
100
        $header = new Header($testHeaderArray);
101
102
        $header->addFormatter('fakeformatter');
103
    }
104
105
    public function testAddInvalidFormatterObject()
106
    {
107
        $this->expectException(FormatterException::class);
108
109
        $testHeaderArray = $this->getTestHeaderArray();
110
        $header = new Header($testHeaderArray);
111
112
        $header->addFormatter(new Header([]));
113
    }
114
115
    /**
116
     * @throws InvalidInputArrayException
117
     */
118
    public function testArrayOfNonStrings()
119
    {
120
        $this->expectException(InvalidInputArrayException::class);
121
122
        $multiArray = [
123
            ['an array'],
124
            new \stdClass(),
125
            $this
126
        ];
127
        new Header($multiArray);
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    private function getTestHeaderArray(): array
134
    {
135
        return ['first_name', 'last_name', 'birthday', 'phone_number', 'email_address'];
136
    }
137
}
138