Skip to content

Commit 5e187c0

Browse files
ste93cryNyholm
authored andcommitted
Do not pass arguments using default values to the Filter\fun function (#89)
* Do not use the arguments that were not specified intentionally by the user for the read/write filters * Do not rely on the number of arguments passed to the FilteredStream class to initialize the stream filter * Add unit tests
1 parent c9206f0 commit 5e187c0

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

Diff for: spec/Encoding/FilteredStreamStubSpec.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Http\Message\Encoding\FilteredStream;
6+
use PhpSpec\ObjectBehavior;
7+
use Psr\Http\Message\StreamInterface;
8+
9+
class FilteredStreamStubSpec extends ObjectBehavior
10+
{
11+
function it_throws_during_instantiation_with_invalid_read_filter_options(StreamInterface $stream)
12+
{
13+
$this->beAnInstanceOf('spec\Http\Message\Encoding\FilteredStreamStub');
14+
$this->beConstructedWith($stream, 'foo');
15+
$this->shouldThrow('RuntimeException')->duringInstantiation();
16+
}
17+
18+
function it_throws_during_instantiation_with_invalid_write_filter_options(StreamInterface $stream)
19+
{
20+
$this->beAnInstanceOf('spec\Http\Message\Encoding\FilteredStreamStub');
21+
$this->beConstructedWith($stream, null, 'foo');
22+
$this->shouldThrow('RuntimeException')->duringInstantiation();
23+
}
24+
25+
function let(StreamInterface $stream)
26+
{
27+
$this->beAnInstanceOf('spec\Http\Message\Encoding\FilteredStreamStub');
28+
$this->beConstructedWith($stream);
29+
}
30+
31+
function it_reads()
32+
{
33+
// "This is a test stream" | base64_encode
34+
$stream = new MemoryStream('This is a test stream');
35+
$this->beConstructedWith($stream);
36+
37+
$this->read(4)->shouldReturn('VGhp');
38+
}
39+
40+
function it_gets_content()
41+
{
42+
// "This is a test stream" | base64_encode
43+
$stream = new MemoryStream('This is a test stream');
44+
$this->beConstructedWith($stream);
45+
46+
$this->getContents()->shouldReturn('VGhpcyBpcyBhIHRlc3Qgc3RyZWFt');
47+
}
48+
49+
function it_does_not_know_the_content_size()
50+
{
51+
$stream = new MemoryStream(gzencode('This is a test stream'));
52+
$this->beConstructedWith($stream);
53+
54+
$this->getSize()->shouldBeNull();
55+
}
56+
}
57+
58+
class FilteredStreamStub extends FilteredStream
59+
{
60+
protected function readFilter()
61+
{
62+
return 'convert.base64-encode';
63+
}
64+
65+
protected function writeFilter()
66+
{
67+
return 'convert.base64-encode';
68+
}
69+
}

Diff for: src/Encoding/FilteredStream.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,18 @@ abstract class FilteredStream implements StreamInterface
5757
*/
5858
public function __construct(StreamInterface $stream, $readFilterOptions = null, $writeFilterOptions = null)
5959
{
60-
$this->readFilterCallback = Filter\fun($this->readFilter(), $readFilterOptions);
61-
$this->writeFilterCallback = Filter\fun($this->writeFilter(), $writeFilterOptions);
60+
if (null !== $readFilterOptions) {
61+
$this->readFilterCallback = Filter\fun($this->readFilter(), $readFilterOptions);
62+
} else {
63+
$this->readFilterCallback = Filter\fun($this->readFilter());
64+
}
6265

6366
if (null !== $writeFilterOptions) {
67+
$this->writeFilterCallback = Filter\fun($this->writeFilter(), $writeFilterOptions);
68+
6469
@trigger_error('The $writeFilterOptions argument is deprecated since version 1.5 and will be removed in 2.0.', E_USER_DEPRECATED);
70+
} else {
71+
$this->writeFilterCallback = Filter\fun($this->writeFilter());
6572
}
6673

6774
$this->stream = $stream;

0 commit comments

Comments
 (0)