Skip to content

Do not pass arguments using default values to the Filter\fun function #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 9, 2018
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
69 changes: 69 additions & 0 deletions spec/Encoding/FilteredStreamStubSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace spec\Http\Message\Encoding;

use Http\Message\Encoding\FilteredStream;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\StreamInterface;

class FilteredStreamStubSpec extends ObjectBehavior
{
function it_throws_during_instantiation_with_invalid_read_filter_options(StreamInterface $stream)
{
$this->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';
}
}
11 changes: 9 additions & 2 deletions src/Encoding/FilteredStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down