Passed
Push — master ( 4318d1...8d8767 )
by Daniel
01:56
created

WriterTest::testSetHeaderPutsHeaderRowBeforeData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
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
    public function testWriteCsvToFile()
93
    {
94
        $header = ['first_name', 'last_name', 'email_address'];
95
        $rows   = [
96
            ['Dan', 'McAdams', '[email protected]'],
97
            ['Lara', 'McAdams', '[email protected]'],
98
            ['Test', 'User', '[email protected]']
99
        ];
100
        $writer = new Csv\Writer();
101
        $writer->addRows($rows);
102
        $writer->setHeader($header);
103
104
        $writer->write($this->filename);
105
        $this->assertCsvWrittenToFile($header, $rows);
106
107
        return $writer;
108
    }
109
110
    private function assertCsvWrittenToFile($header, $rows)
111
    {
112
        $this->assertGreaterThan(0, filesize($this->filename), 'No data written to file');
113
114
        $handle        = fopen($this->filename, 'r');
115
        $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

115
        $headerFromCSV = fgetcsv(/** @scrutinizer ignore-type */ $handle);
Loading history...
116
117
        $this->assertEquals($header, $headerFromCSV);
118
        foreach ($rows as $row) {
119
            $this->assertEquals($row, fgetcsv($handle));
120
        }
121
122
        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

122
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
123
    }
124
125
    /**
126
     * @depends testWriteCsvToFile
127
     */
128
    public function testWriteCsvDifferentLineEndings(Csv\Writer $writer)
129
    {
130
        $writer->setNewline(Csv\Newline::NEWLINE_CRLF);
131
        $writer->write($this->filename);
132
        $this->assertLineEnding(Csv\Newline::NEWLINE_CRLF);
133
    }
134
135
    private function assertLineEnding($lineEnding)
136
    {
137
        $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

137
        $line = fgets(/** @scrutinizer ignore-type */ fopen($this->filename, 'r'));
Loading history...
138
        $this->assertEquals($lineEnding, substr($line, -2));
139
    }
140
}
141