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
|
<?php
namespace League\Csv\test;
use ArrayIterator;
use League\Csv\Writer;
use LimitIterator;
use PHPUnit_Framework_TestCase;
use SplFileObject;
use SplTempFileObject;
/**
* @group writer
*/
class WriterTest extends PHPUnit_Framework_TestCase
{
private $csv;
public function setUp()
{
$this->csv = Writer::createFromFileObject(new SplTempFileObject());
}
public function tearDown()
{
$csv = new SplFileObject(__DIR__.'/foo.csv', 'w');
$csv->setCsvControl();
$csv->fputcsv(["john", "doe", "john.doe@example.com"], ",", '"');
$this->csv = null;
}
public function testSupportsStreamFilter()
{
$csv = Writer::createFromPath(__DIR__.'/foo.csv');
$this->assertTrue($csv->isActiveStreamFilter());
$csv->appendStreamFilter('string.toupper');
$csv->insertOne(['jane', 'doe', 'jane@example.com']);
$this->assertFalse($csv->isActiveStreamFilter());
}
public function testInsert()
{
$expected = [
['john', 'doe', 'john.doe@example.com'],
'jane,doe,jane.doe@example.com',
];
foreach ($expected as $row) {
$this->csv->insertOne($row);
}
$this->assertContains(['john', 'doe', 'john.doe@example.com'], $this->csv);
$this->assertContains(['jane', 'doe', 'jane.doe@example.com'], $this->csv);
}
public function testInsertNormalFile()
{
$csv = Writer::createFromPath(__DIR__.'/foo.csv', 'a+');
$csv->insertOne(['jane', 'doe', 'jane.doe@example.com']);
$this->assertContains(['jane', 'doe', 'jane.doe@example.com'], $csv);
}
/**
* @expectedException InvalidArgumentException
*/
public function testFailedSaveWithWrongType()
{
$this->csv->insertAll(new \StdClass());
}
/**
* @param $argument
* @param $expected
* @dataProvider dataToSave
*/
public function testSave($argument, $expected)
{
$this->csv->insertAll($argument);
$this->assertContains($expected, $this->csv);
}
public function dataToSave()
{
$multipleArray = [
['john', 'doe', 'john.doe@example.com'],
'jane,doe,jane.doe@example.com',
];
return [
'array' => [$multipleArray, $multipleArray[0]],
'iterator' => [new ArrayIterator($multipleArray), ['jane', 'doe', 'jane.doe@example.com']],
];
}
public function testGetReader()
{
$expected = [
['john', 'doe', 'john.doe@example.com'],
'john,doe,john.doe@example.com',
];
foreach ($expected as $row) {
$this->csv->insertOne($row);
}
$reader = $this->csv->newReader();
$this->assertSame(['john', 'doe', 'john.doe@example.com'], $reader->fetchOne(0));
}
public function testCustomNewline()
{
$csv = Writer::createFromFileObject(new SplTempFileObject());
$this->assertSame("\n", $csv->getNewline());
$csv->setNewline("\r\n");
$csv->insertOne(["jane", "doe"]);
$this->assertSame("jane,doe\r\n", (string) $csv);
}
public function testCustomNewlineFromCreateFromString()
{
$expected = "\r\n";
$raw = "john,doe,john.doe@example.com".PHP_EOL
."jane,doe,jane.doe@example.com".PHP_EOL;
$csv = Writer::createFromString($raw, $expected);
$this->assertSame($expected, $csv->getNewline());
}
public function testAddValidationRules()
{
$func = function (array $row) {
return $row;
};
$this->csv->addValidator($func, 'func1');
$this->csv->addValidator($func, 'func2');
$this->assertTrue($this->csv->hasValidator('func1'));
$this->csv->removeValidator('func1');
$this->assertTrue($this->csv->hasValidator('func2'));
$this->csv->clearValidators();
$this->assertFalse($this->csv->hasValidator('func2'));
}
public function testFormatterRules()
{
$func = function (array $row) {
return array_map('strtoupper', $row);
};
$this->csv->addFormatter($func);
$this->csv->addFormatter($func);
$this->assertTrue($this->csv->hasFormatter($func));
$this->csv->removeFormatter($func);
$this->assertTrue($this->csv->hasFormatter($func));
$this->csv->clearFormatters();
$this->assertFalse($this->csv->hasFormatter($func));
}
public function testConversionWithWriter()
{
$this->csv->insertAll([
['john', 'doe', 'john.doe@example.com'],
['jane', 'doe', 'jane.doe@example.com'],
['toto', 'le', 'herisson'],
]);
$this->assertStringStartsWith("<table", $this->csv->toHTML());
}
}
|