7use PHPUnit\Framework\TestCase;
 
   42        return \fopen(
"data://text/plain,$content", $mode);
 
   48    protected function setUp(): void
 
   52        self::$functions = Mockery::mock();
 
   61        $content = 
'awesome content stream';
 
   65        $subject = 
new Stream($resource);
 
   66        $detachedResource = $subject->detach();
 
   69        $this->assertTrue(is_resource($detachedResource));
 
   70        $this->assertSame($resource, $detachedResource);
 
   81        $content = 
'awesome content stream';
 
   85        $subject = 
new Stream($resource);
 
   88        $detachedResource = $subject->detach();
 
   89        $this->assertTrue(is_resource($detachedResource));
 
   92        $detachedResource = $subject->detach();
 
   93        $this->assertNull($detachedResource);
 
  102        $content = 
'awesome content stream';
 
  103        $correctSize = strlen($content);
 
  107        $subject = 
new Stream($resource);
 
  109        $size = $subject->getSize();
 
  110        $this->assertSame($correctSize, $size);
 
  119        $content = 
'awesome content stream';
 
  125        $subject = 
new Stream($resource, $options);
 
  127        $size = $subject->getSize();
 
  128        $this->assertSame($correctSize, $size);
 
  137        $content = 
'awesome content stream';
 
  141        $subject = 
new Stream($resource);
 
  144        $size = $subject->getSize();
 
  145        $this->assertNull($size);
 
  154        $content = 
'awesome content stream';
 
  158        $subject = 
new Stream($resource);
 
  161        $this->assertFalse(is_resource($resource));
 
  170        $content = 
'awesome content stream';
 
  174        $subject = 
new Stream($resource);
 
  176        $actualResource = $subject->detach();
 
  179        $this->assertTrue(is_resource($actualResource));
 
  188        $content = 
'awesome content stream';
 
  192        fseek($resource, $offset);
 
  194        $subject = 
new Stream($resource);
 
  196        $actualPosition = $subject->tell();
 
  197        $this->assertSame($offset, $actualPosition);
 
  206        $content = 
'awesome content stream';
 
  210        $subject = 
new Stream($resource);
 
  213        $this->expectException(\RuntimeException::class);
 
  214        $this->expectExceptionMessage(
'Stream is detached');
 
  225        $content = 
'awesome content stream';
 
  230        $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
 
  231        $functionMock->shouldReceive(
'ftell')
 
  236        $functionMock->shouldReceive(
'fclose')
 
  240        $subject = 
new Stream($resource);
 
  242        $this->expectException(\RuntimeException::class);
 
  243        $this->expectExceptionMessage(
'Unable to determine stream position');
 
  254        $content = 
'awesome content stream';
 
  256        $offset = strlen($content); 
 
  258        fseek($resource, $offset);  
 
  261        $subject = 
new Stream($resource);
 
  263        $endOfFileReached = $subject->eof();
 
  264        $this->assertTrue($endOfFileReached);
 
  273        $content = 
'awesome content stream';
 
  277        $subject = 
new Stream($resource);
 
  280        $this->expectException(\RuntimeException::class);
 
  281        $this->expectExceptionMessage(
'Stream is detached');
 
  293        $content = 
'awesome content stream';
 
  298        $subject = 
new Stream($resource);
 
  300        $subject->seek($offset);
 
  301        $this->assertSame($offset, ftell($resource));
 
  310        $content = 
'awesome content stream';
 
  315        $subject = 
new Stream($resource);
 
  318        $this->expectException(\RuntimeException::class);
 
  319        $this->expectExceptionMessage(
'Stream is detached');
 
  321        $subject->seek($offset);
 
  330        $content = 
'awesome content stream';
 
  335        $subjectMock = Mockery::mock(Stream::class . 
'[isSeekable]', [$resource]);
 
  338            ->shouldReceive(
'isSeekable')
 
  342        $this->expectException(\RuntimeException::class);
 
  343        $this->expectExceptionMessage(
'Stream is not seekable');
 
  345        $subjectMock->seek($offset);
 
  354        $content = 
