Passed
Branch feature/static-formatter (adf5e0)
by Daniel
02:33
created

HeaderTest::testAddInvalidFormatterString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
nc 1
nop 0
dl 0
loc 8
c 0
b 0
f 0
cc 1
rs 9.4285
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
15
use PHPUnit\Framework\TestCase;
16
use RoadBunch\Csv\Exception\FormatterException;
17
use RoadBunch\Csv\Exception\InvalidInputArrayException;
18
use RoadBunch\Csv\Formatter\Formatter;
19
use RoadBunch\Csv\Formatter\SplitCamelCaseWordsFormatter;
20
use RoadBunch\Csv\Formatter\UnderscoreToSpaceFormatter;
21
use RoadBunch\Csv\Formatter\UpperCaseWordsFormatter;
22
use RoadBunch\Csv\Header\Header;
23
use RoadBunch\Csv\Header\HeaderInterface;
24
25
/**
26
 * Class HeaderTest
27
 *
28
 * @author  Dan McAdams
29
 * @package RoadBunch\Csv\Tests
30
 */
31
class HeaderTest extends TestCase
32
{
33
    public function testCreateHeader()
34
    {
35
        $header = new Header();
36
        $this->assertNotNull($header);
37
    }
38
39
    public function testGetColumns()
40
    {
41
        $header = new Header();
42
        $this->assertInternalType('array', $header->getFormattedColumns());
43
    }
44
45
    public function testAddColumn()
46
    {
47
        $header = new Header();
48
        $header->addColumn('First Name');
49
50
        $this->assertCount(1, $header->getFormattedColumns());
51
    }
52
53
    public function testAddMultipleColumns()
54
    {
55
        $header = new Header();
56
57
        $this->assertCount(0, $header->getFormattedColumns());
58
59
        $header->addColumn('one');
60
        $this->assertCount(1, $header->getFormattedColumns());
61
62
        $header->addColumn('two');
63
        $this->assertCount(2, $header->getFormattedColumns());
64
65
        $header->addColumn('three');
66
        $this->assertCount(3, $header->getFormattedColumns());
67
    }
68
69
    public function testCreateFromArray()
70
    {
71
        $testHeaderArray = $this->getTestHeaderArray();
72
73
        $header = new Header($testHeaderArray);
74
        $this->assertCount(count($testHeaderArray), $header->getFormattedColumns());
75
76
        $header->addColumn('employee id');
77
        $this->assertCount(count($testHeaderArray) + 1, $header->getFormattedColumns());
78
    }
79
80
    public function testAddFormatters()
81
    {
82
        $header = new HeaderSpy(['first_name', 'last_name', 'camelCased']);
83
        $header->addFormatter(UnderscoreToSpaceFormatter::class);
84
        $this->assertCount(1, $header->getFormatters());
85
        $header->addFormatter(UpperCaseWordsFormatter::class);
86
        $this->assertCount(2, $header->getFormatters());
87
        $header->addFormatter(SplitCamelCaseWordsFormatter::class);
88
        $this->assertCount(3, $header->getFormatters());
89
90
        $formattedHeader = $header->getFormattedColumns();
91
        $this->assertEquals(['First Name', 'Last Name', 'Camel Cased'], $formattedHeader);
92
    }
93
94
    public function testAddInvalidFormatterString()
95
    {
96
        $this->expectException(FormatterException::class);
97
98
        $testHeaderArray = $this->getTestHeaderArray();
99
        $header = new Header($testHeaderArray);
100
101
        $header->addFormatter('fakeformatter');
102
    }
103
104
    public function testAddInvalidFormatterObject()
105
    {
106
        $this->expectException(FormatterException::class);
107
108
        $testHeaderArray = $this->getTestHeaderArray();
109
        $header = new Header($testHeaderArray);
110
111
        $header->addFormatter(new Header([]));
0 ignored issues
show
Bug introduced by
new RoadBunch\Csv\Header\Header(array()) of type RoadBunch\Csv\Header\Header is incompatible with the type string|RoadBunch\Csv\Formatter\FormatterInterface expected by parameter $formatter of RoadBunch\Csv\Header\Header::addFormatter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
        $header->addFormatter(/** @scrutinizer ignore-type */ new Header([]));
Loading history...
112
    }
113
114
    /**
115
     * @throws InvalidInputArrayException
116
     */
117
    public function testArrayOfNonStrings()
118
    {
119
        $this->expectException(InvalidInputArrayException::class);
120
121
        $multiArray = [
122
            ['an array'],
123
            new \stdClass(),
124
            $this
125
        ];
126
        new Header($multiArray);
127
    }
128
129
    /**
130
     * @return array
131
     */
132
    private function getTestHeaderArray(): array
133
    {
134
        return ['first_name', 'last_name', 'birthday', 'phone_number', 'email_address'];
135
    }
136
}
137