diff --git a/tests/StreamTest.php b/tests/StreamTest.php index a72cecb..f7c0939 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -13,58 +13,66 @@ class StreamTest extends TestCase { + private $handle; + + public function setUp() + { + $this->handle = \fopen('php://memory', 'r+b'); + } + + public function tearDown() + { + if (\is_resource($this->handle)) + { + \fclose($this->handle); + } + } + public function testConstructor() { - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); $this->assertInstanceOf(StreamInterface::class, $stream); - $this->assertStreamResourceEquals($stream, $handle); + $this->assertStreamResourceEquals($stream, $this->handle); + } + + public function testConstructorWithInvalidResource() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid stream resource'); - \fclose($handle); + new Stream(''); } public function testDetach() { - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); - $this->assertEquals($handle, $stream->detach()); + $this->assertEquals($this->handle, $stream->detach()); $this->assertStreamResourceEquals($stream, null); $this->assertEquals(null, $stream->detach()); - - \fclose($handle); } public function testClose() { - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); $stream->close(); $this->assertStreamResourceEquals($stream, null); - $this->assertFalse(\is_resource($handle)); - - if (\is_resource($handle)) { - \fclose($handle); - } + $this->assertFalse(\is_resource($this->handle)); } public function testEof() { - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); - while (! \feof($handle)) { - \fread($handle, 1024); + while (! \feof($this->handle)) { + \fread($this->handle, 1024); } $this->assertTrue($stream->eof()); - - \rewind($handle); + \rewind($this->handle); $this->assertFalse($stream->eof()); - - \fclose($handle); } public function testTell() @@ -72,32 +80,37 @@ public function testTell() $string = 'Hello, world!'; $length = \strlen($string); - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); - \rewind($handle); + \rewind($this->handle); $this->assertEquals(0, $stream->tell()); - \fwrite($handle, $string, $length); + \fwrite($this->handle, $string, $length); $this->assertEquals($length, $stream->tell()); - \rewind($handle); + \rewind($this->handle); $this->assertEquals(0, $stream->tell()); + } - \fclose($handle); + public function testTellUnresourceable() + { + $this->expectException(UntellableStreamException::class); + $this->expectExceptionMessage('Stream is not resourceable'); + + $stream = new Stream($this->handle); + + $stream->close(); + $stream->tell(); } public function testIsSeekable() { - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); $this->assertTrue($stream->isSeekable()); $stream->detach(); $this->assertFalse($stream->isSeekable()); - - \fclose($handle); } public function testRewind() @@ -105,14 +118,22 @@ public function testRewind() $string = 'Hello, world!'; $length = \strlen($string); - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); - \fwrite($handle, $string, $length); + \fwrite($this->handle, $string, $length); $stream->rewind(); - $this->assertEquals(0, \ftell($handle)); + $this->assertEquals(0, \ftell($this->handle)); + } + + public function testRewindUnresourceable() + { + $this->expectException(UnseekableStreamException::class); + $this->expectExceptionMessage('Stream is not resourceable'); + + $stream = new Stream($this->handle); - \fclose($handle); + $stream->close(); + $stream->rewind(); } public function testSeek() @@ -120,15 +141,23 @@ public function testSeek() $string = 'Hello, world!'; $length = \strlen($string); - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); - \fwrite($handle, $string, $length); - \rewind($handle); + \fwrite($this->handle, $string, $length); + \rewind($this->handle); $stream->seek($length, \SEEK_SET); - $this->assertEquals($length, \ftell($handle)); + $this->assertEquals($length, \ftell($this->handle)); + } + + public function testSeekUnresourceable() + { + $this->expectException(UnseekableStreamException::class); + $this->expectExceptionMessage('Stream is not resourceable'); + + $stream = new Stream($this->handle); - \fclose($handle); + $stream->close(); + $stream->seek(0, \SEEK_SET); } public function testIsWritable() @@ -145,15 +174,32 @@ public function testWrite() $string = 'Hello, world!'; $length = \strlen($string); - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); $this->assertEquals($length, $stream->write($string)); - \rewind($handle); - $this->assertEquals($string, \fread($handle, $length)); + \rewind($this->handle); + $this->assertEquals($string, \fread($this->handle, $length)); + } + + public function testWriteUnresourceable() + { + $this->expectException(UnwritableStreamException::class); + $this->expectExceptionMessage('Stream is not resourceable'); + + $stream = new Stream($this->handle); + + $stream->close(); + $stream->write('0', 1); + } + + public function testWriteUnwritable() + { + $this->expectException(UnwritableStreamException::class); + $this->expectExceptionMessage('Stream is not writable'); - \fclose($handle); + $stream = new Stream(\STDIN); + $stream->write('0', 1); } public function testIsReadable() @@ -170,222 +216,134 @@ public function testRead() $string = 'Hello, world!'; $length = \strlen($string); - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); - \fwrite($handle, $string); - \rewind($handle); + \fwrite($this->handle, $string); + \rewind($this->handle); $this->assertEquals($string, $stream->read($length)); - - \fclose($handle); - } - - public function testGetContents() - { - $string = 'Hello, world!'; - $length = \strlen($string); - - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); - - \fwrite($handle, $string); - \rewind($handle); - $this->assertEquals($string, $stream->getContents()); - - \fclose($handle); - } - - public function testGetMetadata() - { - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); - - $this->assertEquals( - \stream_get_meta_data($handle), - $stream->getMetadata() - ); - - \fclose($handle); } - public function testGetMetadataWithKey() + public function testReadUnresourceable() { - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); - - $this->assertEquals( - 'php://memory', - $stream->getMetadata('uri') - ); + $this->expectException(UnreadableStreamException::class); + $this->expectExceptionMessage('Stream is not resourceable'); - $this->assertEquals( - null, - $stream->getMetadata('undefined') - ); + $stream = new Stream($this->handle); - \fclose($handle); + $stream->close(); + $stream->read(1); } - public function testGetSize() + public function testReadUnreadable() { - $string = 'Hello, world!'; - $length = \strlen($string); - - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); - - \fwrite($handle, $string); - $this->assertEquals($length, $stream->getSize()); - - \ftruncate($handle, 0); - $this->assertEquals(0, $stream->getSize()); + $this->expectException(UnreadableStreamException::class); + $this->expectExceptionMessage('Stream is not readable'); - \fclose($handle); + $stream = new Stream(\STDOUT); + $stream->read(1); } - public function testToString() + public function testGetContents() { $string = 'Hello, world!'; $length = \strlen($string); - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); - - \fwrite($handle, $string); - $this->assertEquals($string, (string) $stream); - - \fclose($handle); - } - - public function testConstructorWithInvalidResource() - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Invalid stream resource'); - - new Stream(''); - } - - public function testTellUnresourceable() - { - $this->expectException(UntellableStreamException::class); - $this->expectExceptionMessage('Stream is not resourceable'); + $stream = new Stream($this->handle); - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); - - $stream->close(); - $stream->tell(); + \fwrite($this->handle, $string); + \rewind($this->handle); + $this->assertEquals($string, $stream->getContents()); } - public function testRewindUnresourceable() + public function testGetContentsUnresourceable() { - $this->expectException(UnseekableStreamException::class); + $this->expectException(UnreadableStreamException::class); $this->expectExceptionMessage('Stream is not resourceable'); - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); $stream->close(); - $stream->rewind(); + $stream->getContents(); } - public function testSeekUnresourceable() + public function testGetContentsUnreadable() { - $this->expectException(UnseekableStreamException::class); - $this->expectExceptionMessage('Stream is not resourceable'); - - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $this->expectException(UnreadableStreamException::class); + $this->expectExceptionMessage('Stream is not readable'); - $stream->close(); - $stream->seek(0, \SEEK_SET); + $stream = new Stream(\STDOUT); + $stream->getContents(); } - public function testWriteUnresourceable() + public function testGetMetadata() { - $this->expectException(UnwritableStreamException::class); - $this->expectExceptionMessage('Stream is not resourceable'); + $stream = new Stream($this->handle); - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); - - $stream->close(); - $stream->write('0', 1); + $this->assertEquals( + \stream_get_meta_data($this->handle), + $stream->getMetadata() + ); } - public function testReadUnresourceable() + public function testGetMetadataWithKey() { - $this->expectException(UnreadableStreamException::class); - $this->expectExceptionMessage('Stream is not resourceable'); + $stream = new Stream($this->handle); - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $this->assertEquals( + 'php://memory', + $stream->getMetadata('uri') + ); - $stream->close(); - $stream->read(1); + $this->assertEquals( + null, + $stream->getMetadata('undefined') + ); } - public function testGetContentsUnresourceable() + public function testGetMetadataUnresourceable() { - $this->expectException(UnreadableStreamException::class); - $this->expectExceptionMessage('Stream is not resourceable'); - - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); $stream->close(); - $stream->getContents(); - } - - public function testWriteUnwritable() - { - $this->expectException(UnwritableStreamException::class); - $this->expectExceptionMessage('Stream is not writable'); - - $stream = new Stream(\STDIN); - $stream->write('0', 1); + $this->assertEquals(null, $stream->getMetadata()); } - public function testReadUnreadable() + public function testGetSize() { - $this->expectException(UnreadableStreamException::class); - $this->expectExceptionMessage('Stream is not readable'); + $string = 'Hello, world!'; + $length = \strlen($string); - $stream = new Stream(\STDOUT); - $stream->read(1); - } + $stream = new Stream($this->handle); - public function testGetContentsUnreadable() - { - $this->expectException(UnreadableStreamException::class); - $this->expectExceptionMessage('Stream is not readable'); + \fwrite($this->handle, $string); + $this->assertEquals($length, $stream->getSize()); - $stream = new Stream(\STDOUT); - $stream->getContents(); + \ftruncate($this->handle, 0); + $this->assertEquals(0, $stream->getSize()); } - public function testGetMetadataUnresourceable() + public function testGetSizeUnresourceable() { - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); $stream->close(); - $this->assertEquals(null, $stream->getMetadata()); + $this->assertEquals(null, $stream->getSize()); } - public function testGetSizeUnresourceable() + public function testToString() { - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $string = 'Hello, world!'; + $length = \strlen($string); - $stream->close(); - $this->assertEquals(null, $stream->getSize()); + $stream = new Stream($this->handle); + + \fwrite($this->handle, $string); + $this->assertEquals($string, (string) $stream); } public function testToStringUnresourceable() { - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); + $stream = new Stream($this->handle); $stream->close(); $this->assertEquals('', (string) $stream);