Skip to content

Commit dbbded3

Browse files
committed
Merge pull request #5 from php-http/encoding
Encoding
2 parents a25286b + 827ed4e commit dbbded3

24 files changed

+1242
-2
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313
"require": {
1414
"php": ">=5.4",
1515
"psr/http-message": "^1.0",
16-
"php-http/message-factory": "^1.0"
16+
"php-http/message-factory": "^1.0",
17+
"clue/stream-filter": "^1.3"
1718
},
1819
"require-dev": {
1920
"zendframework/zend-diactoros": "^1.0",
2021
"guzzlehttp/psr7": "^1.0",
22+
"ext-zlib": "*",
2123
"phpspec/phpspec": "^2.4",
2224
"henrikbjorn/phpspec-code-coverage" : "^1.0"
2325
},
2426
"suggest": {
2527
"zendframework/zend-diactoros": "Used with Diactoros Factories",
26-
"guzzlehttp/psr7": "Used with Guzzle PSR-7 Factories"
28+
"guzzlehttp/psr7": "Used with Guzzle PSR-7 Factories",
29+
"ext-zlib": "Used with compressor/decompressor streams"
2730
},
2831
"autoload": {
2932
"psr-4": {
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Decorator;
4+
5+
use Http\Message\Decorator\StreamDecorator;
6+
use Psr\Http\Message\StreamInterface;
7+
use PhpSpec\ObjectBehavior;
8+
use Prophecy\Argument;
9+
10+
class StreamDecoratorSpec extends ObjectBehavior
11+
{
12+
function let(StreamInterface $stream)
13+
{
14+
$this->beAnInstanceOf('spec\Http\Message\Decorator\StreamDecoratorStub', [$stream]);
15+
}
16+
17+
function it_is_initializable()
18+
{
19+
$this->shouldHaveType('spec\Http\Message\Decorator\StreamDecoratorStub');
20+
}
21+
22+
function it_is_a_stream_decorator()
23+
{
24+
$this->shouldUseTrait('Http\Message\Decorator\StreamDecorator');
25+
}
26+
27+
function it_casts_the_stream_to_string(StreamInterface $stream)
28+
{
29+
$stream->__toString()->willReturn('body');
30+
31+
$this->__toString()->shouldReturn('body');
32+
}
33+
34+
function it_closes_the_stream(StreamInterface $stream)
35+
{
36+
$stream->close()->shouldBeCalled();
37+
38+
$this->close();
39+
}
40+
41+
function it_detaches_the_stream(StreamInterface $stream)
42+
{
43+
$stream->detach()->willReturn('detached');
44+
45+
$this->detach()->shouldReturn('detached');
46+
}
47+
48+
function it_returns_the_size_of_the_stream(StreamInterface $stream)
49+
{
50+
$stream->getSize()->willReturn(1234);
51+
52+
$this->getSize()->shouldReturn(1234);
53+
}
54+
55+
function it_returns_the_current_position_of_the_stream(StreamInterface $stream)
56+
{
57+
$stream->tell()->willReturn(0);
58+
59+
$this->tell()->shouldReturn(0);
60+
}
61+
62+
function it_checks_whether_the_stream_is_eof(StreamInterface $stream)
63+
{
64+
$stream->eof()->willReturn(false);
65+
66+
$this->eof()->shouldReturn(false);
67+
}
68+
69+
function it_checks_whether_the_stream_is_seekable(StreamInterface $stream)
70+
{
71+
$stream->isSeekable()->willReturn(true);
72+
73+
$this->isSeekable()->shouldReturn(true);
74+
}
75+
76+
function it_seeks_the_current_position_of_the_stream(StreamInterface $stream)
77+
{
78+
$stream->seek(0, SEEK_SET)->shouldBeCalled();
79+
80+
$this->seek(0);
81+
}
82+
83+
function it_rewinds_to_the_beginning_of_the_stream(StreamInterface $stream)
84+
{
85+
$stream->rewind()->shouldBeCalled();
86+
87+
$this->rewind();
88+
}
89+
90+
function it_checks_whether_the_stream_is_writable(StreamInterface $stream)
91+
{
92+
$stream->isWritable()->willReturn(true);
93+
94+
$this->isWritable()->shouldReturn(true);
95+
}
96+
97+
function it_writes_to_the_stream(StreamInterface $stream)
98+
{
99+
$stream->write('body')->shouldBeCalled();
100+
101+
$this->write('body');
102+
}
103+
104+
function it_checks_whether_the_stream_is_readable(StreamInterface $stream)
105+
{
106+
$stream->isReadable()->willReturn(true);
107+
108+
$this->isReadable()->shouldReturn(true);
109+
}
110+
111+
function it_reads_from_the_stream(StreamInterface $stream)
112+
{
113+
$stream->read(4)->willReturn('body');
114+
115+
$this->read(4)->shouldReturn('body');
116+
}
117+
118+
function it_returns_the_contents_of_the_stream(StreamInterface $stream)
119+
{
120+
$stream->getContents()->willReturn('body');
121+
122+
$this->getContents()->shouldReturn('body');
123+
}
124+
125+
function it_returns_metadata_of_the_stream(StreamInterface $stream)
126+
{
127+
$stream->getMetadata(null)->willReturn(['key' => 'value']);
128+
$stream->getMetadata('key')->willReturn('value');
129+
$stream->getMetadata('key2')->willReturn(null);
130+
131+
$this->getMetadata()->shouldReturn(['key' => 'value']);
132+
$this->getMetadata('key')->shouldReturn('value');
133+
$this->getMetadata('key2')->shouldReturn(null);
134+
}
135+
136+
function getMatchers()
137+
{
138+
return [
139+
'useTrait' => function ($subject, $trait) {
140+
return class_uses($subject, $trait);
141+
}
142+
];
143+
}
144+
}
145+
146+
class StreamDecoratorStub implements StreamInterface
147+
{
148+
use StreamDecorator;
149+
150+
public function __construct(StreamInterface $stream)
151+
{
152+
$this->stream = $stream;
153+
}
154+
}

spec/Encoding/ChunkStreamSpec.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\Exception\Example\SkippingException;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class ChunkStreamSpec extends ObjectBehavior
10+
{
11+
use StreamBehavior;
12+
13+
function let(StreamInterface $stream)
14+
{
15+
if (defined('HHVM_VERSION')) {
16+
throw new SkippingException('Skipping test as there is no dechunk filter on hhvm');
17+
}
18+
19+
$this->beConstructedWith($stream);
20+
}
21+
22+
function it_is_initializable()
23+
{
24+
$this->shouldHaveType('Http\Message\Encoding\ChunkStream');
25+
}
26+
27+
function it_chunks_content()
28+
{
29+
$stream = new MemoryStream('This is a stream');
30+
$this->beConstructedWith($stream, 6);
31+
32+
$this->getContents()->shouldReturn("10\r\nThis is a stream\r\n0\r\n\r\n");
33+
}
34+
35+
function it_chunks_in_multiple()
36+
{
37+
$stream = new MemoryStream('This is a stream', 6);
38+
$this->beConstructedWith($stream, 6);
39+
40+
$this->getContents()->shouldReturn("6\r\nThis i\r\n6\r\ns a st\r\n4\r\nream\r\n0\r\n\r\n");
41+
}
42+
}

spec/Encoding/CompressStreamSpec.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\Exception\Example\SkippingException;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class CompressStreamSpec extends ObjectBehavior
10+
{
11+
use StreamBehavior, ZlibStreamBehavior;
12+
13+
function let(StreamInterface $stream)
14+
{
15+
if (defined('HHVM_VERSION')) {
16+
throw new SkippingException('Skipping test as zlib is not working on hhvm');
17+
}
18+
19+
$this->beConstructedWith($stream);
20+
}
21+
22+
function it_is_initializable()
23+
{
24+
$this->shouldHaveType('Http\Message\Encoding\CompressStream');
25+
}
26+
27+
function it_reads()
28+
{
29+
$stream = new MemoryStream('This is a test stream');
30+
$this->beConstructedWith($stream);
31+
32+
$stream->rewind();
33+
$this->read(4)->shouldReturn(substr(gzcompress('This is a test stream'), 0, 4));
34+
}
35+
36+
function it_gets_content()
37+
{
38+
$stream = new MemoryStream('This is a test stream');
39+
$this->beConstructedWith($stream);
40+
41+
$stream->rewind();
42+
$this->getContents()->shouldReturn(gzcompress('This is a test stream'));
43+
}
44+
}

spec/Encoding/DechunkStreamSpec.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\Exception\Example\SkippingException;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class DechunkStreamSpec extends ObjectBehavior
10+
{
11+
use StreamBehavior;
12+
13+
function let(StreamInterface $stream)
14+
{
15+
if (defined('HHVM_VERSION')) {
16+
throw new SkippingException('Skipping test as there is no dechunk filter on hhvm');
17+
}
18+
19+
$this->beConstructedWith($stream);
20+
}
21+
22+
function it_is_initializable()
23+
{
24+
$this->shouldHaveType('Http\Message\Encoding\DechunkStream');
25+
}
26+
27+
function it_reads()
28+
{
29+
$stream = new MemoryStream("4\r\ntest\r\n4\r\ntest\r\n0\r\n\r\n\0");
30+
$this->beConstructedWith($stream);
31+
32+
$this->read(6)->shouldReturn('testte');
33+
$this->read(6)->shouldReturn('st');
34+
}
35+
36+
function it_gets_content()
37+
{
38+
$stream = new MemoryStream("4\r\ntest\r\n0\r\n\r\n\0");
39+
$this->beConstructedWith($stream);
40+
41+
$this->getContents()->shouldReturn('test');
42+
}
43+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\Exception\Example\SkippingException;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class DecompressStreamSpec extends ObjectBehavior
10+
{
11+
use StreamBehavior, ZlibStreamBehavior;
12+
13+
function let(StreamInterface $stream)
14+
{
15+
if (defined('HHVM_VERSION')) {
16+
throw new SkippingException('Skipping test as zlib is not working on hhvm');
17+
}
18+
19+
$this->beConstructedWith($stream);
20+
}
21+
22+
function it_is_initializable()
23+
{
24+
$this->shouldHaveType('Http\Message\Encoding\DecompressStream');
25+
}
26+
27+
function it_reads()
28+
{
29+
// "This is a test stream" | deflate
30+
$stream = new MemoryStream(gzcompress('This is a test stream'));
31+
$this->beConstructedWith($stream);
32+
33+
$this->read(4)->shouldReturn('This');
34+
}
35+
36+
function it_gets_content()
37+
{
38+
// "This is a test stream" | deflate
39+
$stream = new MemoryStream(gzcompress('This is a test stream'));
40+
$this->beConstructedWith($stream);
41+
42+
$this->getContents()->shouldReturn('This is a test stream');
43+
}
44+
}

spec/Encoding/DeflateStreamSpec.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Encoding;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class DeflateStreamSpec extends ObjectBehavior
9+
{
10+
use StreamBehavior;
11+
12+
function let(StreamInterface $stream)
13+
{
14+
$this->beConstructedWith($stream);
15+
}
16+
17+
function it_is_initializable()
18+
{
19+
$this->shouldHaveType('Http\Message\Encoding\DeflateStream');
20+
}
21+
22+
function it_reads()
23+
{
24+
$stream = new MemoryStream('This is a test stream');
25+
$this->beConstructedWith($stream);
26+
27+
$stream->rewind();
28+
$this->read(4)->shouldReturn(substr(gzdeflate('This is a test stream'),0, 4));
29+
}
30+
31+
function it_gets_content()
32+
{
33+
$stream = new MemoryStream('This is a test stream');
34+
$this->beConstructedWith($stream);
35+
36+
$stream->rewind();
37+
$this->getContents()->shouldReturn(gzdeflate('This is a test stream'));
38+
}
39+
}

0 commit comments

Comments
 (0)