44 return \fopen(
"data://text/plain,$content", $mode);
48 protected function setUp(): void
52 self::$functions = Mockery::mock();
58 $content =
'awesome content stream';
62 $subject =
new Stream($resource);
63 $detachedResource = $subject->detach();
66 $this->assertTrue(is_resource($detachedResource));
67 $this->assertSame($resource, $detachedResource);
75 $content =
'awesome content stream';
79 $subject =
new Stream($resource);
82 $detachedResource = $subject->detach();
83 $this->assertTrue(is_resource($detachedResource));
86 $detachedResource = $subject->detach();
87 $this->assertNull($detachedResource);
93 $content =
'awesome content stream';
94 $correctSize = strlen($content);
98 $subject =
new Stream($resource);
100 $size = $subject->getSize();
101 $this->assertSame($correctSize, $size);
107 $content =
'awesome content stream';
113 $subject =
new Stream($resource, $options);
115 $size = $subject->getSize();
116 $this->assertSame($correctSize, $size);
121 $content =
'awesome content stream';
125 $subject =
new Stream($resource);
128 $size = $subject->getSize();
129 $this->assertNull($size);
134 $content =
'awesome content stream';
138 $subject =
new Stream($resource);
141 $this->assertFalse(is_resource($resource));
146 $content =
'awesome content stream';
150 $subject =
new Stream($resource);
152 $actualResource = $subject->detach();
155 $this->assertTrue(is_resource($actualResource));
160 $content =
'awesome content stream';
164 fseek($resource, $offset);
166 $subject =
new Stream($resource);
168 $actualPosition = $subject->tell();
169 $this->assertSame($offset, $actualPosition);
174 $content =
'awesome content stream';
178 $subject =
new Stream($resource);
181 $this->expectException(\RuntimeException::class);
182 $this->expectExceptionMessage(
'Stream is detached');
189 $content =
'awesome content stream';
194 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
195 $functionMock->shouldReceive(
'ftell')
200 $functionMock->shouldReceive(
'fclose')
204 $subject =
new Stream($resource);
206 $this->expectException(\RuntimeException::class);
207 $this->expectExceptionMessage(
'Unable to determine stream position');
218 $content =
'awesome content stream';
220 $offset = strlen($content);
222 fseek($resource, $offset);
225 $subject =
new Stream($resource);
227 $endOfFileReached = $subject->eof();
228 $this->assertTrue($endOfFileReached);
237 $content =
'awesome content stream';
241 $subject =
new Stream($resource);
244 $this->expectException(\RuntimeException::class);
245 $this->expectExceptionMessage(
'Stream is detached');
257 $content =
'awesome content stream';
262 $subject =
new Stream($resource);
264 $subject->seek($offset);
265 $this->assertSame($offset, ftell($resource));
274 $content =
'awesome content stream';
279 $subject =
new Stream($resource);
282 $this->expectException(\RuntimeException::class);
283 $this->expectExceptionMessage(
'Stream is detached');
285 $subject->seek($offset);
294 $content =
'awesome content stream';
299 $subjectMock = Mockery::mock(Stream::class .
'[isSeekable]', [$resource]);
302 ->shouldReceive(
'isSeekable')
306 $this->expectException(\RuntimeException::class);
307 $this->expectExceptionMessage(
'Stream is not seekable');
309 $subjectMock->seek($offset);
318 $content =
'awesome content stream';
324 $subject =
new Stream($resource);
327 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
328 $functionMock->shouldReceive(
'fseek')
330 ->withArgs([$resource, $offset, $whence])
333 $functionMock->shouldReceive(
'fclose')
337 $this->expectException(\RuntimeException::class);
338 $this->expectExceptionMessage(
"Unable to seek to stream position \"$offset\" with whence \"$whence\"");
340 $subject->seek($offset);
349 $content =
'awesome content stream';
350 $expectedResult =
"awesome";
355 $subject =
new Stream($resource);
357 $text = $subject->read($length);
358 $this->assertSame($expectedResult, $text);
367 $content =
'awesome content stream';
368 $expectedResult =
"";
373 $subject =
new Stream($resource);
375 $text = $subject->read($length);
376 $this->assertSame($expectedResult, $text);
385 $content =
'awesome content stream';
390 $subject =
new Stream($resource);
393 $this->expectException(\RuntimeException::class);
394 $this->expectExceptionMessage(
'Stream is detached');
396 $subject->read($length);
405 $content =
'awesome content stream';
410 $subject =
new Stream($resource);
412 $this->expectException(\RuntimeException::class);
413 $this->expectExceptionMessage(
'Length parameter must not be negative');
415 $subject->read($length);
424 $content =
'awesome content stream';
429 $subject =
new Stream($resource);
431 $this->expectException(\RuntimeException::class);
432 $this->expectExceptionMessage(
'Can not read from non-readable stream');
434 $subject->read($length);
443 $content =
'awesome content stream';
448 $subject =
new Stream($resource);
451 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
453 $functionMock->shouldReceive(
'fread')
455 ->withArgs([$resource, $length])
458 $functionMock->shouldReceive(
'fclose')
462 $this->expectException(\RuntimeException::class);
463 $this->expectExceptionMessage(
'Unable to read from stream');
465 $subject->read($length);
474 $content =
'awesome content stream';
478 $subject =
new Stream($resource);
480 $text = $subject->getContents();
481 $this->assertSame($content, $text);
490 $content =
'awesome content stream';
494 $subject =
new Stream($resource);
497 $this->expectException(\RuntimeException::class);
498 $this->expectExceptionMessage(
'Stream is detached');
500 $subject->getContents();
509 $content =
'awesome content stream';
513 $subject =
new Stream($resource);
516 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
518 $functionMock->shouldReceive(
'stream_get_contents')
523 $functionMock->shouldReceive(
'fclose')
527 $this->expectException(\RuntimeException::class);
528 $this->expectExceptionMessage(
'Unable to read stream contents');
530 $subject->getContents();
539 $content =
'awesome content stream';
543 $subject =
new Stream($resource);
545 $text = $subject->__toString();
546 $this->assertSame($content, $text);
557 $content =
'awesome content stream';
558 $expectedResult =
'';
562 $subject = Mockery::mock(Stream::class .
'[rewind]', [$resource]);
564 $subject->shouldDeferMissing();
565 $subject->shouldReceive(
'rewind')
567 ->andThrow(\RuntimeException::class);
569 $text = $subject->__toString();
570 $this->assertSame($expectedResult, $text);
579 $content =
'awesome content stream';
581 $byteCount = strlen($newContent);
583 $resource = fopen(
'php://memory', $mode);
586 $subject =
new Stream($resource);
587 $currentSize = $subject->getSize();
589 $numberOfBytesWritten = $subject->write($newContent);
590 $newSize = $subject->getSize();
592 $this->assertSame($byteCount, $numberOfBytesWritten,
'The count of bytes passed to write must match the written bytes after the operation.');
593 $this->assertGreaterThan($currentSize, $newSize,
'The new size must be grater than the old size because we wrote to the stream.');
602 $content =
'awesome content stream';
607 $subject =
new Stream($resource);
610 $this->expectException(\RuntimeException::class);
611 $this->expectExceptionMessage(
'Stream is detached');
613 $subject->write($newContent);
622 $content =
'awesome content stream';
627 $subject =
new Stream($resource);
629 $this->expectException(\RuntimeException::class);
630 $this->expectExceptionMessage(
'Can not write to a non-writable stream');
632 $subject->write($newContent);
641 $content =
'awesome content stream';
646 $subject =
new Stream($resource);
649 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
651 $functionMock->shouldReceive(
'fwrite')
653 ->withArgs([$resource, $newContent])
656 $functionMock->shouldReceive(
'fclose')
660 $this->expectException(\RuntimeException::class);
661 $this->expectExceptionMessage(
'Unable to write to stream');
663 $subject->write($newContent);
testCloseWithDetachedStreamWhichShouldDoNothing()
testTellWhichShouldSucceed()
testWriteWithDetachedStreamWhichShouldFail()
testReadWithNegativeLengthWhichShouldFail()
testReadWithZeroLengthWhichShouldSucceed()
testWriteWithFailingFwriteCallWhichShouldFail()
testGetSizeWithDetachedStreamWhichShouldFail()
testEofWithDetachedStreamWhichShouldFail()
createResource(string $content, string $mode)
testWriteWhichShouldSucceed()
testTellWithDetachedStreamWhichShouldFail()
testSeekWithFseekFailureWhichShouldFail()
testCloseWhichShouldSucceed()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testDetachWhichShouldSucceed()
testWriteWithReadOnlyStreamWhichShouldFail()
testDetachDoubleInvocationWhichShouldFail()
testReadWithFailingFreadCallWhichShouldFail()
testToStringWithErrorWhichShouldSucceed()
testSeekWhichShouldSucceed()
testTellWithFtellFailureWhichShouldFail()
static fwrite($handle, string $string, ?int $length=null)
testReadWithUnreadableStreamWhichShouldFail()
testGetContentsWhichShouldSucceed()
testEofWhichShouldSucceed()
testGetSizeWithOptionsWhichShouldSucceed()
testReadWhichShouldSucceed()
testToStringWhichShouldSucceed()
testGetContentsWithDetachedStreamWhichShouldFail()
testGetContentsWithFailingStreamGetContentsCallWhichShouldFail()
testSeekWithNotSeekableStreamWhichShouldFail()
testGetSizeWithStatsWhichShouldSucceed()
testSeekWithDetachedStreamWhichShouldFail()
The streaming options are used by the stream implementation.
testReadWithDetachedStreamWhichShouldFail()