|
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\InvalidHeaderArrayException; |
|
17
|
|
|
use RoadBunch\Csv\Formatter\UnderscoreToSpaceFormatter; |
|
18
|
|
|
use RoadBunch\Csv\Formatter\UpperCaseWordsFormatter; |
|
19
|
|
|
use RoadBunch\Csv\Header\Header; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class HeaderTest |
|
23
|
|
|
* |
|
24
|
|
|
* @author Dan McAdams |
|
25
|
|
|
* @package RoadBunch\Csv\Tests |
|
26
|
|
|
*/ |
|
27
|
|
|
class HeaderTest extends TestCase |
|
28
|
|
|
{ |
|
29
|
|
|
public function testCreateHeader() |
|
30
|
|
|
{ |
|
31
|
|
|
$header = new Header(); |
|
32
|
|
|
$this->assertNotNull($header); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testGetColumns() |
|
36
|
|
|
{ |
|
37
|
|
|
$header = new Header(); |
|
38
|
|
|
$this->assertInternalType('array', $header->getColumns()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testAddColumn() |
|
42
|
|
|
{ |
|
43
|
|
|
$header = new Header(); |
|
44
|
|
|
$header->addColumn('First Name'); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertCount(1, $header->getColumns()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testAddMultipleColumns() |
|
50
|
|
|
{ |
|
51
|
|
|
$header = new Header(); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertCount(0, $header->getColumns()); |
|
54
|
|
|
|
|
55
|
|
|
$header->addColumn('one'); |
|
56
|
|
|
$this->assertCount(1, $header->getColumns()); |
|
57
|
|
|
|
|
58
|
|
|
$header->addColumn('two'); |
|
59
|
|
|
$this->assertCount(2, $header->getColumns()); |
|
60
|
|
|
|
|
61
|
|
|
$header->addColumn('three'); |
|
62
|
|
|
$this->assertCount(3, $header->getColumns()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testCreateFromArray() |
|
66
|
|
|
{ |
|
67
|
|
|
$testHeaderArray = $this->getTestHeaderArray(); |
|
68
|
|
|
|
|
69
|
|
|
$header = new Header($testHeaderArray); |
|
70
|
|
|
$this->assertCount(count($testHeaderArray), $header->getColumns()); |
|
71
|
|
|
|
|
72
|
|
|
$header->addColumn('employee id'); |
|
73
|
|
|
$this->assertCount(count($testHeaderArray) + 1, $header->getColumns()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testAddFormatters() |
|
77
|
|
|
{ |
|
78
|
|
|
$header = new HeaderSpy(['first_name', 'last_name']); |
|
79
|
|
|
$header->addFormatter(new UpperCaseWordsFormatter()); |
|
80
|
|
|
$this->assertCount(1, $header->getFormatters()); |
|
81
|
|
|
$header->addFormatter(new UnderscoreToSpaceFormatter()); |
|
82
|
|
|
$this->assertCount(2, $header->getFormatters()); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertEquals(['First Name', 'Last Name'], $header->getColumns()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @throws InvalidHeaderArrayException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testArrayOfNonStrings() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->expectException(InvalidHeaderArrayException::class); |
|
93
|
|
|
|
|
94
|
|
|
$multiArray = [ |
|
95
|
|
|
['an array'], |
|
96
|
|
|
new \stdClass(), |
|
97
|
|
|
$this |
|
98
|
|
|
]; |
|
99
|
|
|
new Header($multiArray); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
private function getTestHeaderArray(): array |
|
106
|
|
|
{ |
|
107
|
|
|
return ['first_name', 'last_name', 'birthday', 'phone_number', 'email_address']; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|