5 require_once(
'./libs/composer/vendor/autoload.php');
32 return \fopen(
"data://text/plain,$content", $mode);
42 self::$functions = Mockery::mock();
51 $content =
'awesome content stream';
55 $subject =
new Stream($resource);
56 $detachedResource = $subject->detach();
59 $this->assertTrue(is_resource($detachedResource));
60 $this->assertSame($resource, $detachedResource);
71 $content =
'awesome content stream';
75 $subject =
new Stream($resource);
78 $detachedResource = $subject->detach();
79 $this->assertTrue(is_resource($detachedResource));
82 $detachedResource = $subject->detach();
83 $this->assertNull($detachedResource);
92 $content =
'awesome content stream';
93 $correctSize = strlen($content);
97 $subject =
new Stream($resource);
99 $size = $subject->getSize();
100 $this->assertSame($correctSize,
$size);
109 $content =
'awesome content stream';
117 $size = $subject->getSize();
118 $this->assertSame($correctSize,
$size);
127 $content =
'awesome content stream';
131 $subject =
new Stream($resource);
134 $size = $subject->getSize();
135 $this->assertNull(
$size);
144 $content =
'awesome content stream';
148 $subject =
new Stream($resource);
151 $this->assertFalse(is_resource($resource));
160 $content =
'awesome content stream';
164 $subject =
new Stream($resource);
166 $actualResource = $subject->detach();
169 $this->assertTrue(is_resource($actualResource));
178 $content =
'awesome content stream';
182 fseek($resource, $offset);
184 $subject =
new Stream($resource);
186 $actualPosition = $subject->tell();
187 $this->assertSame($offset, $actualPosition);
196 $content =
'awesome content stream';
200 $subject =
new Stream($resource);
203 $this->expectException(\RuntimeException::class);
204 $this->expectExceptionMessage(
'Stream is detached');
215 $content =
'awesome content stream';
220 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
221 $functionMock->shouldReceive(
'ftell')
226 $functionMock->shouldReceive(
'fclose')
230 $subject =
new Stream($resource);
232 $this->expectException(\RuntimeException::class);
233 $this->expectExceptionMessage(
'Unable to determine stream position');
244 $content =
'awesome content stream';
246 $offset = strlen($content);
248 fseek($resource, $offset);
251 $subject =
new Stream($resource);
253 $endOfFileReached = $subject->eof();
254 $this->assertTrue($endOfFileReached);
263 $content =
'awesome content stream';
267 $subject =
new Stream($resource);
270 $this->expectException(\RuntimeException::class);
271 $this->expectExceptionMessage(
'Stream is detached');
283 $content =
'awesome content stream';
288 $subject =
new Stream($resource);
290 $subject->seek($offset);
291 $this->assertSame($offset, ftell($resource));
300 $content =
'awesome content stream';
305 $subject =
new Stream($resource);
308 $this->expectException(\RuntimeException::class);
309 $this->expectExceptionMessage(
'Stream is detached');
311 $subject->seek($offset);
320 $content =
'awesome content stream';
325 $subjectMock = Mockery::mock(Stream::class .
'[isSeekable]', [$resource]);
328 ->shouldReceive(
'isSeekable')
332 $this->expectException(\RuntimeException::class);
333 $this->expectExceptionMessage(
'Stream is not seekable');
335 $subjectMock->seek($offset);
344 $content =
'awesome content stream';
350 $subject =
new Stream($resource);
353 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
354 $functionMock->shouldReceive(
'fseek')
356 ->withArgs([$resource, $offset, $whence])
359 $functionMock->shouldReceive(
'fclose')
363 $this->expectException(\RuntimeException::class);
364 $this->expectExceptionMessage(
"Unable to seek to stream position \"$offset\" with whence \"$whence\"");
366 $subject->seek($offset);
375 $content =
'awesome content stream';
376 $expectedResult =
"awesome";
381 $subject =
new Stream($resource);
383 $text = $subject->read($length);
384 $this->assertSame($expectedResult,
$text);
393 $content =
'awesome content stream';
394 $expectedResult =
"";
399 $subject =
new Stream($resource);
401 $text = $subject->read($length);
402 $this->assertSame($expectedResult,
$text);
411 $content =
'awesome content stream';
416 $subject =
new Stream($resource);
419 $this->expectException(\RuntimeException::class);
420 $this->expectExceptionMessage(
'Stream is detached');
422 $subject->read($length);
431 $content =
'awesome content stream';
436 $subject =
new Stream($resource);
438 $this->expectException(\RuntimeException::class);
439 $this->expectExceptionMessage(
'Length parameter must not be negative');
441 $subject->read($length);
450 $content =
'awesome content stream';
455 $subject =
new Stream($resource);
457 $this->expectException(\RuntimeException::class);
458 $this->expectExceptionMessage(
'Can not read from non-readable stream');
460 $subject->read($length);
469 $content =
'awesome content stream';
474 $subject =
new Stream($resource);
477 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
479 $functionMock->shouldReceive(
'fread')
481 ->withArgs([$resource, $length])
484 $functionMock->shouldReceive(
'fclose')
488 $this->expectException(\RuntimeException::class);
489 $this->expectExceptionMessage(
'Unable to read from stream');
491 $subject->read($length);
500 $content =
'awesome content stream';
504 $subject =
new Stream($resource);
506 $text = $subject->getContents();
507 $this->assertSame($content,
$text);
516 $content =
'awesome content stream';
520 $subject =
new Stream($resource);
523 $this->expectException(\RuntimeException::class);
524 $this->expectExceptionMessage(
'Stream is detached');
526 $subject->getContents();
535 $content =
'awesome content stream';
539 $subject =
new Stream($resource);
542 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
544 $functionMock->shouldReceive(
'stream_get_contents')
549 $functionMock->shouldReceive(
'fclose')
553 $this->expectException(\RuntimeException::class);
554 $this->expectExceptionMessage(
'Unable to read stream contents');
556 $subject->getContents();
565 $content =
'awesome content stream';
569 $subject =
new Stream($resource);
571 $text = $subject->__toString();
572 $this->assertSame($content,
$text);
583 $content =
'awesome content stream';
584 $expectedResult =
'';
588 $subject = Mockery::mock(Stream::class .
'[rewind]', [$resource]);
590 $subject->shouldDeferMissing();
591 $subject->shouldReceive(
'rewind')
593 ->andThrow(\RuntimeException::class);
595 $text = $subject->__toString();
596 $this->assertSame($expectedResult,
$text);
605 $content =
'awesome content stream';
607 $byteCount = strlen($newContent);
609 $resource = fopen(
'php://memory', $mode);
612 $subject =
new Stream($resource);
613 $currentSize = $subject->getSize();
615 $numberOfBytesWritten = $subject->write($newContent);
616 $newSize = $subject->getSize();
618 $this->assertSame($byteCount, $numberOfBytesWritten,
'The count of bytes passed to write must match the written bytes after the operation.');
619 $this->assertGreaterThan($currentSize, $newSize,
'The new size must be grater than the old size because we wrote to the stream.');
628 $content =
'awesome content stream';
633 $subject =
new Stream($resource);
636 $this->expectException(\RuntimeException::class);
637 $this->expectExceptionMessage(
'Stream is detached');
639 $subject->write($newContent);
648 $content =
'awesome content stream';
653 $subject =
new Stream($resource);
655 $this->expectException(\RuntimeException::class);
656 $this->expectExceptionMessage(
'Can not write to a non-writable stream');
658 $subject->write($newContent);
667 $content =
'awesome content stream';
672 $subject =
new Stream($resource);
675 $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
677 $functionMock->shouldReceive(
'fwrite')
679 ->withArgs([$resource, $newContent])
682 $functionMock->shouldReceive(
'fclose')
686 $this->expectException(\RuntimeException::class);
687 $this->expectExceptionMessage(
'Unable to write to stream');
689 $subject->write($newContent);
testCloseWithDetachedStreamWhichShouldDoNothing()
testTellWhichShouldSucceed()
testWriteWithDetachedStreamWhichShouldFail()
testReadWithNegativeLengthWhichShouldFail()
testReadWithZeroLengthWhichShouldSucceed()
testWriteWithFailingFwriteCallWhichShouldFail()
testGetSizeWithDetachedStreamWhichShouldFail()
testEofWithDetachedStreamWhichShouldFail()
testWriteWhichShouldSucceed()
testTellWithDetachedStreamWhichShouldFail()
testSeekWithFseekFailureWhichShouldFail()
testCloseWhichShouldSucceed()
testDetachWhichShouldSucceed()
testWriteWithReadOnlyStreamWhichShouldFail()
testDetachDoubleInvocationWhichShouldFail()
testReadWithFailingFreadCallWhichShouldFail()
testToStringWithErrorWhichShouldSucceed()
testSeekWhichShouldSucceed()
testTellWithFtellFailureWhichShouldFail()
static fwrite($handle, $string, $length=null)
fwrite wrapper
testReadWithUnreadableStreamWhichShouldFail()
testGetContentsWhichShouldSucceed()
testEofWhichShouldSucceed()
testGetSizeWithOptionsWhichShouldSucceed()
createResource($content, $mode)
testReadWhichShouldSucceed()
testToStringWhichShouldSucceed()
testGetContentsWithDetachedStreamWhichShouldFail()
testGetContentsWithFailingStreamGetContentsCallWhichShouldFail()
testSeekWithNotSeekableStreamWhichShouldFail()
testGetSizeWithStatsWhichShouldSucceed()
testSeekWithDetachedStreamWhichShouldFail()
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
testReadWithDetachedStreamWhichShouldFail()