PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.5.5
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.5.5
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
stockpack / vendor / guzzlehttp / psr7 / tests / StreamTest.php

StreamTest.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 2.5.5, at vendor/guzzlehttp/psr7/tests/StreamTest.php

371 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace GuzzleHttp\Tests\Psr7;
3
4 use GuzzleHttp\Psr7\NoSeekStream;
5 use GuzzleHttp\Psr7\Stream;
6
7 /**
8 * @covers GuzzleHttp\Psr7\Stream
9 */
10 class StreamTest extends BaseTest
11 {
12 public static $isFReadError = false;
13
14 /**
15 * @expectedException \InvalidArgumentException
16 */
17 public function testConstructorThrowsExceptionOnInvalidArgument()
18 {
19 new Stream(true);
20 }
21
22 public function testConstructorInitializesProperties()
23 {
24 $handle = fopen('php://temp', 'r+');
25 fwrite($handle, 'data');
26 $stream = new Stream($handle);
27 $this->assertTrue($stream->isReadable());
28 $this->assertTrue($stream->isWritable());
29 $this->assertTrue($stream->isSeekable());
30 $this->assertEquals('php://temp', $stream->getMetadata('uri'));
31 $this->assertInternalType('array', $stream->getMetadata());
32 $this->assertEquals(4, $stream->getSize());
33 $this->assertFalse($stream->eof());
34 $stream->close();
35 }
36
37 public function testConstructorInitializesPropertiesWithRbPlus()
38 {
39 $handle = fopen('php://temp', 'rb+');
40 fwrite($handle, 'data');
41 $stream = new Stream($handle);
42 $this->assertTrue($stream->isReadable());
43 $this->assertTrue($stream->isWritable());
44 $this->assertTrue($stream->isSeekable());
45 $this->assertEquals('php://temp', $stream->getMetadata('uri'));
46 $this->assertInternalType('array', $stream->getMetadata());
47 $this->assertEquals(4, $stream->getSize());
48 $this->assertFalse($stream->eof());
49 $stream->close();
50 }
51
52 public function testStreamClosesHandleOnDestruct()
53 {
54 $handle = fopen('php://temp', 'r');
55 $stream = new Stream($handle);
56 unset($stream);
57 $this->assertFalse(is_resource($handle));
58 }
59
60 public function testConvertsToString()
61 {
62 $handle = fopen('php://temp', 'w+');
63 fwrite($handle, 'data');
64 $stream = new Stream($handle);
65 $this->assertEquals('data', (string) $stream);
66 $this->assertEquals('data', (string) $stream);
67 $stream->close();
68 }
69
70 public function testGetsContents()
71 {
72 $handle = fopen('php://temp', 'w+');
73 fwrite($handle, 'data');
74 $stream = new Stream($handle);
75 $this->assertEquals('', $stream->getContents());
76 $stream->seek(0);
77 $this->assertEquals('data', $stream->getContents());
78 $this->assertEquals('', $stream->getContents());
79 $stream->close();
80 }
81
82 public function testChecksEof()
83 {
84 $handle = fopen('php://temp', 'w+');
85 fwrite($handle, 'data');
86 $stream = new Stream($handle);
87 $this->assertSame(4, $stream->tell(), 'Stream cursor already at the end');
88 $this->assertFalse($stream->eof(), 'Stream still not eof');
89 $this->assertSame('', $stream->read(1), 'Need to read one more byte to reach eof');
90 $this->assertTrue($stream->eof());
91 $stream->close();
92 }
93
94 public function testGetSize()
95 {
96 $size = filesize(__FILE__);
97 $handle = fopen(__FILE__, 'r');
98 $stream = new Stream($handle);
99 $this->assertEquals($size, $stream->getSize());
100 // Load from cache
101 $this->assertEquals($size, $stream->getSize());
102 $stream->close();
103 }
104
105 public function testEnsuresSizeIsConsistent()
106 {
107 $h = fopen('php://temp', 'w+');
108 $this->assertEquals(3, fwrite($h, 'foo'));
109 $stream = new Stream($h);
110 $this->assertEquals(3, $stream->getSize());
111 $this->assertEquals(4, $stream->write('test'));
112 $this->assertEquals(7, $stream->getSize());
113 $this->assertEquals(7, $stream->getSize());
114 $stream->close();
115 }
116
117 public function testProvidesStreamPosition()
118 {
119 $handle = fopen('php://temp', 'w+');
120 $stream = new Stream($handle);
121 $this->assertEquals(0, $stream->tell());
122 $stream->write('foo');
123 $this->assertEquals(3, $stream->tell());
124 $stream->seek(1);
125 $this->assertEquals(1, $stream->tell());
126 $this->assertSame(ftell($handle), $stream->tell());
127 $stream->close();
128 }
129
130 public function testDetachStreamAndClearProperties()
131 {
132 $handle = fopen('php://temp', 'r');
133 $stream = new Stream($handle);
134 $this->assertSame($handle, $stream->detach());
135 $this->assertInternalType('resource', $handle, 'Stream is not closed');
136 $this->assertNull($stream->detach());
137
138 $this->assertStreamStateAfterClosedOrDetached($stream);
139
140 $stream->close();
141 }
142
143 public function testCloseResourceAndClearProperties()
144 {
145 $handle = fopen('php://temp', 'r');
146 $stream = new Stream($handle);
147 $stream->close();
148
149 $this->assertFalse(is_resource($handle));
150
151 $this->assertStreamStateAfterClosedOrDetached($stream);
152 }
153
154 private function assertStreamStateAfterClosedOrDetached(Stream $stream)
155 {
156 $this->assertFalse($stream->isReadable());
157 $this->assertFalse($stream->isWritable());
158 $this->assertFalse($stream->isSeekable());
159 $this->assertNull($stream->getSize());
160 $this->assertSame([], $stream->getMetadata());
161 $this->assertNull($stream->getMetadata('foo'));
162
163 $throws = function (callable $fn) {
164 try {
165 $fn();
166 } catch (\Exception $e) {
167 $this->assertContains('Stream is detached', $e->getMessage());
168
169 return;
170 }
171
172 $this->fail('Exception should be thrown after the stream is detached.');
173 };
174
175 $throws(function () use ($stream) { $stream->read(10); });
176 $throws(function () use ($stream) { $stream->write('bar'); });
177 $throws(function () use ($stream) { $stream->seek(10); });
178 $throws(function () use ($stream) { $stream->tell(); });
179 $throws(function () use ($stream) { $stream->eof(); });
180 $throws(function () use ($stream) { $stream->getContents(); });
181 $this->assertSame('', (string) $stream);
182 }
183
184 public function testStreamReadingWithZeroLength()
185 {
186 $r = fopen('php://temp', 'r');
187 $stream = new Stream($r);
188
189 $this->assertSame('', $stream->read(0));
190
191 $stream->close();
192 }
193
194 /**
195 * @expectedException \RuntimeException
196 * @expectedExceptionMessage Length parameter cannot be negative
197 */
198 public function testStreamReadingWithNegativeLength()
199 {
200 $r = fopen('php://temp', 'r');
201 $stream = new Stream($r);
202
203 try {
204 $stream->read(-1);
205 } catch (\Exception $e) {
206 $stream->close();
207 throw $e;
208 }
209
210 $stream->close();
211 }
212
213 /**
214 * @expectedException \RuntimeException
215 * @expectedExceptionMessage Unable to read from stream
216 */
217 public function testStreamReadingFreadError()
218 {
219 self::$isFReadError = true;
220 $r = fopen('php://temp', 'r');
221 $stream = new Stream($r);
222
223 try {
224 $stream->read(1);
225 } catch (\Exception $e) {
226 self::$isFReadError = false;
227 $stream->close();
228 throw $e;
229 }
230
231 self::$isFReadError = false;
232 $stream->close();
233 }
234
235 /**
236 * @dataProvider gzipModeProvider
237 *
238 * @param string $mode
239 * @param bool $readable
240 * @param bool $writable
241 */
242 public function testGzipStreamModes($mode, $readable, $writable)
243 {
244 if (defined('HHVM_VERSION')) {
245 $this->markTestSkipped('This does not work on HHVM.');
246 }
247
248 $r = gzopen('php://temp', $mode);
249 $stream = new Stream($r);
250
251 $this->assertSame($readable, $stream->isReadable());
252 $this->assertSame($writable, $stream->isWritable());
253
254 $stream->close();
255 }
256
257 public function gzipModeProvider()
258 {
259 return [
260 ['mode' => 'rb9', 'readable' => true, 'writable' => false],
261 ['mode' => 'wb2', 'readable' => false, 'writable' => true],
262 ];
263 }
264
265 /**
266 * @dataProvider readableModeProvider
267 *
268 * @param string $mode
269 */
270 public function testReadableStream($mode)
271 {
272 $r = fopen('php://temp', $mode);
273 $stream = new Stream($r);
274
275 $this->assertTrue($stream->isReadable());
276
277 $stream->close();
278 }
279
280 public function readableModeProvider()
281 {
282 return [
283 ['r'],
284 ['w+'],
285 ['r+'],
286 ['x+'],
287 ['c+'],
288 ['rb'],
289 ['w+b'],
290 ['r+b'],
291 ['x+b'],
292 ['c+b'],
293 ['rt'],
294 ['w+t'],
295 ['r+t'],
296 ['x+t'],
297 ['c+t'],
298 ['a+'],
299 ['rb+'],
300 ];
301 }
302
303 public function testWriteOnlyStreamIsNotReadable()
304 {
305 $r = fopen('php://output', 'w');
306 $stream = new Stream($r);
307
308 $this->assertFalse($stream->isReadable());
309
310 $stream->close();
311 }
312
313 /**
314 * @dataProvider writableModeProvider
315 *
316 * @param string $mode
317 */
318 public function testWritableStream($mode)
319 {
320 $r = fopen('php://temp', $mode);
321 $stream = new Stream($r);
322
323 $this->assertTrue($stream->isWritable());
324
325 $stream->close();
326 }
327
328 public function writableModeProvider()
329 {
330 return [
331 ['w'],
332 ['w+'],
333 ['rw'],
334 ['r+'],
335 ['x+'],
336 ['c+'],
337 ['wb'],
338 ['w+b'],
339 ['r+b'],
340 ['rb+'],
341 ['x+b'],
342 ['c+b'],
343 ['w+t'],
344 ['r+t'],
345 ['x+t'],
346 ['c+t'],
347 ['a'],
348 ['a+'],
349 ];
350 }
351
352 public function testReadOnlyStreamIsNotWritable()
353 {
354 $r = fopen('php://input', 'r');
355 $stream = new Stream($r);
356
357 $this->assertFalse($stream->isWritable());
358
359 $stream->close();
360 }
361 }
362
363 namespace GuzzleHttp\Psr7;
364
365 use GuzzleHttp\Tests\Psr7\StreamTest;
366
367 function fread($handle, $length)
368 {
369 return StreamTest::$isFReadError ? false : \fread($handle, $length);
370 }
371