1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SubjectivePHPTest\Csv; |
4
|
|
|
|
5
|
|
|
use SplFileObject; |
6
|
|
|
use SubjectivePHP\Csv\ProvidedHeaderStrategy; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @coversDefaultClass \SubjectivePHP\Csv\ProvidedHeaderStrategy |
11
|
|
|
* @covers ::__construct |
12
|
|
|
*/ |
13
|
|
|
final class ProvidedHeaderStrategyTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
const HEADERS = ['id', 'author', 'title', 'genre', 'price', 'publish_date', 'description']; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @test |
19
|
|
|
* @covers ::getHeaders |
20
|
|
|
*/ |
21
|
|
|
public function getHeaders() |
22
|
|
|
{ |
23
|
|
|
$fileObject = $this->getFileObject(); |
24
|
|
|
$strategy = $this->getStrategy(); |
25
|
|
|
$this->assertSame(self::HEADERS, $strategy->getHeaders($fileObject)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @test |
30
|
|
|
* @covers ::isHeaderRow |
31
|
|
|
*/ |
32
|
|
|
public function rowIsHeaderRow() |
33
|
|
|
{ |
34
|
|
|
$strategy = $this->getStrategy(); |
35
|
|
|
$this->assertTrue($strategy->isHeaderRow(self::HEADERS)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @test |
40
|
|
|
* @covers ::isHeaderRow |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function rowIsNotHeaderRow() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$strategy = $this->getStrategy(); |
45
|
|
|
$fileObject = $this->getFileObject(); |
46
|
|
|
$fileObject->fgetcsv(); |
47
|
|
|
$this->assertFalse($strategy->isHeaderRow($fileObject->fgetcsv())); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @test |
52
|
|
|
* @covers ::createDataRow |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function createDataRow() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$row = [ |
57
|
|
|
'bk101', |
58
|
|
|
'Gambardella, Matthew', |
59
|
|
|
'XML Developer\'s Guide', |
60
|
|
|
'Computer', |
61
|
|
|
'44.95', |
62
|
|
|
'2000-10-01', |
63
|
|
|
'An in-depth look at creating applications with XML.', |
64
|
|
|
]; |
65
|
|
|
$strategy = $this->getStrategy(); |
66
|
|
|
$this->assertSame( |
67
|
|
|
[ |
68
|
|
|
'id' => 'bk101', |
69
|
|
|
'author' => 'Gambardella, Matthew', |
70
|
|
|
'title' => 'XML Developer\'s Guide', |
71
|
|
|
'genre' => 'Computer', |
72
|
|
|
'price' => '44.95', |
73
|
|
|
'publish_date' => '2000-10-01', |
74
|
|
|
'description' => 'An in-depth look at creating applications with XML.', |
75
|
|
|
], |
76
|
|
|
$strategy->createDataRow($row) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
private function getFileObject() : SplFileObject |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$fileObject = new SplFileObject(__DIR__ . '/_files/basic.csv'); |
83
|
|
|
$fileObject->setFlags(SplFileObject::READ_CSV); |
84
|
|
|
$fileObject->setCsvControl(','); |
85
|
|
|
return $fileObject; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function getStrategy() : ProvidedHeaderStrategy |
89
|
|
|
{ |
90
|
|
|
return new ProvidedHeaderStrategy(self::HEADERS); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.