-
Notifications
You must be signed in to change notification settings - Fork 39
Encoding #5
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
Changes from 4 commits
a301d77
94cc919
946666d
c7fcd53
7f7dd42
6478a77
827ed4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
"require-dev": { | ||
"zendframework/zend-diactoros": "^1.0", | ||
"guzzlehttp/psr7": "^1.0", | ||
"ext-zlib": "*", | ||
"clue/stream-filter": "^1.3", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be in the require part, otherwise encoding does not work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please see #8 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets add these to suggest, and explain for what case one would want to use them in a project. |
||
"phpspec/phpspec": "^2.4", | ||
"henrikbjorn/phpspec-code-coverage" : "^1.0" | ||
}, | ||
|
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace spec\Http\Message\Encoding; | ||
|
||
use Psr\Http\Message\StreamInterface; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class ChunkStreamSpec extends ObjectBehavior | ||
{ | ||
function let(StreamInterface $stream) | ||
{ | ||
$this->beConstructedWith($stream); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Message\Encoding\ChunkStream'); | ||
} | ||
|
||
function it_is_a_stream() | ||
{ | ||
$this->shouldImplement('Psr\Http\Message\StreamInterface'); | ||
} | ||
|
||
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"); | ||
} | ||
} |
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\ObjectBehavior; | ||
|
||
class CompressStreamSpec extends ObjectBehavior | ||
{ | ||
function let(StreamInterface $stream) | ||
{ | ||
$this->beConstructedWith($stream); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Message\Encoding\CompressStream'); | ||
} | ||
|
||
function it_is_a_stream() | ||
{ | ||
$this->shouldImplement('Psr\Http\Message\StreamInterface'); | ||
} | ||
|
||
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')); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace spec\Http\Message\Encoding; | ||
|
||
use Psr\Http\Message\StreamInterface; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class DechunkStreamSpec extends ObjectBehavior | ||
{ | ||
function let(StreamInterface $stream) | ||
{ | ||
$this->beConstructedWith($stream); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Message\Encoding\DechunkStream'); | ||
} | ||
|
||
function it_is_a_stream() | ||
{ | ||
$this->shouldImplement('Psr\Http\Message\StreamInterface'); | ||
} | ||
|
||
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'); | ||
} | ||
} |
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\ObjectBehavior; | ||
|
||
class DecompressStreamSpec extends ObjectBehavior | ||
{ | ||
function let(StreamInterface $stream) | ||
{ | ||
$this->beConstructedWith($stream); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Message\Encoding\DecompressStream'); | ||
} | ||
|
||
function it_is_a_stream() | ||
{ | ||
$this->shouldImplement('Psr\Http\Message\StreamInterface'); | ||
} | ||
|
||
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'); | ||
} | ||
} |
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\ObjectBehavior; | ||
|
||
class DeflateStreamSpec extends ObjectBehavior | ||
{ | ||
function let(StreamInterface $stream) | ||
{ | ||
$this->beConstructedWith($stream); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Message\Encoding\DeflateStream'); | ||
} | ||
|
||
function it_is_a_stream() | ||
{ | ||
$this->shouldImplement('Psr\Http\Message\StreamInterface'); | ||
} | ||
|
||
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')); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could try to detect hhvm and trigger a skip just for the tests that don't work with hhvm rather than ignore hhvm issues over the whole package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point.