1
|
|
|
<?php declare( strict_types = 1 ); |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Tests\Core\Extractors; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\Extractors\CsvExtractor; |
6
|
|
|
use Coco\SourceWatcher\Core\IO\Inputs\FileInput; |
7
|
|
|
use Coco\SourceWatcher\Core\IO\Inputs\Input; |
8
|
|
|
use Coco\SourceWatcher\Core\Row; |
9
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class CsvExtractorTest |
14
|
|
|
* @package Coco\SourceWatcher\Tests\Core\Extractors |
15
|
|
|
*/ |
16
|
|
|
class CsvExtractorTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
public function testSetGetColumns () : void |
19
|
|
|
{ |
20
|
|
|
$csvExtractor = new CsvExtractor(); |
21
|
|
|
|
22
|
|
|
$givenColumns = array( "id", "name", "email" ); |
23
|
|
|
$expectedColumns = array( "id", "name", "email" ); |
24
|
|
|
|
25
|
|
|
$csvExtractor->setColumns( $givenColumns ); |
26
|
|
|
|
27
|
|
|
$this->assertEquals( $expectedColumns, $csvExtractor->getColumns() ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testSetGetDelimiter () : void |
31
|
|
|
{ |
32
|
|
|
$csvExtractor = new CsvExtractor(); |
33
|
|
|
|
34
|
|
|
$givenDelimiter = ","; |
35
|
|
|
$expectedDelimiter = ","; |
36
|
|
|
|
37
|
|
|
$csvExtractor->setDelimiter( $givenDelimiter ); |
38
|
|
|
|
39
|
|
|
$this->assertEquals( $expectedDelimiter, $csvExtractor->getDelimiter() ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testSetGetEnclosure () : void |
43
|
|
|
{ |
44
|
|
|
$csvExtractor = new CsvExtractor(); |
45
|
|
|
|
46
|
|
|
$givenEnclosure = "\""; |
47
|
|
|
$expectedEnclosure = "\""; |
48
|
|
|
|
49
|
|
|
$csvExtractor->setEnclosure( $givenEnclosure ); |
50
|
|
|
|
51
|
|
|
$this->assertEquals( $expectedEnclosure, $csvExtractor->getEnclosure() ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testSetGetInput () : void |
55
|
|
|
{ |
56
|
|
|
$csvExtractor = new CsvExtractor(); |
57
|
|
|
|
58
|
|
|
$givenInput = new FileInput( "/some/file/path/file.csv" ); |
59
|
|
|
$expectedInput = new FileInput( "/some/file/path/file.csv" ); |
60
|
|
|
|
61
|
|
|
$csvExtractor->setInput( $givenInput ); |
62
|
|
|
|
63
|
|
|
$this->assertEquals( $expectedInput, $csvExtractor->getInput() ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testExceptionNoInput () : void |
67
|
|
|
{ |
68
|
|
|
$this->expectException( SourceWatcherException::class ); |
69
|
|
|
|
70
|
|
|
$csvExtractor = new CsvExtractor(); |
71
|
|
|
$csvExtractor->extract(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testExceptionNoFileInput () : void |
75
|
|
|
{ |
76
|
|
|
$this->expectException( SourceWatcherException::class ); |
77
|
|
|
|
78
|
|
|
$csvExtractor = new CsvExtractor(); |
79
|
|
|
$csvExtractor->setInput( $this->createMock( Input::class ) ); |
80
|
|
|
$csvExtractor->extract(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testLoadCsvWithDefaultOptions () : void |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
$csvExtractor = new CsvExtractor(); |
87
|
|
|
|
88
|
|
|
$expected = [ new Row( [ "id" => 1, "name" => "John Doe", "email" => "[email protected]" ] ), new Row( [ "id" => 2, "name" => "Jane Doe", "email" => "[email protected]" ] ) ]; |
89
|
|
|
|
90
|
|
|
$csvExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" ) ); |
91
|
|
|
|
92
|
|
|
$this->assertEquals( $expected, $csvExtractor->extract() ); |
93
|
|
|
} catch ( SourceWatcherException $sourceWatcherException ) { |
|
|
|
|
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testColumnsWithNoIndex1 () : void |
99
|
|
|
{ |
100
|
|
|
try { |
101
|
|
|
$csvExtractor = new CsvExtractor(); |
102
|
|
|
$csvExtractor->setColumns( array( "id", "email" ) ); |
103
|
|
|
$csvExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" ) ); |
104
|
|
|
|
105
|
|
|
$expected = [ new Row( [ "id" => 1, "email" => "[email protected]" ] ), new Row( [ "id" => 2, "email" => "[email protected]" ] ) ]; |
106
|
|
|
|
107
|
|
|
$this->assertEquals( $expected, $csvExtractor->extract() ); |
108
|
|
|
} catch ( SourceWatcherException $sourceWatcherException ) { |
|
|
|
|
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testColumnsWithNoIndex2 () : void |
114
|
|
|
{ |
115
|
|
|
try { |
116
|
|
|
$csvExtractor = new CsvExtractor(); |
117
|
|
|
$csvExtractor->setColumns( array( "id", "name", "email" ) ); |
118
|
|
|
$csvExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" ) ); |
119
|
|
|
|
120
|
|
|
$expected = [ new Row( [ "id" => 1, "name" => "John Doe", "email" => "[email protected]" ] ), new Row( [ "id" => 2, "name" => "Jane Doe", "email" => "[email protected]" ] ) ]; |
121
|
|
|
|
122
|
|
|
$this->assertEquals( $expected, $csvExtractor->extract() ); |
123
|
|
|
} catch ( SourceWatcherException $sourceWatcherException ) { |
|
|
|
|
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testGetColumnsWithDifferentNames () : void |
129
|
|
|
{ |
130
|
|
|
try { |
131
|
|
|
$csvExtractor = new CsvExtractor(); |
132
|
|
|
$csvExtractor->setColumns( array( "id" => "id", "email" => "email_address" ) ); |
133
|
|
|
$csvExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" ) ); |
134
|
|
|
|
135
|
|
|
$expected = [ new Row( [ "id" => 1, "email_address" => "[email protected]" ] ), new Row( [ "id" => 2, "email_address" => "[email protected]" ] ) ]; |
136
|
|
|
|
137
|
|
|
$this->assertEquals( $expected, $csvExtractor->extract() ); |
138
|
|
|
} catch ( SourceWatcherException $sourceWatcherException ) { |
|
|
|
|
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|