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\Row; |
7
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
8
|
|
|
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class CsvExtractorTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function testSetterGetterAttributeColumns () : void |
14
|
|
|
{ |
15
|
|
|
$csvExtractor = new CsvExtractor(); |
16
|
|
|
|
17
|
|
|
$givenColumns = array( "id", "name", "email" ); |
18
|
|
|
$expectedColumns = array( "id", "name", "email" ); |
19
|
|
|
|
20
|
|
|
$csvExtractor->setColumns( $givenColumns ); |
21
|
|
|
|
22
|
|
|
$this->assertEquals( $expectedColumns, $csvExtractor->getColumns() ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testSetterGetterAttributeDelimiter () : void |
26
|
|
|
{ |
27
|
|
|
$csvExtractor = new CsvExtractor(); |
28
|
|
|
|
29
|
|
|
$givenDelimiter = ","; |
30
|
|
|
$expectedDelimiter = ","; |
31
|
|
|
|
32
|
|
|
$csvExtractor->setDelimiter( $givenDelimiter ); |
33
|
|
|
|
34
|
|
|
$this->assertEquals( $expectedDelimiter, $csvExtractor->getDelimiter() ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testSetterGetterAttributeEnclosure () : void |
38
|
|
|
{ |
39
|
|
|
$csvExtractor = new CsvExtractor(); |
40
|
|
|
|
41
|
|
|
$givenEnclosure = "\""; |
42
|
|
|
$expectedEnclosure = "\""; |
43
|
|
|
|
44
|
|
|
$csvExtractor->setEnclosure( $givenEnclosure ); |
45
|
|
|
|
46
|
|
|
$this->assertEquals( $expectedEnclosure, $csvExtractor->getEnclosure() ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testSetterGetterAttributeInput () : void |
50
|
|
|
{ |
51
|
|
|
$csvExtractor = new CsvExtractor(); |
52
|
|
|
|
53
|
|
|
$givenInput = "/some/file/path/file.csv"; |
54
|
|
|
$expectedInput = "/some/file/path/file.csv"; |
55
|
|
|
|
56
|
|
|
$csvExtractor->setInput( $givenInput ); |
57
|
|
|
|
58
|
|
|
$this->assertEquals( $expectedInput, $csvExtractor->getInput() ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testLoadCsvWithDefaultOptions () : void |
62
|
|
|
{ |
63
|
|
|
$csvExtractor = new CsvExtractor(); |
64
|
|
|
|
65
|
|
|
$expected = [ new Row( [ "id" => 1, "name" => "John Doe", "email" => "[email protected]" ] ), new Row( [ "id" => 2, "name" => "Jane Doe", "email" => "[email protected]" ] ) ]; |
66
|
|
|
|
67
|
|
|
$csvExtractor->setInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" ); |
68
|
|
|
|
69
|
|
|
$this->assertEquals( $expected, $csvExtractor->extract() ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testNoInputException () : void |
73
|
|
|
{ |
74
|
|
|
$this->expectException( SourceWatcherException::class ); |
75
|
|
|
|
76
|
|
|
$csvExtractor = new CsvExtractor(); |
77
|
|
|
$csvExtractor->setInput( null ); |
78
|
|
|
$csvExtractor->extract(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|