Passed
Push — master ( 2be253...46260f )
by Jean Paul
01:26
created

CsvExtractorTest::testSetGetInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
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\Inputs\FileInput;
7
use Coco\SourceWatcher\Core\Row;
8
use Coco\SourceWatcher\Core\SourceWatcherException;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * Class CsvExtractorTest
13
 * @package Coco\SourceWatcher\Tests\Core\Extractors
14
 */
15
class CsvExtractorTest extends TestCase
16
{
17
    public function testSetGetColumns () : void
18
    {
19
        $csvExtractor = new CsvExtractor();
20
21
        $givenColumns = array( "id", "name", "email" );
22
        $expectedColumns = array( "id", "name", "email" );
23
24
        $csvExtractor->setColumns( $givenColumns );
25
26
        $this->assertEquals( $expectedColumns, $csvExtractor->getColumns() );
27
    }
28
29
    public function testSetGetDelimiter () : void
30
    {
31
        $csvExtractor = new CsvExtractor();
32
33
        $givenDelimiter = ",";
34
        $expectedDelimiter = ",";
35
36
        $csvExtractor->setDelimiter( $givenDelimiter );
37
38
        $this->assertEquals( $expectedDelimiter, $csvExtractor->getDelimiter() );
39
    }
40
41
    public function testSetGetEnclosure () : void
42
    {
43
        $csvExtractor = new CsvExtractor();
44
45
        $givenEnclosure = "\"";
46
        $expectedEnclosure = "\"";
47
48
        $csvExtractor->setEnclosure( $givenEnclosure );
49
50
        $this->assertEquals( $expectedEnclosure, $csvExtractor->getEnclosure() );
51
    }
52
53
    public function testSetGetInput () : void
54
    {
55
        $csvExtractor = new CsvExtractor();
56
57
        $givenInput = new FileInput( "/some/file/path/file.csv" );
58
        $expectedInput = new FileInput( "/some/file/path/file.csv" );
59
60
        $csvExtractor->setInput( $givenInput );
61
62
        $this->assertEquals( $expectedInput, $csvExtractor->getInput() );
63
    }
64
65
    public function testExceptionNoInput () : void
66
    {
67
        $this->expectException( SourceWatcherException::class );
68
69
        $csvExtractor = new CsvExtractor();
70
71
        $csvExtractor->extract();
72
    }
73
74
    public function testLoadCsvWithDefaultOptions () : void
75
    {
76
        try {
77
            $csvExtractor = new CsvExtractor();
78
79
            $expected = [ new Row( [ "id" => 1, "name" => "John Doe", "email" => "[email protected]" ] ), new Row( [ "id" => 2, "name" => "Jane Doe", "email" => "[email protected]" ] ) ];
80
81
            $csvExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" ) );
82
83
            $this->assertEquals( $expected, $csvExtractor->extract() );
84
        } catch ( SourceWatcherException $sourceWatcherException ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
85
86
        }
87
    }
88
89
    public function testColumnsWithNoIndex1 () : void
90
    {
91
        try {
92
            $csvExtractor = new CsvExtractor();
93
            $csvExtractor->setColumns( array( "id", "email" ) );
94
            $csvExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" ) );
95
96
            $expected = [ new Row( [ "id" => 1, "email" => "[email protected]" ] ), new Row( [ "id" => 2, "email" => "[email protected]" ] ) ];
97
98
            $this->assertEquals( $expected, $csvExtractor->extract() );
99
        } catch ( SourceWatcherException $sourceWatcherException ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
100
101
        }
102
    }
103
104
    public function testColumnsWithNoIndex2 () : void
105
    {
106
        try {
107
            $csvExtractor = new CsvExtractor();
108
            $csvExtractor->setColumns( array( "id", "name", "email" ) );
109
            $csvExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" ) );
110
111
            $expected = [ new Row( [ "id" => 1, "name" => "John Doe", "email" => "[email protected]" ] ), new Row( [ "id" => 2, "name" => "Jane Doe", "email" => "[email protected]" ] ) ];
112
113
            $this->assertEquals( $expected, $csvExtractor->extract() );
114
        } catch ( SourceWatcherException $sourceWatcherException ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
115
116
        }
117
    }
118
119
    public function testGetColumnsWithDifferentNames () : void
120
    {
121
        try {
122
            $csvExtractor = new CsvExtractor();
123
            $csvExtractor->setColumns( array( "id" => "id", "email" => "email_address" ) );
124
            $csvExtractor->setInput( new FileInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" ) );
125
126
            $expected = [ new Row( [ "id" => 1, "email_address" => "[email protected]" ] ), new Row( [ "id" => 2, "email_address" => "[email protected]" ] ) ];
127
128
            $this->assertEquals( $expected, $csvExtractor->extract() );
129
        } catch ( SourceWatcherException $sourceWatcherException ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
130
131
        }
132
    }
133
}
134