From 31ca1b7deec7bf88aff529f075ccda67956aae94 Mon Sep 17 00:00:00 2001 From: Stefano Arlandini Date: Mon, 27 Nov 2017 23:26:10 +0100 Subject: [PATCH 1/3] Do not use the arguments that were not specified intentionally by the user for the read/write filters --- src/Encoding/FilteredStream.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Encoding/FilteredStream.php b/src/Encoding/FilteredStream.php index 4b296ed..1f54169 100644 --- a/src/Encoding/FilteredStream.php +++ b/src/Encoding/FilteredStream.php @@ -57,8 +57,23 @@ 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); + switch (func_num_args()) { + case 1: + $this->readFilterCallback = Filter\fun($this->readFilter()); + $this->writeFilterCallback = Filter\fun($this->writeFilter()); + + break; + case 2: + $this->readFilterCallback = Filter\fun($this->readFilter(), $readFilterOptions); + $this->writeFilterCallback = Filter\fun($this->writeFilter()); + + break; + default: + $this->readFilterCallback = Filter\fun($this->readFilter(), $readFilterOptions); + $this->writeFilterCallback = Filter\fun($this->writeFilter(), $writeFilterOptions); + + break; + } if (null !== $writeFilterOptions) { @trigger_error('The $writeFilterOptions argument is deprecated since version 1.5 and will be removed in 2.0.', E_USER_DEPRECATED); From 6cfbf1b924d2ffcb8df997161a97de9b30421185 Mon Sep 17 00:00:00 2001 From: Stefano Arlandini Date: Fri, 1 Dec 2017 23:24:39 +0100 Subject: [PATCH 2/3] Do not rely on the number of arguments passed to the FilteredStream class to initialize the stream filter --- src/Encoding/FilteredStream.php | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/Encoding/FilteredStream.php b/src/Encoding/FilteredStream.php index 1f54169..d9b0fa8 100644 --- a/src/Encoding/FilteredStream.php +++ b/src/Encoding/FilteredStream.php @@ -57,26 +57,18 @@ abstract class FilteredStream implements StreamInterface */ public function __construct(StreamInterface $stream, $readFilterOptions = null, $writeFilterOptions = null) { - switch (func_num_args()) { - case 1: - $this->readFilterCallback = Filter\fun($this->readFilter()); - $this->writeFilterCallback = Filter\fun($this->writeFilter()); - - break; - case 2: - $this->readFilterCallback = Filter\fun($this->readFilter(), $readFilterOptions); - $this->writeFilterCallback = Filter\fun($this->writeFilter()); - - break; - default: - $this->readFilterCallback = Filter\fun($this->readFilter(), $readFilterOptions); - $this->writeFilterCallback = Filter\fun($this->writeFilter(), $writeFilterOptions); - - break; + 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; From 9087d195ba55c3baf2fd945376debd1967e8bc83 Mon Sep 17 00:00:00 2001 From: Stefano Arlandini Date: Wed, 14 Feb 2018 23:56:13 +0100 Subject: [PATCH 3/3] Add unit tests --- spec/Encoding/FilteredStreamStubSpec.php | 69 ++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 spec/Encoding/FilteredStreamStubSpec.php 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'; + } +}