'awesome content stream';
 
  360        $subject = 
new Stream($resource);
 
  363        $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
 
  364        $functionMock->shouldReceive(
'fseek')
 
  366            ->withArgs([$resource, $offset, $whence])
 
  369        $functionMock->shouldReceive(
'fclose')
 
  373        $this->expectException(\RuntimeException::class);
 
  374        $this->expectExceptionMessage(
"Unable to seek to stream position \"$offset\" with whence \"$whence\"");
 
  376        $subject->seek($offset);
 
  385        $content = 
'awesome content stream';
 
  386        $expectedResult = 
"awesome";
 
  391        $subject = 
new Stream($resource);
 
  393        $text = $subject->read($length);
 
  394        $this->assertSame($expectedResult, $text);
 
  403        $content = 
'awesome content stream';
 
  404        $expectedResult = 
"";
 
  409        $subject = 
new Stream($resource);
 
  411        $text = $subject->read($length);
 
  412        $this->assertSame($expectedResult, $text);
 
  421        $content = 
'awesome content stream';
 
  426        $subject = 
new Stream($resource);
 
  429        $this->expectException(\RuntimeException::class);
 
  430        $this->expectExceptionMessage(
'Stream is detached');
 
  432        $subject->read($length);
 
  441        $content = 
'awesome content stream';
 
  446        $subject = 
new Stream($resource);
 
  448        $this->expectException(\RuntimeException::class);
 
  449        $this->expectExceptionMessage(
'Length parameter must not be negative');
 
  451        $subject->read($length);
 
  460        $content = 
'awesome content stream';
 
  465        $subject = 
new Stream($resource);
 
  467        $this->expectException(\RuntimeException::class);
 
  468        $this->expectExceptionMessage(
'Can not read from non-readable stream');
 
  470        $subject->read($length);
 
  479        $content = 
'awesome content stream';
 
  484        $subject = 
new Stream($resource);
 
  487        $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
 
  489        $functionMock->shouldReceive(
'fread')
 
  491            ->withArgs([$resource, $length])
 
  494        $functionMock->shouldReceive(
'fclose')
 
  498        $this->expectException(\RuntimeException::class);
 
  499        $this->expectExceptionMessage(
'Unable to read from stream');
 
  501        $subject->read($length);
 
  510        $content = 
'awesome content stream';
 
  514        $subject = 
new Stream($resource);
 
  516        $text = $subject->getContents();
 
  517        $this->assertSame($content, $text);
 
  526        $content = 
'awesome content stream';
 
  530        $subject = 
new Stream($resource);
 
  533        $this->expectException(\RuntimeException::class);
 
  534        $this->expectExceptionMessage(
'Stream is detached');
 
  536        $subject->getContents();
 
  545        $content = 
'awesome content stream';
 
  549        $subject = 
new Stream($resource);
 
  552        $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
 
  554        $functionMock->shouldReceive(
'stream_get_contents')
 
  559        $functionMock->shouldReceive(
'fclose')
 
  563        $this->expectException(\RuntimeException::class);
 
  564        $this->expectExceptionMessage(
'Unable to read stream contents');
 
  566        $subject->getContents();
 
  575        $content = 
'awesome content stream';
 
  579        $subject = 
new Stream($resource);
 
  581        $text = $subject->__toString();
 
  582        $this->assertSame($content, $text);
 
  593        $content = 
'awesome content stream';
 
  594        $expectedResult = 
'';
 
  598        $subject = Mockery::mock(Stream::class . 
'[rewind]', [$resource]);
 
  600        $subject->shouldDeferMissing();
 
  601        $subject->shouldReceive(
'rewind')
 
  603            ->andThrow(\RuntimeException::class);
 
  605        $text = $subject->__toString();
 
  606        $this->assertSame($expectedResult, $text);
 
  615        $content = 
'awesome content stream';
 
  617        $byteCount = strlen($newContent);
 
  619        $resource = fopen(
'php://memory', $mode);
 
  622        $subject = 
