2017-05-17 08:17:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\ByteStream\Test;
|
|
|
|
|
|
|
|
use Amp\ByteStream\InMemoryStream;
|
|
|
|
use Amp\ByteStream\IteratorStream;
|
2017-05-22 14:44:36 +02:00
|
|
|
use Amp\ByteStream\StreamException;
|
2017-05-17 08:17:15 +02:00
|
|
|
use Amp\ByteStream\ZlibInputStream;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\PHPUnit\TestCase;
|
|
|
|
use Amp\Producer;
|
|
|
|
|
2018-09-21 22:45:13 +02:00
|
|
|
class ZlibInputStreamTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testRead()
|
|
|
|
{
|
2017-05-17 08:17:15 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$file1 = __DIR__ . "/fixtures/foobar.txt";
|
|
|
|
$file2 = __DIR__ . "/fixtures/foobar.txt.gz";
|
|
|
|
|
|
|
|
$stream = new IteratorStream(new Producer(function (callable $emit) use ($file2) {
|
|
|
|
$content = \file_get_contents($file2);
|
|
|
|
|
|
|
|
while ($content !== "") {
|
|
|
|
yield $emit($content[0]);
|
|
|
|
$content = \substr($content, 1);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
$gzStream = new ZlibInputStream($stream, \ZLIB_ENCODING_GZIP);
|
|
|
|
|
|
|
|
$buffer = "";
|
|
|
|
while (($chunk = yield $gzStream->read()) !== null) {
|
|
|
|
$buffer .= $chunk;
|
|
|
|
}
|
|
|
|
|
2018-04-04 07:33:09 +02:00
|
|
|
$expected = \str_replace("\r\n", "\n", \file_get_contents($file1));
|
|
|
|
$this->assertSame($expected, $buffer);
|
2017-05-17 08:17:15 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-21 22:45:13 +02:00
|
|
|
public function testGetEncoding()
|
|
|
|
{
|
2017-10-06 22:25:55 +02:00
|
|
|
$gzStream = new ZlibInputStream(new InMemoryStream(""), \ZLIB_ENCODING_GZIP);
|
|
|
|
|
|
|
|
$this->assertSame(\ZLIB_ENCODING_GZIP, $gzStream->getEncoding());
|
|
|
|
}
|
|
|
|
|
2018-09-21 22:45:13 +02:00
|
|
|
public function testInvalidEncoding()
|
|
|
|
{
|
2017-05-22 14:44:36 +02:00
|
|
|
$this->expectException(StreamException::class);
|
2017-05-17 08:17:15 +02:00
|
|
|
|
|
|
|
new ZlibInputStream(new InMemoryStream(""), 1337);
|
|
|
|
}
|
2017-10-06 22:25:55 +02:00
|
|
|
|
2018-09-21 22:45:13 +02:00
|
|
|
public function testGetOptions()
|
|
|
|
{
|
2017-10-06 22:25:55 +02:00
|
|
|
$options = [
|
|
|
|
"level" => -1,
|
|
|
|
"memory" => 8,
|
|
|
|
"window" => 15,
|
|
|
|
"strategy" => \ZLIB_DEFAULT_STRATEGY,
|
|
|
|
];
|
|
|
|
|
|
|
|
$gzStream = new ZlibInputStream(new InMemoryStream(""), \ZLIB_ENCODING_GZIP, $options);
|
|
|
|
|
|
|
|
$this->assertSame($options, $gzStream->getOptions());
|
|
|
|
}
|
|
|
|
|
2018-09-21 22:45:13 +02:00
|
|
|
public function testInvalidStream()
|
|
|
|
{
|
2017-10-06 22:25:55 +02:00
|
|
|
$this->expectException(StreamException::class);
|
|
|
|
|
|
|
|
Loop::run(function () {
|
|
|
|
$gzStream = new ZlibInputStream(new InMemoryStream("Invalid"), \ZLIB_ENCODING_GZIP);
|
|
|
|
|
|
|
|
yield $gzStream->read();
|
|
|
|
});
|
|
|
|
}
|
2017-05-17 08:17:15 +02:00
|
|
|
}
|