Passed
Push — master ( 7d22f9...1460f7 )
by Jean Paul
01:51
created

CsvExtractorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.9666
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
 *
15
 * @package Coco\SourceWatcher\Tests\Core\Extractors
16
 */
17
class CsvExtractorTest extends TestCase
18
{
19
    private string $csvLocation;
20
21
    private string $idIndex;
22
    private string $nameIndex;
23
    private string $emailIndex;
24
    private string $emailAddressIndex;
25
26
    private string $johnDoeName;
27
    private string $johnDoeEmailAddress;
28
29
    private string $janeDoeName;
30
    private string $janeDoeEmailAddress;
31
32
    public function setUp () : void
33
    {
34
        $this->csvLocation = __DIR__ . "/../../../samples/data/csv/csv1.csv";
35
36
        $this->idIndex = "id";
37
        $this->nameIndex = "name";
38
        $this->emailIndex = "email";
39
        $this->emailAddressIndex = "email_address";
40
41
        $this->johnDoeName = "John Doe";
42
        $this->johnDoeEmailAddress = "[email protected]";
43
44
        $this->janeDoeName = "Jane Doe";
45
        $this->janeDoeEmailAddress = "[email protected]";
46
    }
47
48
    public function testSetGetColumns () : void
49
    {
50
        $csvExtractor = new CsvExtractor();
51
52
        $givenColumns = [ $this->idIndex, $this->nameIndex, $this->emailIndex ];
53
        $expectedColumns = [ $this->idIndex, $this->nameIndex, $this->emailIndex ];
54
55
        $csvExtractor->setColumns( $givenColumns );
56
57
        $this->assertEquals( $expectedColumns, $csvExtractor->getColumns() );
58
    }
59
60
    public function testSetGetDelimiter () : void
61
    {
62
        $csvExtractor = new CsvExtractor();
63
64
        $givenDelimiter = ",";
65
        $expectedDelimiter = ",";
66
67
        $csvExtractor->setDelimiter( $givenDelimiter );
68
69
        $this->assertEquals( $expectedDelimiter, $csvExtractor->getDelimiter() );
70
    }
71
72
    public function testSetGetEnclosure () : void
73
    {
74
        $csvExtractor = new CsvExtractor();
75
76
        $givenEnclosure = "\"";
77
        $expectedEnclosure = "\"";
78
79
        $csvExtractor->setEnclosure( $givenEnclosure );
80
81
        $this->assertEquals( $expectedEnclosure, $csvExtractor->getEnclosure() );
82
    }
83
84
    public function testSetGetInput () : void
85
    {
86
        $csvExtractor = new CsvExtractor();
87
88
        $givenInput = new FileInput( "/some/file/path/file.csv" );
89
        $expectedInput = new FileInput( "/some/file/path/file.csv" );
90
91
        $csvExtractor->setInput( $givenInput );
92
93
        $this->assertEquals( $expectedInput, $csvExtractor->getInput() );
94
    }
95
96
    /**
97
     * @throws SourceWatcherException
98
     */
99
    public function testExceptionNoInput () : void
100
    {
101
        $this->expectException( SourceWatcherException::class );
102
103
        $csvExtractor = new CsvExtractor();
104
        $csvExtractor->extract();
105
    }
106
107
    /**
108
     * @throws SourceWatcherException
109
     */
110
    public function testExceptionNoFileInput () : void
111
    {
112
        $this->expectException( SourceWatcherException::class );
113
114
        $csvExtractor = new CsvExtractor();
115
        $csvExtractor->setInput( $this->createMock( Input::class ) );
116
        $csvExtractor->extract();
117
    }
118
119
    /**
120
     * @throws SourceWatcherException
121
     */
122
    public function testLoadCsvWithDefaultOptions () : void
123
    {
124
        $csvExtractor = new CsvExtractor();
125
126
        $expected = [
127
            new Row( [
128
                $this->idIndex => 1,
129
                $this->nameIndex => $this->johnDoeName,
130
                $this->emailIndex => $this->johnDoeEmailAddress
131
            ] ),
132
            new Row( [
133
                $this->idIndex => 2,
134
                $this->nameIndex => $this->janeDoeName,
135
                $this->emailIndex => $this->janeDoeEmailAddress
136
            ] )
137
        ];
138
139
        $csvExtractor->setInput( new FileInput( $this->csvLocation ) );
140
141
        $this->assertEquals( $expected, $csvExtractor->extract() );
142
    }
143
144
    /**
145
     * @throws SourceWatcherException
146
     */
147
    public function testColumnsWithNoIndex1 () : void
148
    {
149
        $csvExtractor = new CsvExtractor();
150
        $csvExtractor->setColumns( [ $this->idIndex, $this->emailIndex ] );
151
        $csvExtractor->setInput( new FileInput( $this->csvLocation ) );
152
153
        $expected = [
154
            new Row( [ $this->idIndex => 1, $this->emailIndex => $this->johnDoeEmailAddress ] ),
155
            new Row( [ $this->idIndex => 2, $this->emailIndex => $this->janeDoeEmailAddress ] )
156
        ];
157
158
        $this->assertEquals( $expected, $csvExtractor->extract() );
159
    }
160
161
    /**
162
     * @throws SourceWatcherException
163
     */
164
    public function testColumnsWithNoIndex2 () : void
165
    {
166
        $csvExtractor = new CsvExtractor();
167
        $csvExtractor->setColumns( [ $this->idIndex, $this->nameIndex, $this->emailIndex ] );
168
        $csvExtractor->setInput( new FileInput( $this->csvLocation ) );
169
170
        $expected = [
171
            new Row( [
172
                $this->idIndex => 1,
173
                $this->nameIndex => $this->johnDoeName,
174
                $this->emailIndex => $this->johnDoeEmailAddress
175
            ] ),
176
            new Row( [
177
                $this->idIndex => 2,
178
                $this->nameIndex => $this->janeDoeName,
179
                $this->emailIndex => $this->janeDoeEmailAddress
180
            ] )
181
        ];
182
183
        $this->assertEquals( $expected, $csvExtractor->extract() );
184
    }
185
186
    /**
187
     * @throws SourceWatcherException
188
     */
189
    public function testGetColumnsWithDifferentNames () : void
190
    {
191
        $csvExtractor = new CsvExtractor();
192
        $csvExtractor->setColumns( [
193
            $this->idIndex => $this->idIndex,
194
            $this->emailIndex => $this->emailAddressIndex
195
        ] );
196
        $csvExtractor->setInput( new FileInput( $this->csvLocation ) );
197
198
        $expected = [
199
            new Row( [ $this->idIndex => 1, $this->emailAddressIndex => $this->johnDoeEmailAddress ] ),
200
            new Row( [ $this->idIndex => 2, $this->emailAddressIndex => $this->janeDoeEmailAddress ] )
201
        ];
202
203
        $this->assertEquals( $expected, $csvExtractor->extract() );
204
    }
205
}
206