Passed
Push — master ( 14b05a...fa6902 )
by Daniel
02:41
created

WriterTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testSetHeaderPutsHeaderRowBeforeData() 0 6 1
A testCreateWriter() 0 4 1
A tearDown() 0 4 2
A testSetHeader() 0 8 1
A testAddRows() 0 13 1
A testCreateFile() 0 4 1
A testAddRowsNotArrayOfArrays() 0 7 1
A testAddRow() 0 10 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($this->filename);
43
        $this->assertEquals($this->filename, $writer->getFilename());
44
    }
45
46
    public function testCreateFile()
47
    {
48
        new Csv\Writer($this->filename);
49
        $this->assertTrue(is_file($this->filename), 'File does not exist');
50
    }
51
52
    public function testAddRow()
53
    {
54
        $writer = new WriterSpy($this->filename);
55
        $rowOne = ['Dan', 'McAdams'];
56
        $rowTwo = ['Lara', 'McAdams'];
57
58
        $writer->addRow($rowOne)
59
               ->addRow($rowTwo);
60
61
        $this->assertCount(2, $writer->getRows());
62
    }
63
64
    public function testAddRowsNotArrayOfArrays()
65
    {
66
        $this->expectException(Csv\Exception\InvalidInputArrayException::class);
67
        $writer = new Csv\Writer($this->filename);
68
        $rows   = ['Dan', 'McAdams'];
69
70
        $writer->addRows($rows);
71
    }
72
73
    public function testAddRows()
74
    {
75
        $writer = new WriterSpy($this->filename);
76
        $rows   = [
77
            ['Dan', 'McAdams'],
78
            ['Lara', 'McAdams'],
79
            ['Test', 'User']
80
        ];
81
        $writer->addRows($rows);
82
83
        $this->assertCount(count($rows), $writer->getRows());
84
85
        return $writer;
86
    }
87
88
    public function testSetHeader()
89
    {
90
        $header = ['first_name', 'last_name'];
91
92
        $writer = new WriterSpy($this->filename);
93
        $writer->setHeader($header);
94
95
        $this->assertEquals($header, $writer->getHeader());
96
    }
97
98
    /**
99
     * @depends testAddRows
100
     * @param Csv\WriterInterface|WriterSpy $writer
101
     */
102
    public function testSetHeaderPutsHeaderRowBeforeData(Csv\WriterInterface $writer)
103
    {
104
        $header = ['first_name', 'last_name'];
105
106
        $writer->setHeader($header);
107
        $this->assertEquals($header, $writer->getRows()[0]);
0 ignored issues
show
Bug introduced by
The method getRows() does not exist on RoadBunch\Csv\WriterInterface. It seems like you code against a sub-type of RoadBunch\Csv\WriterInterface such as RoadBunch\Csv\Tests\Csv\WriterSpy. ( Ignorable by Annotation )

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

107
        $this->assertEquals($header, $writer->/** @scrutinizer ignore-call */ getRows()[0]);
Loading history...
108
    }
109
}
110