Skip to content

Commit

Permalink
Improve strictness and interoperability
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Oct 18, 2015
1 parent 2f969fb commit 10e00e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,21 @@ function builtin($filter, $params = null)
throw new RuntimeException('Unable to access built-in filter: ' . $error['message']);
}

// append filter function which buffers internally
$buffer = '';
append($fp, function ($chunk) use (&$buffer) {
$buffer .= $chunk;

// always return empty string in order to skip actually writing to stream resource
return '';
}, STREAM_FILTER_WRITE);

return function ($chunk) use ($fp, &$buffer) {
// initialize buffer and invoke filters by attempting to write to stream
$buffer = '';

fwrite($fp, $chunk);

// buffer now contains everything the filter function returned
return $buffer;
};
}
Expand Down
17 changes: 12 additions & 5 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,20 @@ public function testBuiltInRot13()

public function testAppendBuiltInDechunk()
{
try {
$dechunk = StreamFilter\builtin('dechunk');
} catch (Exception $e) {
if (defined('HHVM_VERSION')) $this->markTestSkipped('Not supported on HHVM');
throw $e;
}

$stream = $this->createStream();

StreamFilter\append($stream, StreamFilter\builtin('dechunk'), STREAM_FILTER_WRITE);
StreamFilter\append($stream, $dechunk, STREAM_FILTER_WRITE);

fwrite($stream, "2\r\nhe\r\n");
fwrite($stream, "3\r\nllo\r\n");
fwrite($stream, "0\r\n");
fwrite($stream, "0\r\n\r\n");
rewind($stream);

$this->assertEquals('hello', stream_get_contents($stream));
Expand All @@ -196,26 +203,26 @@ public function testBuildInInvalid()
*/
public function testAppendInvalidStreamIsRuntimeError()
{
if (defined('HHVM_VERSION')) $this->markTestSkipped('Not supported on HHVM');
StreamFilter\append(false, function () { });
if (defined('HHVM_VERSION')) $this->markTestSkipped('Not supported on HHVM');
}

/**
* @expectedException RuntimeException
*/
public function testPrependInvalidStreamIsRuntimeError()
{
if (defined('HHVM_VERSION')) $this->markTestSkipped('Not supported on HHVM');
StreamFilter\prepend(false, function () { });
if (defined('HHVM_VERSION')) $this->markTestSkipped('Not supported on HHVM');
}

/**
* @expectedException RuntimeException
*/
public function testRemoveInvalidFilterIsRuntimeError()
{
if (defined('HHVM_VERSION')) $this->markTestSkipped('Not supported on HHVM');
StreamFilter\remove(false);
if (defined('HHVM_VERSION')) $this->markTestSkipped('Not supported on HHVM');
}

/**
Expand Down

0 comments on commit 10e00e6

Please sign in to comment.