Passed
Push — master ( aa88c1...a2e34f )
by Daniel
01:46
created

WriterTest::assertLineEnding()   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 1
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
               ->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
    /**
93
     * @depends testAddRows
94
     * @param WriterSpy $writer
95
     */
96
    public function testSetHeaderPutsHeaderRowBeforeData($writer)
97
    {
98
        $header = ['first_name', 'last_name'];
99
100
        $writer->setHeader($header);
101
        $this->assertEquals($header, $writer->getRows()[0]);
102
    }
103
104
    public function testWriteCsvToFile()
105
    {
106
        $header = ['first_name', 'last_name', 'email_address'];
107
        $rows   = [
108
            ['Dan', 'McAdams', '[email protected]'],
109
            ['Lara', 'McAdams', '[email protected]'],
110
            ['Test', 'User', '[email protected]']
111
        ];
112
        $writer = new Csv\Writer();
113
        $writer->addRows($rows);
114
        $writer->setHeader($header);
115
116
        $writer->write($this->filename);
117
        $this->assertCsvWrittenToFile($header, $rows);
118
119
        return $writer;
120
    }
121
122
    private function assertCsvWrittenToFile($header, $rows)
123
    {
124
        $this->assertGreaterThan(0, filesize($this->filename), 'No data written to file');
125
126
        $handle        = fopen($this->filename, 'r');
127
        $headerFromCSV = 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

127
        $headerFromCSV = fgetcsv(/** @scrutinizer ignore-type */ $handle);
Loading history...
128
129
        $this->assertEquals($header, $headerFromCSV);
130
        foreach ($rows as $row) {
131
            $this->assertEquals($row, fgetcsv($handle));
132
        }
133
134
        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

134
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
135
    }
136
137
    /**
138
     * @depends testWriteCsvToFile
139
     */
140
    public function testWriteCsvDifferentLineEndings(Csv\Writer $writer)
141
    {
142
        $writer->setNewline(Csv\Newline::NEWLINE_CRLF);
143
        $writer->write($this->filename);
144
        $this->assertLineEnding(Csv\Newline::NEWLINE_CRLF);
145
    }
146
147
    private function assertLineEnding($lineEnding)
148
    {
149
        $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

149
        $line = fgets(/** @scrutinizer ignore-type */ fopen($this->filename, 'r'));
Loading history...
150
        $this->assertEquals($lineEnding, substr($line, -2));
151
    }
152
}
153