forked from laminas/laminas-diactoros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelativeStreamTest.php
199 lines (169 loc) · 6.94 KB
/
RelativeStreamTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace LaminasTest\Diactoros;
use Laminas\Diactoros\RelativeStream;
use Laminas\Diactoros\Stream;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use const SEEK_SET;
/**
* @covers \Laminas\Diactoros\RelativeStream
*/
class RelativeStreamTest extends TestCase
{
public function testToString(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->method('isSeekable')->willReturn(true);
$decorated->method('tell')->willReturn(100);
$decorated->expects(self::once())->method('seek')->with(100, SEEK_SET);
$decorated->expects(self::once())->method('getContents')->willReturn('foobarbaz');
$stream = new RelativeStream($decorated, 100);
$ret = $stream->__toString();
$this->assertSame('foobarbaz', $ret);
}
public function testClose(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('close');
$stream = new RelativeStream($decorated, 100);
$stream->close();
}
public function testDetach(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('detach')->willReturn(250);
$stream = new RelativeStream($decorated, 100);
$ret = $stream->detach();
$this->assertSame(250, $ret);
}
public function testGetSize(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('getSize')->willReturn(250);
$stream = new RelativeStream($decorated, 100);
$ret = $stream->getSize();
$this->assertSame(150, $ret);
}
public function testTell(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('tell')->willReturn(188);
$stream = new RelativeStream($decorated, 100);
$ret = $stream->tell();
$this->assertSame(88, $ret);
}
public function testIsSeekable(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('isSeekable')->willReturn(true);
$stream = new RelativeStream($decorated, 100);
$ret = $stream->isSeekable();
$this->assertSame(true, $ret);
}
public function testIsWritable(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('isWritable')->willReturn(true);
$stream = new RelativeStream($decorated, 100);
$ret = $stream->isWritable();
$this->assertSame(true, $ret);
}
public function testIsReadable(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('isReadable')->willReturn(false);
$stream = new RelativeStream($decorated, 100);
$ret = $stream->isReadable();
$this->assertSame(false, $ret);
}
public function testSeek(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('seek')->with(126, SEEK_SET);
$stream = new RelativeStream($decorated, 100);
$this->assertNull($stream->seek(26));
}
public function testRewind(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('seek')->with(100, SEEK_SET);
$stream = new RelativeStream($decorated, 100);
$this->assertNull($stream->rewind());
}
public function testWrite(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->method('tell')->willReturn(100);
$decorated->expects(self::once())->method('write')->with('foobaz')->willReturn(6);
$stream = new RelativeStream($decorated, 100);
$ret = $stream->write("foobaz");
$this->assertSame(6, $ret);
}
public function testRead(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->method('tell')->willReturn(100);
$decorated->expects(self::once())->method('read')->with(3)->willReturn('foo');
$stream = new RelativeStream($decorated, 100);
$ret = $stream->read(3);
$this->assertSame("foo", $ret);
}
public function testGetContents(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->method('tell')->willReturn(100);
$decorated->expects(self::once())->method('getContents')->willReturn('foo');
$stream = new RelativeStream($decorated, 100);
$ret = $stream->getContents();
$this->assertSame("foo", $ret);
}
public function testGetMetadata(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('getMetadata')->with('bar')->willReturn('foo');
$stream = new RelativeStream($decorated, 100);
$ret = $stream->getMetadata("bar");
$this->assertSame("foo", $ret);
}
public function testWriteRaisesExceptionWhenPointerIsBehindOffset(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('tell')->willReturn(0);
$decorated->expects(self::never())->method('write')->with('foobaz');
$stream = new RelativeStream($decorated, 100);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Invalid pointer position');
$stream->write("foobaz");
}
public function testReadRaisesExceptionWhenPointerIsBehindOffset(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('tell')->willReturn(0);
$decorated->expects(self::never())->method('read')->with(3);
$stream = new RelativeStream($decorated, 100);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Invalid pointer position');
$stream->read(3);
}
public function testGetContentsRaisesExceptionWhenPointerIsBehindOffset(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->expects(self::once())->method('tell')->willReturn(0);
$decorated->expects(self::never())->method('getContents');
$stream = new RelativeStream($decorated, 100);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Invalid pointer position');
$stream->getContents();
}
public function testCanReadContentFromNotSeekableResource(): void
{
$decorated = $this->createMock(Stream::class);
$decorated->method('isSeekable')->willReturn(false);
$decorated->expects(self::never())->method('seek');
$decorated->method('tell')->willReturn(3);
$decorated->method('getContents')->willReturn('CONTENTS');
$stream = new RelativeStream($decorated, 3);
$this->assertSame('CONTENTS', $stream->__toString());
}
}