new Stream($resource);
 
  623        $currentSize = $subject->getSize();
 
  625        $numberOfBytesWritten = $subject->write($newContent);
 
  626        $newSize = $subject->getSize();
 
  628        $this->assertSame($byteCount, $numberOfBytesWritten, 
'The count of bytes passed to write must match the written bytes after the operation.');
 
  629        $this->assertGreaterThan($currentSize, $newSize, 
'The new size must be grater than the old size because we wrote to the stream.');
 
  638        $content = 
'awesome content stream';
 
  643        $subject = 
new Stream($resource);
 
  646        $this->expectException(\RuntimeException::class);
 
  647        $this->expectExceptionMessage(
'Stream is detached');
 
  649        $subject->write($newContent);
 
  658        $content = 
'awesome content stream';
 
  663        $subject = 
new Stream($resource);
 
  665        $this->expectException(\RuntimeException::class);
 
  666        $this->expectExceptionMessage(
'Can not write to a non-writable stream');
 
  668        $subject->write($newContent);
 
  677        $content = 
'awesome content stream';
 
  682        $subject = 
new Stream($resource);
 
  685        $functionMock = Mockery::mock(
'alias:' . PHPStreamFunctions::class);
 
  687        $functionMock->shouldReceive(
'fwrite')
 
  689            ->withArgs([$resource, $newContent])
 
  692        $functionMock->shouldReceive(
'fclose')
 
  696        $this->expectException(\RuntimeException::class);
 
  697        $this->expectExceptionMessage(
'Unable to write to stream');
 
  699        $subject->write($newContent);
 
testDetachDoubleInvocationWhichShouldFail()
@Test @small
 
testGetSizeWithDetachedStreamWhichShouldFail()
@Test @small
 
testDetachWhichShouldSucceed()
@Test @small
 
testEofWhichShouldSucceed()
@Test @small
 
testWriteWhichShouldSucceed()
@Test @small
 
testGetContentsWithFailingStreamGetContentsCallWhichShouldFail()
@Test @small
 
testGetSizeWithOptionsWhichShouldSucceed()
@Test @small
 
testSeekWithNotSeekableStreamWhichShouldFail()
@Test @small
 
testWriteWithFailingFwriteCallWhichShouldFail()
@Test @small
 
testSeekWithFseekFailureWhichShouldFail()
@Test @small
 
testTellWithFtellFailureWhichShouldFail()
@Test @small
 
testCloseWhichShouldSucceed()
@Test @small
 
testEofWithDetachedStreamWhichShouldFail()
@Test @small
 
testToStringWhichShouldSucceed()
@Test @small
 
testTellWhichShouldSucceed()
@Test @small
 
testReadWithFailingFreadCallWhichShouldFail()
@Test @small
 
testReadWithZeroLengthWhichShouldSucceed()
@Test @small
 
testToStringWithErrorWhichShouldSucceed()
@Test @small
 
testWriteWithReadOnlyStreamWhichShouldFail()
@Test @small
 
testGetContentsWithDetachedStreamWhichShouldFail()
@Test @small
 
testCloseWithDetachedStreamWhichShouldDoNothing()
@Test @small
 
createResource($content, $mode)
 
testTellWithDetachedStreamWhichShouldFail()
@Test @small
 
testReadWhichShouldSucceed()
@Test @small
 
testWriteWithDetachedStreamWhichShouldFail()
@Test @small
 
testReadWithDetachedStreamWhichShouldFail()
@Test @small
 
testGetSizeWithStatsWhichShouldSucceed()
@Test @small
 
testSeekWithDetachedStreamWhichShouldFail()
@Test @small
 
testGetContentsWhichShouldSucceed()
@Test @small
 
testReadWithUnreadableStreamWhichShouldFail()
@Test @small
 
testSeekWhichShouldSucceed()
@Test @small
 
testReadWithNegativeLengthWhichShouldFail()
@Test @small
 
static fwrite($handle, string $string, ?int $length=null)