1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SubjectivePHPTest\Csv; |
4
|
|
|
|
5
|
|
|
use SplFileObject; |
6
|
|
|
use SubjectivePHP\Csv\MappedHeaderStrategy; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @coversDefaultClass \SubjectivePHP\Csv\MappedHeaderStrategy |
11
|
|
|
* @covers ::__construct |
12
|
|
|
*/ |
13
|
|
|
final class MappedHeaderStrategyTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
const HEADER_MAP = [ |
16
|
|
|
'id' => 'Book ID', |
17
|
|
|
'author' => 'Author', |
18
|
|
|
'title' => 'Title', |
19
|
|
|
'genre' => 'Genre', |
20
|
|
|
'price' => 'Price', |
21
|
|
|
'publish_date' => 'Publish Date', |
22
|
|
|
'description' => 'Description', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @test |
27
|
|
|
* @covers ::getHeaders |
28
|
|
|
*/ |
29
|
|
|
public function getHeaders() |
30
|
|
|
{ |
31
|
|
|
$fileObject = $this->getFileObject(); |
32
|
|
|
$strategy = $this->getStrategy(); |
33
|
|
|
$this->assertSame(array_values(self::HEADER_MAP), $strategy->getHeaders($fileObject)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @test |
38
|
|
|
* @covers ::isHeaderRow |
39
|
|
|
*/ |
40
|
|
|
public function rowIsHeaderRow() |
41
|
|
|
{ |
42
|
|
|
$strategy = $this->getStrategy(); |
43
|
|
|
$this->assertTrue($strategy->isHeaderRow(array_keys(self::HEADER_MAP))); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
* @covers ::isHeaderRow |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function rowIsNotHeaderRow() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$strategy = $this->getStrategy(); |
53
|
|
|
$fileObject = $this->getFileObject(); |
54
|
|
|
$fileObject->fgetcsv(); |
55
|
|
|
$this->assertFalse($strategy->isHeaderRow($fileObject->fgetcsv())); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
* @covers ::createDataRow |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function createDataRow() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$row = [ |
65
|
|
|
'bk101', |
66
|
|
|
'Gambardella, Matthew', |
67
|
|
|
'XML Developer\'s Guide', |
68
|
|
|
'Computer', |
69
|
|
|
'44.95', |
70
|
|
|
'2000-10-01', |
71
|
|
|
'An in-depth look at creating applications with XML.', |
72
|
|
|
]; |
73
|
|
|
$strategy = $this->getStrategy(); |
74
|
|
|
$this->assertSame( |
75
|
|
|
[ |
76
|
|
|
'Book ID' => 'bk101', |
77
|
|
|
'Author' => 'Gambardella, Matthew', |
78
|
|
|
'Title' => 'XML Developer\'s Guide', |
79
|
|
|
'Genre' => 'Computer', |
80
|
|
|
'Price' => '44.95', |
81
|
|
|
'Publish Date' => '2000-10-01', |
82
|
|
|
'Description' => 'An in-depth look at creating applications with XML.', |
83
|
|
|
], |
84
|
|
|
$strategy->createDataRow($row) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
private function getFileObject() : SplFileObject |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$fileObject = new SplFileObject(__DIR__ . '/_files/basic.csv'); |
91
|
|
|
$fileObject->setFlags(SplFileObject::READ_CSV); |
92
|
|
|
$fileObject->setCsvControl(','); |
93
|
|
|
return $fileObject; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function getStrategy() : MappedHeaderStrategy |
97
|
|
|
{ |
98
|
|
|
return new MappedHeaderStrategy(self::HEADER_MAP); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.