-
-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathStreamTest.php
More file actions
313 lines (263 loc) · 8.5 KB
/
Copy pathStreamTest.php
File metadata and controls
313 lines (263 loc) · 8.5 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* League.Csv (https://csv.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace League\Csv;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use SplFileObject;
use TypeError;
use function feof;
use function fopen;
use function fputcsv;
use function fread;
use function fseek;
use function ftell;
use function fwrite;
use function in_array;
use function stream_context_create;
use function stream_context_get_options;
use function stream_get_wrappers;
use function stream_wrapper_register;
use function stream_wrapper_unregister;
use const STREAM_FILTER_READ;
#[Group('csv')]
final class StreamTest extends TestCase
{
protected function setUp(): void
{
stream_wrapper_register(StreamWrapper::PROTOCOL, StreamWrapper::class);
}
protected function tearDown(): void
{
stream_wrapper_unregister(StreamWrapper::PROTOCOL);
}
public function testCloningIsForbidden(): void
{
$this->expectException(UnavailableStream::class);
clone Stream::from(STDOUT);
}
public function testCreateStreamWithInvalidParameter(): void
{
$this->expectException(TypeError::class);
Stream::createFromResource(__DIR__.'/../test_files/foo.csv'); /* @phpstan-ignore-line */
}
public function testCreateStreamWithWrongResourceType(): void
{
$this->expectException(TypeError::class);
/** @var resource $resource */
$resource = stream_filter_append(STDOUT, 'string.rot13', STREAM_FILTER_WRITE);
Stream::from($resource);
}
public function testCreateStreamFromPath(): void
{
$path = 'no/such/file.csv';
$this->expectException(UnavailableStream::class);
$this->expectExceptionMessage('`'.$path.'`: failed to open stream: No such file or directory');
Stream::from($path);
}
public function testCreateStreamFromPathWithContext(): void
{
/** @var resource $fp */
$fp = fopen('php://temp', 'r+');
$expected = [
['john', 'doe', 'john.doe@example.com'],
['john', 'doe', 'john.doe@example.com'],
];
foreach ($expected as $row) {
fputcsv($fp, $row, escape: '');
}
$stream = Stream::from(
StreamWrapper::PROTOCOL.'://stream',
'r+',
stream_context_create([StreamWrapper::PROTOCOL => ['stream' => $fp]])
);
$stream->setFlags(SplFileObject::READ_AHEAD | SplFileObject::READ_CSV);
$stream->rewind();
self::assertIsArray($stream->current());
}
#[DataProvider('fputcsvProvider')]
public function testfputcsv(string $delimiter, string $enclosure, string $escape): void
{
$this->expectException(InvalidArgument::class);
$stream = Stream::from(STDOUT);
$stream->fputcsv(['john', 'doe', 'john.doe@example.com'], $delimiter, $enclosure, $escape);
}
public static function fputcsvProvider(): array
{
return [
'wrong delimiter' => ['toto', '"', '\\'],
'wrong enclosure' => [',', 'é', '\\'],
'wrong escape' => [',', '"', 'à'],
];
}
public function testVarDump(): void
{
$stream = Stream::from(STDOUT);
$debugInfo = $stream->__debugInfo();
self::assertArrayHasKey('delimiter', $debugInfo);
self::assertArrayHasKey('enclosure', $debugInfo);
self::assertArrayHasKey('escape', $debugInfo);
self::assertArrayHasKey('stream_filters', $debugInfo);
}
public function testSeekThrowsException(): void
{
$this->expectException(InvalidArgument::class);
$stream = Stream::from(STDOUT);
$stream->seek(-1);
}
public function testFSeekThrowsExceptionOnNonSeakableResource(): void
{
$this->expectException(UnavailableFeature::class);
$stream = Stream::from(STDOUT);
$stream->fputcsv(['foo', 'bar'], escape: '');
$stream->fseek(-1);
}
public function testSeek(): void
{
$doc = Stream::from(__DIR__.'/../test_files/prenoms.csv');
$doc->setCsvControl(';');
$doc->setFlags(SplFileObject::READ_CSV);
$doc->seek(1);
self::assertSame(['Aaron', '55', 'M', '2004'], $doc->current());
}
public function testSeekToPositionZero(): void
{
$doc = Stream::fromString();
$doc->seek(0);
self::assertSame(0, $doc->key());
}
public function testRewindThrowsException(): void
{
$this->expectException(UnavailableFeature::class);
/** @var resource $filePointer */
$filePointer = fopen('php://stdout', 'w');
$stream = Stream::from($filePointer);
$stream->rewind();
}
public function testCreateStreamWithNonSeekableStream(): void
{
$this->expectException(UnavailableFeature::class);
/** @var resource $filePointer */
$filePointer = fopen('php://stdout', 'w');
$stream = Stream::from($filePointer);
$stream->seek(3);
}
public function testCsvControl(): void
{
$doc = Stream::fromString('foo,bar');
self::assertSame([',', '"', '\\'], $doc->getCsvControl());
$expected = [';', '|', '"'];
$doc->setCsvControl(...$expected);
self::assertSame($expected, $doc->getCsvControl());
$this->expectException(InvalidArgument::class);
$doc->setCsvControl(...['foo']);
}
public function testCsvControlAcceptsEmptyEscapeString(): void
{
$doc = Stream::fromString();
$expected = [';', '|', ''];
$doc->setCsvControl(...$expected);
self::assertSame($expected, $doc->getCsvControl());
}
public function testAppendStreamFilterThrowsException(): void
{
$filtername = 'foo.bar';
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('unable to locate filter `'.$filtername.'`');
$stream = Stream::from('php://temp', 'r+');
$stream->appendFilter($filtername, STREAM_FILTER_READ);
}
public function testIterateOverLines(): void
{
$text = <<<TEXT
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Duis nec sapien felis, ac sodales nisl.
Nulla vitae magna vitae purus aliquet consequat.
TEXT;
$newText = '';
$file = Stream::fromString($text);
$file->setMaxLineLen(20);
foreach ($file as $line) {
$newText .= $line."\n";
}
self::assertStringContainsString('Lorem ipsum dolor s', $newText);
self::assertSame(20, $file->getMaxLineLen());
}
}
final class StreamWrapper
{
public const PROTOCOL = 'leaguetest';
/**
* @var resource
*/
public $context;
/**
* @var resource
*/
private $stream;
public static function register(): void
{
if (!in_array(self::PROTOCOL, stream_get_wrappers(), true)) {
stream_wrapper_register(self::PROTOCOL, __CLASS__);
}
}
public function stream_open(string $path, string $mode, int $options, ?string &$opened_path = null): bool
{
$options = stream_context_get_options($this->context);
if (!isset($options[self::PROTOCOL]['stream'])) {
return false;
}
$this->stream = $options[self::PROTOCOL]['stream'];
return true;
}
/**
* @param int<1, max> $count
*/
public function stream_read(int $count): string|false
{
return fread($this->stream, $count);
}
public function stream_write(string $data): int|false
{
return fwrite($this->stream, $data);
}
public function stream_tell(): int|false
{
return ftell($this->stream);
}
public function stream_eof(): bool
{
return feof($this->stream);
}
public function stream_seek(int $offset, int $whence): bool
{
fseek($this->stream, $whence);
return true;
}
public function stream_stat(): array
{
return [
'dev' => 0,
'ino' => 0,
'mode' => 33206,
'nlink' => 0,
'uid' => 0,
'gid' => 0,
'rdev' => 0,
'size' => 0,
'atime' => 0,
'mtime' => 0,
'ctime' => 0,
'blksize' => 0,
'blocks' => 0,
];
}
}