diff --git a/spec/Encoding/FilteredStreamStubSpec.php b/spec/Encoding/FilteredStreamStubSpec.php new file mode 100644 index 0000000..6e6692e --- /dev/null +++ b/spec/Encoding/FilteredStreamStubSpec.php @@ -0,0 +1,69 @@ +beAnInstanceOf('spec\Http\Message\Encoding\FilteredStreamStub'); + $this->beConstructedWith($stream, 'foo'); + $this->shouldThrow('RuntimeException')->duringInstantiation(); + } + + function it_throws_during_instantiation_with_invalid_write_filter_options(StreamInterface $stream) + { + $this->beAnInstanceOf('spec\Http\Message\Encoding\FilteredStreamStub'); + $this->beConstructedWith($stream, null, 'foo'); + $this->shouldThrow('RuntimeException')->duringInstantiation(); + } + + function let(StreamInterface $stream) + { + $this->beAnInstanceOf('spec\Http\Message\Encoding\FilteredStreamStub'); + $this->beConstructedWith($stream); + } + + function it_reads() + { + // "This is a test stream" | base64_encode + $stream = new MemoryStream('This is a test stream'); + $this->beConstructedWith($stream); + + $this->read(4)->shouldReturn('VGhp'); + } + + function it_gets_content() + { + // "This is a test stream" | base64_encode + $stream = new MemoryStream('This is a test stream'); + $this->beConstructedWith($stream); + + $this->getContents()->shouldReturn('VGhpcyBpcyBhIHRlc3Qgc3RyZWFt'); + } + + function it_does_not_know_the_content_size() + { + $stream = new MemoryStream(gzencode('This is a test stream')); + $this->beConstructedWith($stream); + + $this->getSize()->shouldBeNull(); + } +} + +class FilteredStreamStub extends FilteredStream +{ + protected function readFilter() + { + return 'convert.base64-encode'; + } + + protected function writeFilter() + { + return 'convert.base64-encode'; + } +} diff --git a/src/Encoding/FilteredStream.php b/src/Encoding/FilteredStream.php index 4b296ed..d9b0fa8 100644 --- a/src/Encoding/FilteredStream.php +++ b/src/Encoding/FilteredStream.php @@ -57,11 +57,18 @@ abstract class FilteredStream implements StreamInterface */ public function __construct(StreamInterface $stream, $readFilterOptions = null, $writeFilterOptions = null) { - $this->readFilterCallback = Filter\fun($this->readFilter(), $readFilterOptions); - $this->writeFilterCallback = Filter\fun($this->writeFilter(), $writeFilterOptions); + if (null !== $readFilterOptions) { + $this->readFilterCallback = Filter\fun($this->readFilter(), $readFilterOptions); + } else { + $this->readFilterCallback = Filter\fun($this->readFilter()); + } if (null !== $writeFilterOptions) { + $this->writeFilterCallback = Filter\fun($this->writeFilter(), $writeFilterOptions); + @trigger_error('The $writeFilterOptions argument is deprecated since version 1.5 and will be removed in 2.0.', E_USER_DEPRECATED); + } else { + $this->writeFilterCallback = Filter\fun($this->writeFilter()); } $this->stream = $stream;