Skip to content

Commit ffad01f

Browse files
joelwurtzdbu
authored andcommitted
Add buffered stream (#52)
Add buffered stream
1 parent 8cd7fb1 commit ffad01f

File tree

2 files changed

+454
-0
lines changed

2 files changed

+454
-0
lines changed

spec/Stream/BufferedStreamSpec.php

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Stream;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Psr\Http\Message\StreamInterface;
7+
8+
class BufferedStreamSpec extends ObjectBehavior
9+
{
10+
public function let(StreamInterface $stream)
11+
{
12+
$this->beConstructedWith($stream);
13+
14+
$stream->getSize()->willReturn(null);
15+
}
16+
17+
public function it_is_castable_to_string(StreamInterface $stream)
18+
{
19+
$eofCounter = 0;
20+
$stream->eof()->will(function () use(&$eofCounter) {
21+
return (++$eofCounter > 1);
22+
});
23+
24+
$stream->read(8192)->willReturn('foo');
25+
26+
$this->__toString()->shouldReturn('foo');
27+
}
28+
29+
public function it_detachs(StreamInterface $stream)
30+
{
31+
$stream->eof()->willReturn(true);
32+
$stream->read(8192)->willReturn('');
33+
$stream->close()->shouldBeCalled();
34+
35+
$this->detach()->shouldBeResource();
36+
$this->detach()->shouldBeNull();
37+
}
38+
39+
public function it_gets_size(StreamInterface $stream)
40+
{
41+
$stream->eof()->willReturn(false);
42+
$this->getSize()->shouldReturn(null);
43+
44+
$eofCounter = 0;
45+
$stream->eof()->will(function () use(&$eofCounter) {
46+
return (++$eofCounter > 1);
47+
});
48+
49+
$stream->read(8192)->willReturn('foo');
50+
51+
$this->getContents()->shouldReturn('foo');
52+
$this->getSize()->shouldReturn(3);
53+
}
54+
55+
public function it_tells(StreamInterface $stream)
56+
{
57+
$this->tell()->shouldReturn(0);
58+
59+
$eofCounter = 0;
60+
$stream->eof()->will(function () use(&$eofCounter) {
61+
return (++$eofCounter > 1);
62+
});
63+
64+
$stream->read(8192)->willReturn('foo');
65+
$this->getContents()->shouldReturn('foo');
66+
$this->tell()->shouldReturn(3);
67+
}
68+
69+
public function it_eof(StreamInterface $stream)
70+
{
71+
// Case when underlying is false
72+
$stream->eof()->willReturn(false);
73+
$this->eof()->shouldReturn(false);
74+
75+
// Case when sync and underlying is true
76+
$stream->eof()->willReturn(true);
77+
$this->eof()->shouldReturn(true);
78+
79+
// Case not sync but underlying is true
80+
$eofCounter = 0;
81+
$stream->eof()->will(function () use(&$eofCounter) {
82+
return (++$eofCounter > 1);
83+
});
84+
85+
$stream->read(8192)->willReturn('foo');
86+
87+
$this->getContents()->shouldReturn('foo');
88+
$this->seek(0);
89+
90+
$stream->eof()->willReturn(true);
91+
$this->eof()->shouldReturn(false);
92+
}
93+
94+
public function it_is_seekable(StreamInterface $stream)
95+
{
96+
$this->isSeekable()->shouldReturn(true);
97+
}
98+
99+
public function it_seeks(StreamInterface $stream)
100+
{
101+
$this->seek(0);
102+
$this->tell()->shouldReturn(0);
103+
104+
$eofCounter = 0;
105+
$stream->eof()->will(function () use(&$eofCounter) {
106+
return (++$eofCounter > 1);
107+
});
108+
109+
$stream->read(8192)->willReturn('foo');
110+
111+
$this->getContents()->shouldReturn('foo');
112+
$this->seek(2);
113+
$this->tell()->shouldReturn(2);
114+
}
115+
116+
public function it_rewinds(StreamInterface $stream)
117+
{
118+
$this->rewind();
119+
$this->tell()->shouldReturn(0);
120+
121+
$eofCounter = 0;
122+
$stream->eof()->will(function () use(&$eofCounter) {
123+
return (++$eofCounter > 1);
124+
});
125+
126+
$stream->read(8192)->willReturn('foo');
127+
128+
$this->getContents()->shouldReturn('foo');
129+
$this->tell()->shouldReturn(3);
130+
$this->rewind();
131+
$this->tell()->shouldReturn(0);
132+
}
133+
134+
public function it_is_not_writable(StreamInterface $stream)
135+
{
136+
$this->isWritable()->shouldReturn(false);
137+
$this->shouldThrow('\RuntimeException')->duringWrite('foo');
138+
}
139+
140+
public function it_is_readable(StreamInterface $stream)
141+
{
142+
$this->isReadable()->shouldReturn(true);
143+
}
144+
145+
public function it_reads(StreamInterface $stream)
146+
{
147+
$eofCounter = 0;
148+
$stream->eof()->will(function () use(&$eofCounter) {
149+
return (++$eofCounter > 1);
150+
});
151+
152+
$stream->read(3)->willReturn('foo');
153+
$this->read(3)->shouldReturn('foo');
154+
155+
$stream->read(3)->willReturn('bar');
156+
$this->read(3)->shouldReturn('bar');
157+
158+
$this->rewind();
159+
$this->read(4)->shouldReturn('foob');
160+
161+
$stream->read(3)->willReturn('baz');
162+
$this->read(5)->shouldReturn('arbaz');
163+
}
164+
165+
public function it_get_contents(StreamInterface $stream)
166+
{
167+
$eofCounter = 0;
168+
$stream->eof()->will(function () use(&$eofCounter) {
169+
return (++$eofCounter > 1);
170+
});
171+
172+
$stream->read(8192)->willReturn('foo');
173+
174+
$this->getContents()->shouldReturn('foo');
175+
$this->eof()->shouldReturn(true);
176+
}
177+
178+
public function it_get_metadatas(StreamInterface $stream)
179+
{
180+
$this->getMetadata()->shouldBeArray();
181+
$this->getMetadata('unexistant')->shouldBeNull();
182+
$this->getMetadata('stream_type')->shouldReturn('TEMP');
183+
}
184+
}

0 commit comments

Comments
 (0)