1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-11-30 04:19:23 +01:00
byte-stream/test/ZlibInputStreamTest.php

81 lines
2.2 KiB
PHP
Raw Permalink Normal View History

2017-05-17 08:17:15 +02:00
<?php
namespace Amp\ByteStream\Test;
use Amp\ByteStream\InMemoryStream;
use Amp\ByteStream\IteratorStream;
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;
}
$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()
{
$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
}