Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Compressor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ final class Compressor extends TransformStream
/** @var ?resource */
private $context;

/** @var int */
private $flush;

/**
* @param int $encoding ZLIB_ENCODING_GZIP, ZLIB_ENCODING_RAW or ZLIB_ENCODING_DEFLATE
* @param int $level optional compression level
* @param int $flush optional flush mode (ZLIB_NO_FLUSH, ZLIB_SYNC_FLUSH, ZLIB_FULL_FLUSH, ZLIB_FINISH)
*/
public function __construct($encoding, $level = -1)
public function __construct($encoding, $level = -1, int $flush = ZLIB_NO_FLUSH)
{
$errstr = '';
set_error_handler(function ($_, $error) use (&$errstr) {
Expand All @@ -61,12 +65,17 @@ public function __construct($encoding, $level = -1)
throw new \InvalidArgumentException('Unable to initialize compressor' . $errstr); // @codeCoverageIgnore
}

if (!in_array($flush, [ZLIB_NO_FLUSH, ZLIB_SYNC_FLUSH, ZLIB_FULL_FLUSH, ZLIB_FINISH], true)) {
throw new \InvalidArgumentException('Argument #3 ($flush) must be one of ZLIB_NO_FLUSH, ZLIB_SYNC_FLUSH, ZLIB_FULL_FLUSH or ZLIB_FINISH');
}

$this->context = $context;
$this->flush = $flush;
}

protected function transformData($chunk)
{
$ret = deflate_add($this->context, $chunk, ZLIB_NO_FLUSH);
$ret = deflate_add($this->context, $chunk, $this->flush);

if ($ret !== '') {
$this->emit('data', [$ret]);
Expand Down
6 changes: 6 additions & 0 deletions tests/CompressorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ public function testCtorThrowsForInvalidEncodingAndUnsetsUsedErrorHandler()

$this->assertEquals($handler, $checkHandler);
}

public function testCtorThrowsForInvalidFlushMode()
{
$this->expectException(\InvalidArgumentException::class);
new Compressor(ZLIB_ENCODING_GZIP, -1, -1);
}
}
48 changes: 42 additions & 6 deletions tests/GzipCompressorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ class GzipCompressorTest extends TestCase
{
private $compressor;

/** @var string */
private $os;

/**
* @before
*/
public function setUpCompressor()
{
$this->compressor = new Compressor(ZLIB_ENCODING_GZIP);
$this->os = DIRECTORY_SEPARATOR !== '\\' ? "\x03" : (PHP_VERSION_ID >= 70200 ? "\x0a" : "\x0b"); // UNIX (0x03) or incorrect TOPS-20(0x0a) or NTFS(0x0b)
}

public function testCompressEmpty()
{
if (DIRECTORY_SEPARATOR === '\\') {
$this->markTestSkipped('Not supported on Windows');
}

$os = DIRECTORY_SEPARATOR === '\\' ? "\x0a" : "\x03"; // NTFS(0x0a) or UNIX (0x03)
$this->compressor->on('data', $this->expectCallableOnceWith("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00" . $os . "\x03\x00" . "\x00\x00\x00\x00\x00\x00\x00\x00"));
$this->compressor->on('data', $this->expectCallableOnceWith("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00" . $this->os . "\x03\x00" . "\x00\x00\x00\x00\x00\x00\x00\x00"));
$this->compressor->on('end', $this->expectCallableOnce());

$this->compressor->end();
Expand Down Expand Up @@ -58,4 +57,41 @@ public function testCompressBig()
// PHP < 5.4 does not support gzdecode(), so let's assert this the other way around…
$this->assertEquals(gzencode($data), $buffered);
}

public function testWriteWillOnlyFlushHeaderByDefaultToBufferDataBeforeFlushing()
{
$compressor = new Compressor(ZLIB_ENCODING_GZIP);

$compressor->on('data', $this->expectCallableOnceWith("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00" . $this->os));

$compressor->write('hello');
}

public function testWriteWithSyncFlushWillFlushHeaderWithFirstChunkImmediately()
{
$compressor = new Compressor(ZLIB_ENCODING_GZIP, -1, ZLIB_SYNC_FLUSH);

$compressor->on('data', $this->expectCallableOnceWith("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00" . $this->os . "\xca\x48\xcd\xc9\xc9\x07\x00\x00\x00\xff\xff"));

$compressor->write('hello');
}

public function testWriteWithFinishFlushWillFlushEntireGzipHeaderAndFooterWithFirstChunkImmediately()
{
$compressor = new Compressor(ZLIB_ENCODING_GZIP, -1, ZLIB_FINISH);

$compressor->on('data', $this->expectCallableOnceWith("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00" . $this->os . "\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36" . "\x05\x00\x00\x00"));

$compressor->write('hello');
}

public function testWriteAfterFinishFlushWillFlushEntireGzipWithSyncFlushWillFlushEntireGzipHeaderAndFooterAgainImmediately()
{
$compressor = new Compressor(ZLIB_ENCODING_GZIP, -1, ZLIB_FINISH);
$compressor->write('hello');

$compressor->on('data', $this->expectCallableOnceWith("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00" . $this->os . "\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36" . "\x05\x00\x00\x00"));

$compressor->write('hello');
}
}