Skip to content

Instantly share code, notes, and snippets.

@sebdesign
Created October 2, 2017 19:47
Show Gist options
  • Save sebdesign/b2ea5b9dc1bedb3f9614fa8f44031ab8 to your computer and use it in GitHub Desktop.
Save sebdesign/b2ea5b9dc1bedb3f9614fa8f44031ab8 to your computer and use it in GitHub Desktop.
Testing ZlibOutputStream

Here are attached the test file and the OutputBuffer class.

$this->assertStringEqualsFile($file2, yield $buffer); fails with:

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-Binary String: 0x1f8b080006701b5900034bcbcf4f4a2c5248cecf2b49cd2be10200adcda2990f000000
+Binary String: 0x1f8b08000000000000034a03000000ffffca07000000ffffca07000000ffff4a02000000ffff4a04000000ffff2a02000000ffff5200000000ffff4a06000000ffffca07000000ffffca03000000ffff2a01000000ffff4a05000000ffffca03000000ffff2a01000000ffffe202000000ffff0300adcda2990f000000

$this->assertSame(\file_get_contents($file2), yield $buffer); fails with:

Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'pK��OJ,RH��+I�+��͢�'
+'J��������J��J��*��R��J��������*��J�����*������͢�'
<?php
namespace Amp\ByteStream;
use Amp\Promise;
class OutputBuffer implements OutputStream, Promise {
private $contents;
/** @var \Throwable Used to fail future writes on failure. */
private $error;
public function write(string $data): Promise {
$this->contents .= $data;
return $this;
}
public function end(string $finalData = ""): Promise {
$this->contents .= $finalData;
return $this;
}
public function onResolve(callable $onResolved) {
$onResolved($this->error, $this->contents);
}
}
<?php
namespace Amp\ByteStream\Test;
use Amp\ByteStream\IteratorStream;
use Amp\ByteStream\OutputBuffer;
use Amp\ByteStream\ResourceInputStream;
use Amp\ByteStream\ResourceOutputStream;
use Amp\ByteStream\StreamException;
use Amp\ByteStream\ZlibOutputStream;
use Amp\Loop;
use Amp\PHPUnit\TestCase;
use Amp\Producer;
class ZlibOutputStreamTest extends TestCase {
public function testWrite() {
Loop::run(function () {
$file1 = __DIR__ . "/fixtures/foobar.txt";
$file2 = __DIR__ . "/fixtures/foobar.txt.gz";
$fileStream = new IteratorStream(new Producer(function (callable $emit) use ($file1) {
$content = \file_get_contents($file1);
while ($content !== "") {
yield $emit($content[0]);
$content = \substr($content, 1);
}
}));
$buffer = new OutputBuffer();
$gzStream = new ZlibOutputStream($buffer, \ZLIB_ENCODING_GZIP);
while (($chunk = yield $fileStream->read()) !== null) {
yield $gzStream->write($chunk);
}
yield $gzStream->end();
$this->assertStringEqualsFile($file2, yield $buffer);
// $this->assertSame(\file_get_contents($file2), yield $buffer);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment