35 #[BackupGlobals(false)] 36 #[BackupStaticProperties(false)] 37 #[PreserveGlobalState(false)] 38 #[RunTestsInSeparateProcesses] 49 return \fopen(
"data://text/plain,$content", $mode);
53 protected function setUp(): void
57 self::$functions = Mockery::mock();
63 $content =
'awesome content stream';
67 $subject =
new Stream($resource);
68 $detachedResource = $subject->detach();
71 $this->assertTrue(is_resource($detachedResource));
72 $this->assertSame($resource, $detachedResource);
80 $content =
'awesome content stream';
84 $subject =
new Stream($resource);
87 $detachedResource = $subject->detach();
88 $this->assertTrue(is_resource($detachedResource));
91 $detachedResource = $subject->detach();
92 $this->assertNull($detachedResource);
98 $content =
'awesome content stream';
99 $correctSize = strlen($content);
103 $subject =
new Stream($resource);
105 $size = $subject->getSize();
106 $this->assertSame($correctSize, $size);
112 $content =
'awesome content stream';
118 $subject =
new Stream($resource, $options);
120 $size = $subject->getSize();
121 $this->assertSame($correctSize, $size);
126 $content =
'awesome content stream';
130 $subject =
new Stream($resource);
133 $size = $subject->getSize();
134 $this->assertNull($size);
139 $content =
'awesome content stream';
143 $subject =
new Stream($resource);
146 $this->assertFalse(is_resource($resource));
151 $content =
'awesome content stream';
155 $subject =
new Stream($resource);
157 $actualResource = $subject->detach();
160 $this->assertTrue(is_resource($actualResource));
165 $content =
'awesome content stream';
169 fseek($resource, $offset);
171 $subject =
new Stream($resource);
173 $actualPosition = $subject->tell();
174 $this->assertSame($offset, $actualPosition);
179 $content =
'awesome content stream';
183 $subject =
new Stream($resource);
186 $this->expectException(\RuntimeException::class);
187 $this->expectExceptionMessage(
'Stream is detached');
194 $content =
'awesome content stream';
199 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
200 $functionMock->shouldReceive(
'ftell')
205 $functionMock->shouldReceive(
'fclose')
209 $subject =
new Stream($resource);
211 $this->expectException(\RuntimeException::class);
212 $this->expectExceptionMessage(
'Unable to determine stream position');
221 $content =
'awesome content stream';
223 $offset = strlen($content);
225 fseek($resource, $offset);
228 $subject =
new Stream($resource);
230 $endOfFileReached = $subject->eof();
231 $this->assertTrue($endOfFileReached);
238 $content =
'awesome content stream';
242 $subject =
new Stream($resource);
245 $this->expectException(\RuntimeException::class);
246 $this->expectExceptionMessage(
'Stream is detached');
256 $content =
'awesome content stream';
261 $subject =
new Stream($resource);
263 $subject->seek($offset);
264 $this->assertSame($offset, ftell($resource));
271 $content =
'awesome content stream';
276 $subject =
new Stream($resource);
279 $this->expectException(\RuntimeException::class);
280 $this->expectExceptionMessage(
'Stream is detached');
282 $subject->seek($offset);
289 $content =
'awesome content stream';
294 $subjectMock = Mockery::mock(Stream::class .
'[isSeekable]', [$resource]);
297 ->shouldReceive(
'isSeekable')
301 $this->expectException(\RuntimeException::class);
302 $this->expectExceptionMessage(
'Stream is not seekable');
304 $subjectMock->seek($offset);
311 $content =
'awesome content stream';
317 $subject =
new Stream($resource);
320 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
321 $functionMock->shouldReceive(
'fseek')
323 ->withArgs([$resource, $offset, $whence])
326 $functionMock->shouldReceive(
'fclose')
330 $this->expectException(\RuntimeException::class);
331 $this->expectExceptionMessage(
"Unable to seek to stream position \"$offset\" with whence \"$whence\"");
333 $subject->seek($offset);
340 $content =
'awesome content stream';
341 $expectedResult =
"awesome";
346 $subject =
new Stream($resource);
348 $text = $subject->read($length);
349 $this->assertSame($expectedResult, $text);
356 $content =
'awesome content stream';
357 $expectedResult =
"";
362 $subject =
new Stream($resource);
364 $text = $subject->read($length);
365 $this->assertSame($expectedResult, $text);
372 $content =
'awesome content stream';
377 $subject =
new Stream($resource);
380 $this->expectException(\RuntimeException::class);
381 $this->expectExceptionMessage(
'Stream is detached');
383 $subject->read($length);
390 $content =
'awesome content stream';
395 $subject =
new Stream($resource);
397 $this->expectException(\RuntimeException::class);
398 $this->expectExceptionMessage(
'Length parameter must not be negative');
400 $subject->read($length);
407 $content =
'awesome content stream';
412 $subject =
new Stream($resource);
414 $this->expectException(\RuntimeException::class);
415 $this->expectExceptionMessage(
'Can not read from non-readable stream');
417 $subject->read($length);
424 $content =
'awesome content stream';
429 $subject =
new Stream($resource);
432 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
434 $functionMock->shouldReceive(
'fread')
436 ->withArgs([$resource, $length])
439 $functionMock->shouldReceive(
'fclose')
443 $this->expectException(\RuntimeException::class);
444 $this->expectExceptionMessage(
'Unable to read from stream');
446 $subject->read($length);
453 $content =
'awesome content stream';
457 $subject =
new Stream($resource);
459 $text = $subject->getContents();
460 $this->assertSame($content, $text);
467 $content =
'awesome content stream';
471 $subject =
new Stream($resource);
474 $this->expectException(\RuntimeException::class);
475 $this->expectExceptionMessage(
'Stream is detached');
477 $subject->getContents();
484 $content =
'awesome content stream';
488 $subject =
new Stream($resource);
491 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
493 $functionMock->shouldReceive(
'stream_get_contents')
498 $functionMock->shouldReceive(
'fclose')
502 $this->expectException(\RuntimeException::class);
503 $this->expectExceptionMessage(
'Unable to read stream contents');
505 $subject->getContents();
512 $content =
'awesome content stream';
516 $subject =
new Stream($resource);
518 $text = $subject->__toString();
519 $this->assertSame($content, $text);
526 $content =
'awesome content stream';
527 $expectedResult =
'';
531 $subject = Mockery::mock(Stream::class .
'[rewind]', [$resource]);
533 $subject->shouldDeferMissing();
534 $subject->shouldReceive(
'rewind')
536 ->andThrow(\RuntimeException::class);
538 $text = $subject->__toString();
539 $this->assertSame($expectedResult, $text);
546 $content =
'awesome content stream';
548 $byteCount = strlen($newContent);
550 $resource = fopen(
'php://memory', $mode);
553 $subject =
new Stream($resource);
554 $currentSize = $subject->getSize();
556 $numberOfBytesWritten = $subject->write($newContent);
557 $newSize = $subject->getSize();
559 $this->assertSame($byteCount, $numberOfBytesWritten,
'The count of bytes passed to write must match the written bytes after the operation.');
560 $this->assertGreaterThan($currentSize, $newSize,
'The new size must be grater than the old size because we wrote to the stream.');
567 $content =
'awesome content stream';
572 $subject =
new Stream($resource);
575 $this->expectException(\RuntimeException::class);
576 $this->expectExceptionMessage(
'Stream is detached');
578 $subject->write($newContent);
585 $content =
'awesome content stream';
590 $subject =
new Stream($resource);
592 $this->expectException(\RuntimeException::class);
593 $this->expectExceptionMessage(
'Can not write to a non-writable stream');
595 $subject->write($newContent);
602 $content =
'awesome content stream';
607 $subject =
new Stream($resource);
610 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
612 $functionMock->shouldReceive(
'fwrite')
614 ->withArgs([$resource, $newContent])
617 $functionMock->shouldReceive(
'fclose')
621 $this->expectException(\RuntimeException::class);
622 $this->expectExceptionMessage(
'Unable to write to stream');
624 $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()