Skip to content
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

Encoding #5

Merged
merged 7 commits into from
Dec 24, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
"require": {
"php": ">=5.4",
"psr/http-message": "^1.0",
"php-http/message-factory": "^1.0"
"php-http/message-factory": "^1.0",
"clue/stream-filter": "^1.3"
},
"require-dev": {
"zendframework/zend-diactoros": "^1.0",
"guzzlehttp/psr7": "^1.0",
"ext-zlib": "*",
"phpspec/phpspec": "^2.4",
"henrikbjorn/phpspec-code-coverage" : "^1.0"
},
"suggest": {
"zendframework/zend-diactoros": "Used with Diactoros Factories",
"guzzlehttp/psr7": "Used with Guzzle PSR-7 Factories"
"guzzlehttp/psr7": "Used with Guzzle PSR-7 Factories",
"ext-zlib": "Used with compressor/decompressor streams"
},
"autoload": {
"psr-4": {
Expand Down
154 changes: 154 additions & 0 deletions spec/Decorator/StreamDecoratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace spec\Http\Message\Decorator;

use Http\Message\Decorator\StreamDecorator;
use Psr\Http\Message\StreamInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class StreamDecoratorSpec extends ObjectBehavior
{
function let(StreamInterface $stream)
{
$this->beAnInstanceOf('spec\Http\Message\Decorator\StreamDecoratorStub', [$stream]);
}

function it_is_initializable()
{
$this->shouldHaveType('spec\Http\Message\Decorator\StreamDecoratorStub');
}

function it_is_a_stream_decorator()
{
$this->shouldUseTrait('Http\Message\Decorator\StreamDecorator');
}

function it_casts_the_stream_to_string(StreamInterface $stream)
{
$stream->__toString()->willReturn('body');

$this->__toString()->shouldReturn('body');
}

function it_closes_the_stream(StreamInterface $stream)
{
$stream->close()->shouldBeCalled();

$this->close();
}

function it_detaches_the_stream(StreamInterface $stream)
{
$stream->detach()->willReturn('detached');

$this->detach()->shouldReturn('detached');
}

function it_returns_the_size_of_the_stream(StreamInterface $stream)
{
$stream->getSize()->willReturn(1234);

$this->getSize()->shouldReturn(1234);
}

function it_returns_the_current_position_of_the_stream(StreamInterface $stream)
{
$stream->tell()->willReturn(0);

$this->tell()->shouldReturn(0);
}

function it_checks_whether_the_stream_is_eof(StreamInterface $stream)
{
$stream->eof()->willReturn(false);

$this->eof()->shouldReturn(false);
}

function it_checks_whether_the_stream_is_seekable(StreamInterface $stream)
{
$stream->isSeekable()->willReturn(true);

$this->isSeekable()->shouldReturn(true);
}

function it_seeks_the_current_position_of_the_stream(StreamInterface $stream)
{
$stream->seek(0, SEEK_SET)->shouldBeCalled();

$this->seek(0);
}

function it_rewinds_to_the_beginning_of_the_stream(StreamInterface $stream)
{
$stream->rewind()->shouldBeCalled();

$this->rewind();
}

function it_checks_whether_the_stream_is_writable(StreamInterface $stream)
{
$stream->isWritable()->willReturn(true);

$this->isWritable()->shouldReturn(true);
}

function it_writes_to_the_stream(StreamInterface $stream)
{
$stream->write('body')->shouldBeCalled();

$this->write('body');
}

function it_checks_whether_the_stream_is_readable(StreamInterface $stream)
{
$stream->isReadable()->willReturn(true);

$this->isReadable()->shouldReturn(true);
}

function it_reads_from_the_stream(StreamInterface $stream)
{
$stream->read(4)->willReturn('body');

$this->read(4)->shouldReturn('body');
}

function it_returns_the_contents_of_the_stream(StreamInterface $stream)
{
$stream->getContents()->willReturn('body');

$this->getContents()->shouldReturn('body');
}

function it_returns_metadata_of_the_stream(StreamInterface $stream)
{
$stream->getMetadata(null)->willReturn(['key' => 'value']);
$stream->getMetadata('key')->willReturn('value');
$stream->getMetadata('key2')->willReturn(null);

$this->getMetadata()->shouldReturn(['key' => 'value']);
$this->getMetadata('key')->shouldReturn('value');
$this->getMetadata('key2')->shouldReturn(null);
}

function getMatchers()
{
return [
'useTrait' => function ($subject, $trait) {
return class_uses($subject, $trait);
}
];
}
}

class StreamDecoratorStub implements StreamInterface
{
use StreamDecorator;

public function __construct(StreamInterface $stream)
{
$this->stream = $stream;
}
}
42 changes: 42 additions & 0 deletions spec/Encoding/ChunkStreamSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace spec\Http\Message\Encoding;

use Psr\Http\Message\StreamInterface;
use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;

class ChunkStreamSpec extends ObjectBehavior
{
use StreamBehavior;

function let(StreamInterface $stream)
{
if (defined('HHVM_VERSION')) {
throw new SkippingException('Skipping test as there is no dechunk filter on hhvm');
}

$this->beConstructedWith($stream);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\Encoding\ChunkStream');
}

function it_chunks_content()
{
$stream = new MemoryStream('This is a stream');
$this->beConstructedWith($stream, 6);

$this->getContents()->shouldReturn("10\r\nThis is a stream\r\n0\r\n\r\n");
}

function it_chunks_in_multiple()
{
$stream = new MemoryStream('This is a stream', 6);
$this->beConstructedWith($stream, 6);

$this->getContents()->shouldReturn("6\r\nThis i\r\n6\r\ns a st\r\n4\r\nream\r\n0\r\n\r\n");
}
}
44 changes: 44 additions & 0 deletions spec/Encoding/CompressStreamSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace spec\Http\Message\Encoding;

use Psr\Http\Message\StreamInterface;
use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;

class CompressStreamSpec extends ObjectBehavior
{
use StreamBehavior, ZlibStreamBehavior;

function let(StreamInterface $stream)
{
if (defined('HHVM_VERSION')) {
throw new SkippingException('Skipping test as zlib is not working on hhvm');
}

$this->beConstructedWith($stream);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\Encoding\CompressStream');
}

function it_reads()
{
$stream = new MemoryStream('This is a test stream');
$this->beConstructedWith($stream);

$stream->rewind();
$this->read(4)->shouldReturn(substr(gzcompress('This is a test stream'), 0, 4));
}

function it_gets_content()
{
$stream = new MemoryStream('This is a test stream');
$this->beConstructedWith($stream);

$stream->rewind();
$this->getContents()->shouldReturn(gzcompress('This is a test stream'));
}
}
43 changes: 43 additions & 0 deletions spec/Encoding/DechunkStreamSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace spec\Http\Message\Encoding;

use Psr\Http\Message\StreamInterface;
use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;

class DechunkStreamSpec extends ObjectBehavior
{
use StreamBehavior;

function let(StreamInterface $stream)
{
if (defined('HHVM_VERSION')) {
throw new SkippingException('Skipping test as there is no dechunk filter on hhvm');
}

$this->beConstructedWith($stream);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\Encoding\DechunkStream');
}

function it_reads()
{
$stream = new MemoryStream("4\r\ntest\r\n4\r\ntest\r\n0\r\n\r\n\0");
$this->beConstructedWith($stream);

$this->read(6)->shouldReturn('testte');
$this->read(6)->shouldReturn('st');
}

function it_gets_content()
{
$stream = new MemoryStream("4\r\ntest\r\n0\r\n\r\n\0");
$this->beConstructedWith($stream);

$this->getContents()->shouldReturn('test');
}
}
44 changes: 44 additions & 0 deletions spec/Encoding/DecompressStreamSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace spec\Http\Message\Encoding;

use Psr\Http\Message\StreamInterface;
use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;

class DecompressStreamSpec extends ObjectBehavior
{
use StreamBehavior, ZlibStreamBehavior;

function let(StreamInterface $stream)
{
if (defined('HHVM_VERSION')) {
throw new SkippingException('Skipping test as zlib is not working on hhvm');
}

$this->beConstructedWith($stream);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\Encoding\DecompressStream');
}

function it_reads()
{
// "This is a test stream" | deflate
$stream = new MemoryStream(gzcompress('This is a test stream'));
$this->beConstructedWith($stream);

$this->read(4)->shouldReturn('This');
}

function it_gets_content()
{
// "This is a test stream" | deflate
$stream = new MemoryStream(gzcompress('This is a test stream'));
$this->beConstructedWith($stream);

$this->getContents()->shouldReturn('This is a test stream');
}
}
39 changes: 39 additions & 0 deletions spec/Encoding/DeflateStreamSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace spec\Http\Message\Encoding;

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

class DeflateStreamSpec extends ObjectBehavior
{
use StreamBehavior;

function let(StreamInterface $stream)
{
$this->beConstructedWith($stream);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\Encoding\DeflateStream');
}

function it_reads()
{
$stream = new MemoryStream('This is a test stream');
$this->beConstructedWith($stream);

$stream->rewind();
$this->read(4)->shouldReturn(substr(gzdeflate('This is a test stream'),0, 4));
}

function it_gets_content()
{
$stream = new MemoryStream('This is a test stream');
$this->beConstructedWith($stream);

$stream->rewind();
$this->getContents()->shouldReturn(gzdeflate('This is a test stream'));
}
}