Passed
Push — master ( 646aca...16a29e )
by Jean Paul
01:32
created

testGetColumnsWithDifferentNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
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
    /**
82
     * This test is similar to the code from: samples/Core/Extractors/CsvExtractorSample2.php
83
     *
84
     * @throws SourceWatcherException
85
     */
86
    public function testColumnsWithNoIndex1 () : void
87
    {
88
        $csvExtractor = new CsvExtractor();
89
        $csvExtractor->setColumns( array( "id", "email" ) );
90
        $csvExtractor->setInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" );
91
92
        $expected = [ new Row( [ "id" => 1, "email" => "[email protected]" ] ), new Row( [ "id" => 2, "email" => "[email protected]" ] ) ];
93
94
        $this->assertEquals( $expected, $csvExtractor->extract() );
95
    }
96
97
    /**
98
     * This test is similar to the code from: samples/Core/Extractors/CsvExtractorSample3.php
99
     *
100
     * @throws SourceWatcherException
101
     */
102
    public function testColumnsWithNoIndex2 () : void
103
    {
104
        $csvExtractor = new CsvExtractor();
105
        $csvExtractor->setColumns( array( "id", "name", "email" ) );
106
        $csvExtractor->setInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" );
107
108
        $expected = [ new Row( [ "id" => 1, "name" => "John Doe", "email" => "[email protected]" ] ), new Row( [ "id" => 2, "name" => "Jane Doe", "email" => "[email protected]" ] ) ];
109
110
        $this->assertEquals( $expected, $csvExtractor->extract() );
111
    }
112
113
    /**
114
     * This test is similar to the code from: samples/Core/Extractors/CsvExtractorSample4.php
115
     *
116
     * @throws SourceWatcherException
117
     */
118
    public function testGetColumnsWithDifferentNames () : void
119
    {
120
        $csvExtractor = new CsvExtractor();
121
        $csvExtractor->setColumns( array( "id" => "id", "email" => "email_address" ) );
122
        $csvExtractor->setInput( __DIR__ . "/../../../samples/data/csv/csv1.csv" );
123
124
        $expected = [ new Row( [ "id" => 1, "email_address" => "[email protected]" ] ), new Row( [ "id" => 2, "email_address" => "[email protected]" ] ) ];
125
126
        $this->assertEquals( $expected, $csvExtractor->extract() );
127
    }
128
}
129