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\Exceptions\InvalidHeaderArrayException; |
17
|
|
|
use RoadBunch\Csv\Formatters\UnderscoreToSpaceFormatter; |
18
|
|
|
use RoadBunch\Csv\Formatters\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 testSkipDuplicateColumns() |
50
|
|
|
{ |
51
|
|
|
$header = new Header(); |
52
|
|
|
|
53
|
|
|
// test adding duplicate columns |
54
|
|
|
$header->addColumn('Test Column Three This should only be added once'); |
55
|
|
|
$header->addColumn('Test Column Three This should only be added once'); |
56
|
|
|
$header->addColumn('Test Column Four'); |
57
|
|
|
$header->addColumn('Test Column Five'); |
58
|
|
|
|
59
|
|
|
$this->assertCount(3, $header->getColumns()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testCreateFromArray() |
63
|
|
|
{ |
64
|
|
|
$testHeaderArray = $this->getTestHeaderArray(); |
65
|
|
|
|
66
|
|
|
$header = new Header($testHeaderArray); |
67
|
|
|
$this->assertCount(count($testHeaderArray), $header->getColumns()); |
68
|
|
|
|
69
|
|
|
$header->addColumn('employee id'); |
70
|
|
|
$this->assertCount(count($testHeaderArray) + 1, $header->getColumns()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testAddFormatters() |
74
|
|
|
{ |
75
|
|
|
$header = new HeaderSpy(['first_name', 'last_name']); |
76
|
|
|
$header->addFormatter(new UpperCaseWordsFormatter()); |
77
|
|
|
$this->assertCount(1, $header->getFormatters()); |
78
|
|
|
$header->addFormatter(new UnderscoreToSpaceFormatter()); |
79
|
|
|
$this->assertCount(2, $header->getFormatters()); |
80
|
|
|
|
81
|
|
|
$this->assertEquals(['First Name', 'Last Name'], $header->getColumns()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws InvalidHeaderArrayException |
86
|
|
|
*/ |
87
|
|
|
public function testArrayOfNonStrings() |
88
|
|
|
{ |
89
|
|
|
$this->expectException(InvalidHeaderArrayException::class); |
90
|
|
|
|
91
|
|
|
$multiArray = [ |
92
|
|
|
['an array'], |
93
|
|
|
new \stdClass(), |
94
|
|
|
$this |
95
|
|
|
]; |
96
|
|
|
new Header($multiArray); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
private function getTestHeaderArray(): array |
103
|
|
|
{ |
104
|
|
|
return ['first_name', 'last_name', 'birthday', 'phone_number', 'email_address']; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|