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