mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Added test for 1 MB stream data
This commit is contained in:
101
tests/methods/ExampleStream.php
Normal file
101
tests/methods/ExampleStream.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\methods;
|
||||
|
||||
/**
|
||||
* This is a very simple implementation of a stream wrapper. All URLs are mapped
|
||||
* to just one buffer. It exists to prove that ParseCsv can read and write
|
||||
* streams.
|
||||
*
|
||||
* @see https://www.php.net/manual/en/class.streamwrapper.php
|
||||
*/
|
||||
class ExampleStream {
|
||||
|
||||
private static $position = 0;
|
||||
|
||||
private static $stream_content;
|
||||
|
||||
public function stream_open($uri, $mode) {
|
||||
if (strpos($mode, 'a') === false) {
|
||||
self::$position = 0;
|
||||
}
|
||||
if (strpos($mode, 'w') !== false) {
|
||||
self::$stream_content = '';
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function stream_read($count) {
|
||||
$ret = substr(self::$stream_content, self::$position, $count);
|
||||
self::$position += strlen($ret);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function stream_write($data) {
|
||||
$left = substr(self::$stream_content, 0, self::$position);
|
||||
$right = substr(self::$stream_content, self::$position + strlen($data));
|
||||
self::$stream_content = $left . $data . $right;
|
||||
self::$position += strlen($data);
|
||||
return strlen($data);
|
||||
}
|
||||
|
||||
public function stream_stat() {
|
||||
return ['size' => strlen(self::$stream_content)];
|
||||
}
|
||||
|
||||
|
||||
public function stream_tell() {
|
||||
return self::$position;
|
||||
}
|
||||
|
||||
public function stream_eof() {
|
||||
return self::$position >= strlen(self::$stream_content);
|
||||
}
|
||||
|
||||
public function url_stat() {
|
||||
return ['size' => strlen(self::$stream_content)];
|
||||
}
|
||||
|
||||
public function stream_seek($offset, $whence) {
|
||||
switch ($whence) {
|
||||
case SEEK_SET:
|
||||
if ($offset < strlen(self::$stream_content) && $offset >= 0) {
|
||||
self::$position = $offset;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case SEEK_CUR:
|
||||
if ($offset >= 0) {
|
||||
self::$position += $offset;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case SEEK_END:
|
||||
if (strlen(self::$stream_content) + $offset >= 0) {
|
||||
self::$position = strlen(self::$stream_content) + $offset;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function stream_lock($operation) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function stream_metadata() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
71
tests/methods/StreamTest.php
Normal file
71
tests/methods/StreamTest.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\methods;
|
||||
|
||||
use ParseCsv\Csv;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Writes roughly 1 MB of data. This is useful because of a limit of 8 KB
|
||||
* encountered with stream operations in certain PHP versions.
|
||||
*/
|
||||
class StreamTest extends TestCase {
|
||||
|
||||
public function setUp() {
|
||||
static $done;
|
||||
if ($done) {
|
||||
// Make sure we register the stream just once - or PHP will scream.
|
||||
return;
|
||||
}
|
||||
|
||||
stream_wrapper_register("example", ExampleStream::class)
|
||||
or die("Failed to register protocol");
|
||||
$done = 1;
|
||||
}
|
||||
|
||||
public function testReadStream() {
|
||||
$csv = new Csv();
|
||||
|
||||
// Write data to our stream:
|
||||
$filename = 'example://tmp';
|
||||
copy(__DIR__ . '/fixtures/datatype.csv', $filename);
|
||||
$many_dots = str_repeat('.', 1000 * 1000) . ";;;;;\n";
|
||||
file_put_contents($filename, $many_dots, FILE_APPEND);
|
||||
|
||||
self::assertSame(';', $csv->auto(file_get_contents($filename)));
|
||||
self::assertCount(4, $csv->data);
|
||||
self::assertCount(6, reset($csv->data));
|
||||
}
|
||||
|
||||
public function testWriteStream() {
|
||||
$csv = new Csv();
|
||||
$csv->linefeed = "\n";
|
||||
$many_dots = str_repeat('.', 1000 * 1000);
|
||||
$csv->data = [
|
||||
[
|
||||
'Name' => 'Rudolf',
|
||||
'Question' => 'Which color is his nose?',
|
||||
],
|
||||
[
|
||||
'Name' => 'Sponge Bob',
|
||||
'Question' => 'Which shape are his pants?',
|
||||
],
|
||||
[
|
||||
'Name' => $many_dots,
|
||||
'Question' => 'Can you count one million dots?',
|
||||
],
|
||||
];
|
||||
|
||||
// Just export the first column, but with a new name
|
||||
$csv->titles = ['Name' => 'Character'];
|
||||
|
||||
// Write data to our stream:
|
||||
$filename = 'example://data';
|
||||
copy(__DIR__ . '/fixtures/datatype.csv', $filename);
|
||||
|
||||
self::assertSame(true, $csv->save($filename));
|
||||
$expected = "Character\nRudolf\nSponge Bob\n";
|
||||
$expected .= $many_dots . "\n";
|
||||
self::assertSame($expected, file_get_contents($filename));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user