Skip to content

Commit a301d77

Browse files
committed
Add Stream decorator
1 parent 9fc3a41 commit a301d77

File tree

2 files changed

+292
-0
lines changed

2 files changed

+292
-0
lines changed
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+
}

src/Decorator/StreamDecorator.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace Http\Message\Decorator;
4+
5+
use Psr\Http\Message\StreamInterface;
6+
7+
/**
8+
* Decorates a stream.
9+
*
10+
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
11+
*/
12+
trait StreamDecorator
13+
{
14+
/**
15+
* @var StreamInterface
16+
*/
17+
protected $stream;
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function __toString()
23+
{
24+
return $this->stream->__toString();
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function close()
31+
{
32+
$this->stream->close();
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function detach()
39+
{
40+
return $this->stream->detach();
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function getSize()
47+
{
48+
return $this->stream->getSize();
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function tell()
55+
{
56+
return $this->stream->tell();
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function eof()
63+
{
64+
return $this->stream->eof();
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function isSeekable()
71+
{
72+
return $this->stream->isSeekable();
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function seek($offset, $whence = SEEK_SET)
79+
{
80+
$this->stream->seek($offset, $whence);
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function rewind()
87+
{
88+
$this->stream->rewind();
89+
}
90+
91+
/**
92+
* {@inheritdoc}
93+
*/
94+
public function isWritable()
95+
{
96+
return $this->stream->isWritable();
97+
}
98+
99+
/**
100+
* {@inheritdoc}
101+
*/
102+
public function write($string)
103+
{
104+
return $this->stream->write($string);
105+
}
106+
107+
/**
108+
* {@inheritdoc}
109+
*/
110+
public function isReadable()
111+
{
112+
return $this->stream->isReadable();
113+
}
114+
115+
/**
116+
* {@inheritdoc}
117+
*/
118+
public function read($length)
119+
{
120+
return $this->stream->read($length);
121+
}
122+
123+
/**
124+
* {@inheritdoc}
125+
*/
126+
public function getContents()
127+
{
128+
return $this->stream->getContents();
129+
}
130+
131+
/**
132+
* {@inheritdoc}
133+
*/
134+
public function getMetadata($key = null)
135+
{
136+
return $this->stream->getMetadata($key);
137+
}
138+
}

0 commit comments

Comments
 (0)