Passed
Branch feature/static-formatter (adf5e0)
by Daniel
02:33
created

WriterTest::assertHeaderWrittenToFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
        $writer->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
        $moreRows = [
77
            ['more1', 'row1'],
78
            ['more2', 'row2'],
79
            ['more3', 'row3']
80
        ];
81
        $writer->addRows($moreRows);
82
        $this->assertCount(6, $writer->getRows());
83
84
        return $writer;
85
    }
86
87
    public function testSetHeader()
88
    {
89
        $header = ['first_name', 'last_name'];
90
91
        $writer = new WriterSpy();
92
        $writer->setHeader($header);
93
94
        $this->assertEquals($header, $writer->getHeader());
95
    }
96
97
    public function testSetHeaderWithFormatters()
98
    {
99
        $writer = new Csv\Writer();
100
        $rows   = [['dan', 'mcadams']];
101
        $writer->addRows($rows);
102
103
        $header = ['first_name', 'last_name'];
104
        $expectedFormattedHeader = ['First Name', 'Last Name'];
105
        $formatters = [
106
            new Csv\Formatter\UpperCaseWordsFormatter(),
107
            new Csv\Formatter\UnderscoreToSpaceFormatter()
108
        ];
109
        $writer->setHeader($header, $formatters);
110
111
        $writer->saveToFile($this->filename);
112
        $this->assertCsvWrittenToFile($expectedFormattedHeader, $rows);
113
    }
114
115
    public function testWriteCsvToFile()
116
    {
117
        $header = ['first_name', 'last_name', 'email_address'];
118
        $rows   = [
119
            ['Dan', 'McAdams', '[email protected]'],
120
            ['Lara', 'McAdams', '[email protected]'],
121
            ['Test', 'User', '[email protected]']
122
        ];
123
        $writer = new Csv\Writer();
124
        $writer->setHeader($header);
125
        $writer->addRow();
126
        $writer->addRows($rows);
127
        $writer->saveToFile($this->filename);
128
129
        $handle = fopen($this->filename, 'r');
130
        $this->assertEquals($header, 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

130
        $this->assertEquals($header, fgetcsv(/** @scrutinizer ignore-type */ $handle));
Loading history...
131
        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

131
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
132
133
        return $writer;
134
    }
135
136
    /**
137
     * @depends testWriteCsvToFile
138
     */
139
    public function testWriteCsvToNonSeekableStream(Csv\WriterInterface $writer)
140
    {
141
        $header = ['first_name', 'last_name', 'email_address'];
142
        $writer->setHeader($header);
143
144
        ob_start();
145
        $writer->saveToFile('php://output');
146
        $output = ob_get_clean();
147
148
        $this->assertContains(implode(',', $header), $output);
149
    }
150
151
    /**
152
     * @depends testWriteCsvToFile
153
     */
154
    public function testWriteToString(Csv\WriterInterface $writer)
155
    {
156
        $header = ['first_name', 'last_name', 'email_address'];
157
        $writer->setHeader($header);
158
159
        $this->assertContains(implode(',', $header), $writer->writeToString());
160
    }
161
162
    /**
163
     * @depends testWriteCsvToFile
164
     */
165
    public function testWriteCsvDifferentLineEndings(Csv\Writer $writer)
166
    {
167
        $writer->setNewline(Csv\Newline::NEWLINE_CRLF);
168
        $writer->saveToFile($this->filename);
169
        $this->assertLineEnding(Csv\Newline::NEWLINE_CRLF);
170
    }
171
172
    private function assertCsvWrittenToFile($header, $rows)
173
    {
174
        $this->assertGreaterThan(0, filesize($this->filename), 'No data written to file');
175
176
        $handle = fopen($this->filename, 'r');
177
        $this->assertEquals($header, 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

177
        $this->assertEquals($header, fgetcsv(/** @scrutinizer ignore-type */ $handle));
Loading history...
178
179
        foreach ($rows as $row) {
180
            $this->assertEquals($row, fgetcsv($handle));
181
        }
182
183
        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

183
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
184
    }
185
186
    private function assertLineEnding($lineEnding)
187
    {
188
        $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

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