Passed
Push — master ( 8d8767...99864b )
by Daniel
01:52
created

WriterTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 167
rs 10
c 0
b 0
f 0
wmc 17

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testCreateWriter() 0 4 1
A tearDown() 0 4 2
A testSetHeader() 0 8 1
A testAddRows() 0 13 1
A testAddRowsNotArrayOfArrays() 0 7 1
A testAddRow() 0 10 1
A assertLineEnding() 0 4 1
A assertHeaderWrittenToFile() 0 4 1
A testWriteCsvDifferentLineEndings() 0 5 1
A assertCsvWrittenToFile() 0 12 2
A testWriteCsvToNonSeekableStream() 0 10 1
A testSetHeaderWithFormatters() 0 16 1
A testWriteCsvToFile() 0 19 1
A testWriteToString() 0 6 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Csv-Machine package.
5
 *
6
 * (c) Dan McAdams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace RoadBunch\Csv\Tests\Csv;
13
14
15
use PHPUnit\Framework\TestCase;
16
use RoadBunch\Csv;
17
18
/**
19
 * Class WriterTest
20
 *
21
 * @author  Dan McAdams
22
 * @package RoadBunch\Csv\Tests\Csv
23
 */
24
class WriterTest extends TestCase
25
{
26
    protected $filename;
27
28
    public function setUp()
29
    {
30
        $this->filename = sprintf('%s/%s.csv', __DIR__, md5(uniqid()));
31
    }
32
33
    public function tearDown()
34
    {
35
        if (is_file($this->filename)) {
36
            unlink($this->filename);
37
        }
38
    }
39
40
    public function testCreateWriter()
41
    {
42
        $writer = new WriterSpy();
43
        $this->assertNotNull($writer);
44
    }
45
46
    public function testAddRow()
47
    {
48
        $writer = new WriterSpy();
49
        $rowOne = ['Dan', 'McAdams'];
50
        $rowTwo = ['Lara', 'McAdams'];
51
52
        $writer->addRow($rowOne)
53
               ->addRow($rowTwo);
54
55
        $this->assertCount(2, $writer->getRows());
56
    }
57
58
    public function testAddRowsNotArrayOfArrays()
59
    {
60
        $this->expectException(Csv\Exception\InvalidInputArrayException::class);
61
        $writer = new Csv\Writer();
62
        $rows   = ['Dan', 'McAdams'];
63
64
        $writer->addRows($rows);
65
    }
66
67
    public function testAddRows()
68
    {
69
        $writer = new WriterSpy();
70
        $rows   = [
71
            ['Dan', 'McAdams'],
72
            ['Lara', 'McAdams'],
73
            ['Test', 'User']
74
        ];
75
        $writer->addRows($rows);
76
77
        $this->assertCount(count($rows), $writer->getRows());
78
79
        return $writer;
80
    }
81
82
    public function testSetHeader()
83
    {
84
        $header = ['first_name', 'last_name'];
85
86
        $writer = new WriterSpy();
87
        $writer->setHeader($header);
88
89
        $this->assertEquals($header, $writer->getHeader());
90
    }
91
92
    public function testSetHeaderWithFormatters()
93
    {
94
        $writer = new Csv\Writer();
95
        $rows   = [['dan', 'mcadams']];
96
        $writer->addRows($rows);
97
98
        $header = ['first_name', 'last_name'];
99
        $expectedFormattedHeader = ['First Name', 'Last Name'];
100
        $formatters = [
101
            new Csv\Formatter\UpperCaseWordsFormatter(),
102
            new Csv\Formatter\UnderscoreToSpaceFormatter()
103
        ];
104
        $writer->setHeader($header, $formatters);
105
106
        $writer->write($this->filename);
107
        $this->assertCsvWrittenToFile($expectedFormattedHeader, $rows);
108
    }
109
110
    public function testWriteCsvToFile()
111
    {
112
        $header = ['first_name', 'last_name', 'email_address'];
113
        $rows   = [
114
            ['Dan', 'McAdams', '[email protected]'],
115
            ['Lara', 'McAdams', '[email protected]'],
116
            ['Test', 'User', '[email protected]']
117
        ];
118
        $writer = new Csv\Writer();
119
        $writer->addRows($rows);
120
        $writer->setHeader($header);
121
122
        $writer->write($this->filename);
123
124
        $handle = fopen($this->filename, 'r');
125
        $this->assertHeaderWrittenToFile($header, $handle);
126
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

126
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
127
128
        return $writer;
129
    }
130
131
    /**
132
     * @depends testWriteCsvToFile
133
     */
134
    public function testWriteCsvToNonSeekableStream(Csv\WriterInterface $writer)
135
    {
136
        $header = ['first_name', 'last_name', 'email_address'];
137
        $writer->setHeader($header);
138
139
        ob_start();
140
        $writer->write('php://output');
141
        $output = ob_get_clean();
142
143
        $this->assertContains(implode(',', $header), $output);
144
    }
145
146
    /**
147
     * @depends testWriteCsvToFile
148
     */
149
    public function testWriteToString(Csv\WriterInterface $writer)
150
    {
151
        $header = ['first_name', 'last_name', 'email_address'];
152
        $writer->setHeader($header);
153
154
        $this->assertContains(implode(',', $header), $writer->writeToString());
155
    }
156
157
    /**
158
     * @depends testWriteCsvToFile
159
     */
160
    public function testWriteCsvDifferentLineEndings(Csv\Writer $writer)
161
    {
162
        $writer->setNewline(Csv\Newline::NEWLINE_CRLF);
163
        $writer->write($this->filename);
164
        $this->assertLineEnding(Csv\Newline::NEWLINE_CRLF);
165
    }
166
167
    private function assertHeaderWrittenToFile($header, $handle)
168
    {
169
        $headerFromCSV = fgetcsv($handle);
170
        $this->assertEquals($header, $headerFromCSV);
171
    }
172
173
    private function assertCsvWrittenToFile($header, $rows)
174
    {
175
        $this->assertGreaterThan(0, filesize($this->filename), 'No data written to file');
176
177
        $handle        = fopen($this->filename, 'r');
178
        $this->assertHeaderWrittenToFile($header, $handle);
179
180
        foreach ($rows as $row) {
181
            $this->assertEquals($row, fgetcsv($handle));
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fgetcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

181
            $this->assertEquals($row, fgetcsv(/** @scrutinizer ignore-type */ $handle));
Loading history...
182
        }
183
184
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

184
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
185
    }
186
187
    private function assertLineEnding($lineEnding)
188
    {
189
        $line = fgets(fopen($this->filename, 'r'));
0 ignored issues
show
Bug introduced by
It seems like fopen($this->filename, 'r') can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

189
        $line = fgets(/** @scrutinizer ignore-type */ fopen($this->filename, 'r'));
Loading history...
190
        $this->assertEquals($lineEnding, substr($line, -2));
191
    }
192
}
193