diff --git a/src/Stream.php b/src/Stream.php index 575ada3..f525aff 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -48,16 +48,6 @@ public function __construct($resource) $this->resource = $resource; } - /** - * Checks if the stream is resourceable - * - * @return bool - */ - public function isResourceable() : bool - { - return \is_resource($this->resource); - } - /** * Detaches a resource from the stream * @@ -83,7 +73,7 @@ public function detach() */ public function close() : void { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { return; } @@ -102,7 +92,7 @@ public function close() : void */ public function eof() : bool { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { return true; } @@ -121,7 +111,7 @@ public function eof() : bool */ public function tell() : int { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { throw new Exception\UntellableStreamException('Stream is not resourceable'); } @@ -143,7 +133,7 @@ public function tell() : int */ public function isSeekable() : bool { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { return false; } @@ -164,7 +154,7 @@ public function isSeekable() : bool */ public function rewind() : void { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { throw new Exception\UnseekableStreamException('Stream is not resourceable'); } @@ -196,7 +186,7 @@ public function rewind() : void */ public function seek($offset, $whence = \SEEK_SET) : void { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { throw new Exception\UnseekableStreamException('Stream is not resourceable'); } @@ -221,7 +211,7 @@ public function seek($offset, $whence = \SEEK_SET) : void */ public function isWritable() : bool { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { return false; } @@ -246,7 +236,7 @@ public function isWritable() : bool */ public function write($string) : int { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { throw new Exception\UnwritableStreamException('Stream is not resourceable'); } @@ -266,37 +256,6 @@ public function write($string) : int return $result; } - /** - * Truncates the stream to the given length - * - * @param int $length - * - * @return void - * - * @throws Exception\UnwritableStreamException - * - * @link http://php.net/manual/en/function.ftruncate.php - */ - public function truncate(int $length = 0) : void - { - if (! $this->isResourceable()) - { - throw new Exception\UnwritableStreamException('Stream is not resourceable'); - } - - if (! $this->isWritable()) - { - throw new Exception\UnwritableStreamException('Stream is not writable'); - } - - $result = \ftruncate($this->resource, $length); - - if (false === $result) - { - throw new Exception\UnwritableStreamException('Unable to truncate the stream'); - } - } - /** * Checks if the stream is readable * @@ -304,7 +263,7 @@ public function truncate(int $length = 0) : void */ public function isReadable() : bool { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { return false; } @@ -327,7 +286,7 @@ public function isReadable() : bool */ public function read($length) : string { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { throw new Exception\UnreadableStreamException('Stream is not resourceable'); } @@ -358,7 +317,7 @@ public function read($length) : string */ public function getContents() : string { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { throw new Exception\UnreadableStreamException('Stream is not resourceable'); } @@ -389,7 +348,7 @@ public function getContents() : string */ public function getMetadata($key = null) { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { return null; } @@ -415,7 +374,7 @@ public function getMetadata($key = null) */ public function getSize() : ?int { - if (! $this->isResourceable()) + if (! \is_resource($this->resource)) { return null; } diff --git a/tests/StreamTest.php b/tests/StreamTest.php index 0e67aa5..efc9e10 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -21,19 +21,7 @@ public function testConstructor() $stream = new Stream($handle); $this->assertInstanceOf(StreamInterface::class, $stream); - - \fclose($handle); - } - - public function testIsResourceable() - { - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); - - $this->assertTrue($stream->isResourceable()); - - $stream->detach(); - $this->assertFalse($stream->isResourceable()); + $this->assertStreamResourceEquals($stream, $handle); \fclose($handle); } @@ -44,8 +32,7 @@ public function testDetach() $stream = new Stream($handle); $this->assertEquals($handle, $stream->detach()); - - $this->assertFalse($stream->isResourceable()); + $this->assertStreamResourceEquals($stream, null); $this->assertEquals(null, $stream->detach()); \fclose($handle); @@ -57,6 +44,7 @@ public function testClose() $stream = new Stream($handle); $stream->close(); + $this->assertStreamResourceEquals($stream, null); $this->assertFalse(\is_resource($handle)); if (\is_resource($handle)) { @@ -170,26 +158,6 @@ public function testWrite() \fclose($handle); } - public function testTruncate() - { - $string = 'Hello, world!'; - $length = \strlen($string); - - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); - - \fwrite($handle, $string); - $stream->truncate(4); - \rewind($handle); - $this->assertEquals(\substr($string, 0, 4), \fread($handle, $length)); - - $stream->truncate(0); - \rewind($handle); - $this->assertEquals('', \fread($handle, $length)); - - \fclose($handle); - } - public function testIsReadable() { $stream = new Stream(\STDIN); @@ -347,18 +315,6 @@ public function testWriteUnresourceable() $stream->write('0', 1); } - public function testTruncateUnresourceable() - { - $this->expectException(UnwritableStreamException::class); - $this->expectExceptionMessage('Stream is not resourceable'); - - $handle = \fopen('php://memory', 'r+b'); - $stream = new Stream($handle); - - $stream->close(); - $stream->truncate(0); - } - public function testReadUnresourceable() { $this->expectException(UnreadableStreamException::class); @@ -392,15 +348,6 @@ public function testWriteUnwritable() $stream->write('0', 1); } - public function testTruncateUnwritable() - { - $this->expectException(UnwritableStreamException::class); - $this->expectExceptionMessage('Stream is not writable'); - - $stream = new Stream(\STDIN); - $stream->truncate(0); - } - public function testReadUnreadable() { $this->expectException(UnreadableStreamException::class); @@ -463,4 +410,13 @@ public function testExceptions() $this->assertInstanceOf(Exception::class, new UntellableStreamException('')); $this->assertInstanceOf(Exception::class, new UnwritableStreamException('')); } + + private function assertStreamResourceEquals(StreamInterface $stream, $expected) + { + $property = new \ReflectionProperty($stream, 'resource'); + + $property->setAccessible(true); + + return $this->assertEquals($property->getValue($stream), $expected); + } }