1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
<?php
namespace League\Csv;
use PHPUnit_Framework_TestCase;
use SplTempFileObject;
/**
* @group reader
*/
class ReaderTest extends PHPUnit_Framework_TestCase
{
private $csv;
private $expected = [
['john', 'doe', 'john.doe@example.com'],
['jane','doe','jane.doe@example.com'],
];
public function setUp()
{
$csv = new SplTempFileObject;
foreach ($this->expected as $row) {
$csv->fputcsv($row);
}
$this->csv = new Reader($csv);
}
public function testFetchAll()
{
$func = function ($value) {
return array_map('strtoupper', $value);
};
$this->assertSame($this->expected, $this->csv->fetchAll());
$this->assertSame(array_map($func, $this->expected), $this->csv->fetchAll($func));
}
public function testFetchAssoc()
{
$keys = ['firstname', 'lastname', 'email'];
$res = $this->csv->fetchAssoc($keys);
foreach ($res as $index => $row) {
$this->assertSame($keys, array_keys($row));
$this->assertSame($this->expected[$index], array_values($row));
}
}
public function testFetchAssocCallback()
{
$keys = ['firstname', 'lastname', 'email'];
$func = function ($value) {
return array_map('strtoupper', $value);
};
$res = $this->csv->fetchAssoc($keys, $func);
foreach ($res as $row) {
$this->assertSame($keys, array_keys($row));
}
}
public function testFetchAssocLessKeys()
{
$keys = ['firstname'];
$res = $this->csv->fetchAssoc($keys);
$this->assertSame([['firstname' => 'john'], ['firstname' => 'jane']], $res);
}
public function testFetchAssocMoreKeys()
{
$keys = ['firstname', 'lastname', 'email', 'age'];
$res = $this->csv->fetchAssoc($keys);
foreach ($res as $row) {
$this->assertCount(4, array_values($row));
$this->assertNull($row['age']);
}
}
/**
* @expectedException InvalidArgumentException
*/
public function testFetchAssocKeyFailure()
{
$this->csv->fetchAssoc([['firstname', 'lastname', 'email', 'age']]);
}
public function testFetchCol()
{
$this->assertSame(['john', 'jane'], $this->csv->fetchCol(0));
}
public function testFetchColEmptyCol()
{
$raw = [
['john', 'doe'],
['lara', 'croft', 'lara.croft@example.com']
];
$file = new SplTempFileObject;
foreach ($raw as $row) {
$file->fputcsv($row);
}
$csv = new Reader($file);
$res = $csv->fetchCol(2);
$this->assertInternalType('array', $res);
$this->assertCount(2, $res);
$this->assertNull($res[0][2]);
}
public function testFetchColCallback()
{
$func = function ($value) {
return array_map('strtoupper', $value);
};
$this->assertSame(['JOHN', 'JANE'], $this->csv->fetchCol(0, $func));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testFetchColFailure()
{
$this->csv->fetchCol('toto');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testfetchOne()
{
$this->assertSame($this->expected[0], $this->csv->fetchOne(0));
$this->assertSame($this->expected[1], $this->csv->fetchOne(1));
$this->assertSame([], $this->csv->fetchOne(35));
$this->csv->fetchOne(-5);
}
public function testGetWriter()
{
$writer = $this->csv->getWriter();
$writer->insertOne(['toto', 'le', 'herisson']);
$expected = <<<EOF
<table class="table-csv-data">
<tr>
<td>john</td>
<td>doe</td>
<td>john.doe@example.com</td>
</tr>
<tr>
<td>jane</td>
<td>doe</td>
<td>jane.doe@example.com</td>
</tr>
<tr>
<td>toto</td>
<td>le</td>
<td>herisson</td>
</tr>
</table>
EOF;
$this->assertSame($expected, $writer->toHTML());
}
public function testGetWriter2()
{
$csv = (new Reader(__DIR__.'/foo.csv'))->getWriter('a+');
$this->assertInstanceOf('\League\Csv\Writer', $csv);
}
}
|