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]); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|