-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCsvWriterTest.php
More file actions
175 lines (146 loc) · 5.23 KB
/
Copy pathCsvWriterTest.php
File metadata and controls
175 lines (146 loc) · 5.23 KB
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
171
172
173
174
175
<?php
namespace Port\Tests\Csv;
use Port\Csv\CsvWriter;
use Port\Test\StreamWriterTest;
class CsvWriterTest extends StreamWriterTest
{
public function testWriteItem()
{
$writer = new CsvWriter(';', '"', $this->getStream());
$writer->prepare();
$writer->writeItem(array('first', 'last'));
$writer->writeItem(array(
'first' => 'James',
'last' => 'Bond'
));
$writer->writeItem(array(
'first' => '',
'last' => 'Dr. No'
));
$this->assertContentsEquals(
"first;last\nJames;Bond\n;\"Dr. No\"\n",
$writer
);
$writer->finish();
}
public function testWriteUtf8Item()
{
$writer = new CsvWriter(';', '"', $this->getStream(), true);
$writer->prepare();
$writer->writeItem(array('Précédent', 'Suivant'));
$this->assertContentsEquals(
chr(0xEF) . chr(0xBB) . chr(0xBF) . "Précédent;Suivant\n",
$writer
);
$writer->finish();
}
/**
* Test that column names not prepended to first row
* if CsvWriter's 5-th parameter not given
*
* @author Igor Mukhin <igor.mukhin@gmail.com>
*/
public function testHeaderNotPrependedByDefault()
{
$writer = new CsvWriter(';', '"', $this->getStream(), false);
$writer->prepare();
$writer->writeItem(array(
'col 1 name'=>'col 1 value',
'col 2 name'=>'col 2 value',
'col 3 name'=>'col 3 value'
));
# Values should be at first line
$this->assertContentsEquals(
"\"col 1 value\";\"col 2 value\";\"col 3 value\"\n",
$writer
);
$writer->finish();
}
/**
* Test that column names prepended at first row
* and values have been written at second line
* if CsvWriter's 5-th parameter set to true
*
* @author Igor Mukhin <igor.mukhin@gmail.com>
*/
public function testHeaderPrependedWhenOptionSetToTrue()
{
$writer = new CsvWriter(';', '"', $this->getStream(), false, true);
$writer->prepare();
$writer->writeItem(array(
'col 1 name'=>'col 1 value',
'col 2 name'=>'col 2 value',
'col 3 name'=>'col 3 value'
));
# Column names should be at second line
# Values should be at second line
$this->assertContentsEquals(
"\"col 1 name\";\"col 2 name\";\"col 3 name\"\n" .
"\"col 1 value\";\"col 2 value\";\"col 3 value\"\n",
$writer
);
$writer->finish();
}
/**
* Proves escape is applied: default '\\' and empty-string escape produce different CSV
* for a field that contains a backslash before a quote.
*
* Also exercises that fputcsv receives an explicit $escape argument, which is required
* on PHP 8.4+ (omitting it is deprecated; phpunit.xml converts deprecations to exceptions).
*/
public function testEscapeParameterAffectsOutput()
{
$item = array('a\\"b');
$defaultWriter = new CsvWriter(',', '"', fopen('php://temp', 'r+'));
$defaultWriter->setCloseStreamOnFinish(false);
$defaultWriter->prepare();
$defaultWriter->writeItem($item);
$defaultOutput = $this->readWriterContents($defaultWriter);
$emptyEscapeWriter = new CsvWriter(',', '"', fopen('php://temp', 'r+'), false, false, '');
$emptyEscapeWriter->setCloseStreamOnFinish(false);
$emptyEscapeWriter->prepare();
$emptyEscapeWriter->writeItem($item);
$emptyOutput = $this->readWriterContents($emptyEscapeWriter);
$this->assertNotSame(
$defaultOutput,
$emptyOutput,
'Custom escape should change CSV encoding of fields containing backslash/quote'
);
$this->assertSame($this->fputcsvString($item, '\\'), $defaultOutput);
$this->assertSame($this->fputcsvString($item, ''), $emptyOutput);
fclose($defaultWriter->getStream());
fclose($emptyEscapeWriter->getStream());
}
/**
* Empty escape is usable for modern "no escape" CSV and does not trigger PHP 8.4+
* fputcsv deprecation (which phpunit.xml converts to exceptions).
*/
public function testEmptyEscapeWritesWithoutDeprecation()
{
$writer = new CsvWriter(',', '"', $this->getStream(), false, false, '');
$writer->prepare();
$writer->writeItem(array('hello', 'world'));
$writer->writeItem(array('say "hi"', 'path\\to'));
$this->assertContentsEquals(
$this->fputcsvString(array('hello', 'world'), '') .
$this->fputcsvString(array('say "hi"', 'path\\to'), ''),
$writer
);
$writer->finish();
}
private function readWriterContents(CsvWriter $writer)
{
$stream = $writer->getStream();
rewind($stream);
return stream_get_contents($stream);
}
private function fputcsvString(array $fields, $escape)
{
$stream = fopen('php://temp', 'r+');
fputcsv($stream, $fields, ',', '"', $escape);
rewind($stream);
$contents = stream_get_contents($stream);
fclose($stream);
return $contents;
}
}