From 50492ab16518c5e10ad43600a12baca51613d6a8 Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Fri, 25 Dec 2020 09:57:04 +0500 Subject: [PATCH] csf --- .editorconfig | 3 - phpcs.xml.dist | 7 + src/Stream.php | 715 ++++++++++++++++++------------------ src/StreamFactory.php | 66 ++-- tests/StreamFactoryTest.php | 72 ++-- tests/StreamTest.php | 590 ++++++++++++++--------------- 6 files changed, 725 insertions(+), 728 deletions(-) create mode 100644 phpcs.xml.dist diff --git a/.editorconfig b/.editorconfig index 2da7375..27e2667 100644 --- a/.editorconfig +++ b/.editorconfig @@ -14,8 +14,5 @@ insert_final_newline = true [*.md] trim_trailing_whitespace = false -[*.php] -indent_style = tab - [*.yml] indent_size = 2 diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..be45cac --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,7 @@ + + + + + src + tests + diff --git a/src/Stream.php b/src/Stream.php index 1748dcc..f84ae92 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -24,365 +24,358 @@ class Stream implements StreamInterface { - /** - * Resource of the stream - * - * @var resource - */ - protected $resource; - - /** - * Constructor of the class - * - * @param resource $resource - * - * @throws \InvalidArgumentException - */ - public function __construct($resource) - { - if (! \is_resource($resource)) { - throw new \InvalidArgumentException('Invalid stream resource'); - } - - $this->resource = $resource; - } - - /** - * Detaches a resource from the stream - * - * Returns NULL if the stream already without a resource. - * - * @return null|resource - */ - public function detach() - { - $resource = $this->resource; - - $this->resource = null; - - return $resource; - } - - /** - * Closes the stream - * - * @return void - * - * @link http://php.net/manual/en/function.fclose.php - */ - public function close() : void - { - if (! \is_resource($this->resource)) { - return; - } - - $resource = $this->detach(); - - \fclose($resource); - } - - /** - * Checks if the end of the stream is reached - * - * @return bool - * - * @link http://php.net/manual/en/function.feof.php - */ - public function eof() : bool - { - if (! \is_resource($this->resource)) { - return true; - } - - return \feof($this->resource); - } - - /** - * Gets the stream pointer position - * - * @return int - * - * @throws Exception\UntellableStreamException - * - * @link http://php.net/manual/en/function.ftell.php - */ - public function tell() : int - { - if (! \is_resource($this->resource)) { - throw new Exception\UntellableStreamException('Stream is not resourceable'); - } - - $result = \ftell($this->resource); - - if (false === $result) { - throw new Exception\UntellableStreamException('Unable to get the stream pointer position'); - } - - return $result; - } - - /** - * Checks if the stream is seekable - * - * @return bool - */ - public function isSeekable() : bool - { - if (! \is_resource($this->resource)) { - return false; - } - - $metadata = \stream_get_meta_data($this->resource); - - return $metadata['seekable']; - } - - /** - * Moves the stream pointer to begining - * - * @return void - * - * @throws Exception\UnseekableStreamException - * - * @link http://php.net/manual/en/function.rewind.php - */ - public function rewind() : void - { - if (! \is_resource($this->resource)) { - throw new Exception\UnseekableStreamException('Stream is not resourceable'); - } - - if (! $this->isSeekable()) { - throw new Exception\UnseekableStreamException('Stream is not seekable'); - } - - $result = \fseek($this->resource, 0, \SEEK_SET); - - if (! (0 === $result)) { - throw new Exception\UnseekableStreamException('Unable to move the stream pointer to beginning'); - } - } - - /** - * Moves the stream pointer to the given position - * - * @param int $offset - * @param int $whence - * - * @return void - * - * @throws Exception\UnseekableStreamException - * - * @link http://php.net/manual/en/function.fseek.php - */ - public function seek($offset, $whence = \SEEK_SET) : void - { - if (! \is_resource($this->resource)) { - throw new Exception\UnseekableStreamException('Stream is not resourceable'); - } - - if (! $this->isSeekable()) { - throw new Exception\UnseekableStreamException('Stream is not seekable'); - } - - $result = \fseek($this->resource, $offset, $whence); - - if (! (0 === $result)) { - throw new Exception\UnseekableStreamException('Unable to move the stream pointer to the given position'); - } - } - - /** - * Checks if the stream is writable - * - * @return bool - */ - public function isWritable() : bool - { - if (! \is_resource($this->resource)) { - return false; - } - - $metadata = \stream_get_meta_data($this->resource); - - return ! (false === \strpbrk($metadata['mode'], '+acwx')); - } - - /** - * Writes the given string to the stream - * - * Returns the number of bytes written to the stream. - * - * @param string $string - * - * @return int - * - * @throws Exception\UnwritableStreamException - * - * @link http://php.net/manual/en/function.fwrite.php - */ - public function write($string) : int - { - if (! \is_resource($this->resource)) { - throw new Exception\UnwritableStreamException('Stream is not resourceable'); - } - - if (! $this->isWritable()) { - throw new Exception\UnwritableStreamException('Stream is not writable'); - } - - $result = \fwrite($this->resource, $string); - - if (false === $result) { - throw new Exception\UnwritableStreamException('Unable to write to the stream'); - } - - return $result; - } - - /** - * Checks if the stream is readable - * - * @return bool - */ - public function isReadable() : bool - { - if (! \is_resource($this->resource)) { - return false; - } - - $metadata = \stream_get_meta_data($this->resource); - - return ! (false === \strpbrk($metadata['mode'], '+r')); - } - - /** - * Reads the given number of bytes from the stream - * - * @param int $length - * - * @return string - * - * @throws Exception\UnreadableStreamException - * - * @link http://php.net/manual/en/function.fread.php - */ - public function read($length) : string - { - if (! \is_resource($this->resource)) { - throw new Exception\UnreadableStreamException('Stream is not resourceable'); - } - - if (! $this->isReadable()) { - throw new Exception\UnreadableStreamException('Stream is not readable'); - } - - $result = \fread($this->resource, $length); - - if (false === $result) { - throw new Exception\UnreadableStreamException('Unable to read from the stream'); - } - - return $result; - } - - /** - * Reads remainder of the stream - * - * @return string - * - * @throws Exception\UnreadableStreamException - * - * @link http://php.net/manual/en/function.stream-get-contents.php - */ - public function getContents() : string - { - if (! \is_resource($this->resource)) { - throw new Exception\UnreadableStreamException('Stream is not resourceable'); - } - - if (! $this->isReadable()) { - throw new Exception\UnreadableStreamException('Stream is not readable'); - } - - $result = \stream_get_contents($this->resource); - - if (false === $result) { - throw new Exception\UnreadableStreamException('Unable to read remainder of the stream'); - } - - return $result; - } - - /** - * Gets the stream metadata - * - * @param string $key - * - * @return mixed - * - * @link http://php.net/manual/en/function.stream-get-meta-data.php - */ - public function getMetadata($key = null) - { - if (! \is_resource($this->resource)) { - return null; - } - - $metadata = \stream_get_meta_data($this->resource); - - if (! (null === $key)) { - return $metadata[$key] ?? null; - } - - return $metadata; - } - - /** - * Gets the stream size - * - * Returns NULL if the stream without a resource, or if the stream size cannot be determined. - * - * @return null|int - * - * @link http://php.net/manual/en/function.fstat.php - */ - public function getSize() : ?int - { - if (! \is_resource($this->resource)) { - return null; - } - - $stats = \fstat($this->resource); - - if (false === $stats) { - return null; - } - - return $stats['size']; - } - - /** - * Converts the stream to string - * - * @return string - * - * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring - */ - public function __toString() - { - try { - if ($this->isReadable()) { - if ($this->isSeekable()) { - $this->rewind(); - } - - return $this->getContents(); - } - } catch (\Throwable $e) { - // ignore... - } - - return ''; - } + /** + * Resource of the stream + * + * @var resource + */ + protected $resource; + + /** + * Constructor of the class + * + * @param resource $resource + * + * @throws \InvalidArgumentException + */ + public function __construct($resource) + { + if (! \is_resource($resource)) { + throw new \InvalidArgumentException('Invalid stream resource'); + } + + $this->resource = $resource; + } + + /** + * Detaches a resource from the stream + * + * Returns NULL if the stream already without a resource. + * + * @return null|resource + */ + public function detach() + { + $resource = $this->resource; + $this->resource = null; + + return $resource; + } + + /** + * Closes the stream + * + * @return void + * + * @link http://php.net/manual/en/function.fclose.php + */ + public function close() : void + { + if (! \is_resource($this->resource)) { + return; + } + + $resource = $this->detach(); + \fclose($resource); + } + + /** + * Checks if the end of the stream is reached + * + * @return bool + * + * @link http://php.net/manual/en/function.feof.php + */ + public function eof() : bool + { + if (! \is_resource($this->resource)) { + return true; + } + + return \feof($this->resource); + } + + /** + * Gets the stream pointer position + * + * @return int + * + * @throws Exception\UntellableStreamException + * + * @link http://php.net/manual/en/function.ftell.php + */ + public function tell() : int + { + if (! \is_resource($this->resource)) { + throw new Exception\UntellableStreamException('Stream is not resourceable'); + } + + $result = \ftell($this->resource); + if (false === $result) { + throw new Exception\UntellableStreamException('Unable to get the stream pointer position'); + } + + return $result; + } + + /** + * Checks if the stream is seekable + * + * @return bool + */ + public function isSeekable() : bool + { + if (! \is_resource($this->resource)) { + return false; + } + + $metadata = \stream_get_meta_data($this->resource); + + return $metadata['seekable']; + } + + /** + * Moves the stream pointer to begining + * + * @return void + * + * @throws Exception\UnseekableStreamException + * + * @link http://php.net/manual/en/function.rewind.php + */ + public function rewind() : void + { + if (! \is_resource($this->resource)) { + throw new Exception\UnseekableStreamException('Stream is not resourceable'); + } + + if (! $this->isSeekable()) { + throw new Exception\UnseekableStreamException('Stream is not seekable'); + } + + $result = \fseek($this->resource, 0, \SEEK_SET); + + if (! (0 === $result)) { + throw new Exception\UnseekableStreamException('Unable to move the stream pointer to beginning'); + } + } + + /** + * Moves the stream pointer to the given position + * + * @param int $offset + * @param int $whence + * + * @return void + * + * @throws Exception\UnseekableStreamException + * + * @link http://php.net/manual/en/function.fseek.php + */ + public function seek($offset, $whence = \SEEK_SET) : void + { + if (! \is_resource($this->resource)) { + throw new Exception\UnseekableStreamException('Stream is not resourceable'); + } + + if (! $this->isSeekable()) { + throw new Exception\UnseekableStreamException('Stream is not seekable'); + } + + $result = \fseek($this->resource, $offset, $whence); + + if (! (0 === $result)) { + throw new Exception\UnseekableStreamException('Unable to move the stream pointer to the given position'); + } + } + + /** + * Checks if the stream is writable + * + * @return bool + */ + public function isWritable() : bool + { + if (! \is_resource($this->resource)) { + return false; + } + + $metadata = \stream_get_meta_data($this->resource); + + return ! (false === \strpbrk($metadata['mode'], '+acwx')); + } + + /** + * Writes the given string to the stream + * + * Returns the number of bytes written to the stream. + * + * @param string $string + * + * @return int + * + * @throws Exception\UnwritableStreamException + * + * @link http://php.net/manual/en/function.fwrite.php + */ + public function write($string) : int + { + if (! \is_resource($this->resource)) { + throw new Exception\UnwritableStreamException('Stream is not resourceable'); + } + + if (! $this->isWritable()) { + throw new Exception\UnwritableStreamException('Stream is not writable'); + } + + $result = \fwrite($this->resource, $string); + + if (false === $result) { + throw new Exception\UnwritableStreamException('Unable to write to the stream'); + } + + return $result; + } + + /** + * Checks if the stream is readable + * + * @return bool + */ + public function isReadable() : bool + { + if (! \is_resource($this->resource)) { + return false; + } + + $metadata = \stream_get_meta_data($this->resource); + + return ! (false === \strpbrk($metadata['mode'], '+r')); + } + + /** + * Reads the given number of bytes from the stream + * + * @param int $length + * + * @return string + * + * @throws Exception\UnreadableStreamException + * + * @link http://php.net/manual/en/function.fread.php + */ + public function read($length) : string + { + if (! \is_resource($this->resource)) { + throw new Exception\UnreadableStreamException('Stream is not resourceable'); + } + + if (! $this->isReadable()) { + throw new Exception\UnreadableStreamException('Stream is not readable'); + } + + $result = \fread($this->resource, $length); + if (false === $result) { + throw new Exception\UnreadableStreamException('Unable to read from the stream'); + } + + return $result; + } + + /** + * Reads remainder of the stream + * + * @return string + * + * @throws Exception\UnreadableStreamException + * + * @link http://php.net/manual/en/function.stream-get-contents.php + */ + public function getContents() : string + { + if (! \is_resource($this->resource)) { + throw new Exception\UnreadableStreamException('Stream is not resourceable'); + } + + if (! $this->isReadable()) { + throw new Exception\UnreadableStreamException('Stream is not readable'); + } + + $result = \stream_get_contents($this->resource); + if (false === $result) { + throw new Exception\UnreadableStreamException('Unable to read remainder of the stream'); + } + + return $result; + } + + /** + * Gets the stream metadata + * + * @param string $key + * + * @return mixed + * + * @link http://php.net/manual/en/function.stream-get-meta-data.php + */ + public function getMetadata($key = null) + { + if (! \is_resource($this->resource)) { + return null; + } + + $metadata = \stream_get_meta_data($this->resource); + if (! (null === $key)) { + return $metadata[$key] ?? null; + } + + return $metadata; + } + + /** + * Gets the stream size + * + * Returns NULL if the stream without a resource, or if the stream size cannot be determined. + * + * @return null|int + * + * @link http://php.net/manual/en/function.fstat.php + */ + public function getSize() : ?int + { + if (! \is_resource($this->resource)) { + return null; + } + + $stats = \fstat($this->resource); + if (false === $stats) { + return null; + } + + return $stats['size']; + } + + /** + * Converts the stream to string + * + * @return string + * + * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring + */ + public function __toString() + { + try { + if ($this->isReadable()) { + if ($this->isSeekable()) { + $this->rewind(); + } + + return $this->getContents(); + } + } catch (\Throwable $e) { + // ignore... + } + + return ''; + } } diff --git a/src/StreamFactory.php b/src/StreamFactory.php index c5f02ed..0096111 100644 --- a/src/StreamFactory.php +++ b/src/StreamFactory.php @@ -25,43 +25,43 @@ class StreamFactory implements StreamFactoryInterface { - /** - * {@inheritDoc} - */ - public function createStream(string $content = '') : StreamInterface - { - $resource = \fopen('php://temp', 'r+b'); + /** + * {@inheritDoc} + */ + public function createStream(string $content = '') : StreamInterface + { + $resource = \fopen('php://temp', 'r+b'); - \fwrite($resource, $content); - \rewind($resource); + \fwrite($resource, $content); + \rewind($resource); - return new Stream($resource); - } + return new Stream($resource); + } - /** - * {@inheritDoc} - * - * @throws Exception\UnopenableStreamException If the given file does not open - */ - public function createStreamFromFile(string $filename, string $mode = 'r') : StreamInterface - { - // See http://php.net/manual/en/function.fopen.php - $resource = @ \fopen($filename, $mode); + /** + * {@inheritDoc} + * + * @throws Exception\UnopenableStreamException If the given file does not open + */ + public function createStreamFromFile(string $filename, string $mode = 'r') : StreamInterface + { + // See http://php.net/manual/en/function.fopen.php + $resource = @ \fopen($filename, $mode); - if (false === $resource) { - throw new Exception\UnopenableStreamException( - \sprintf('Unable to open file "%s" in mode "%s"', $filename, $mode) - ); - } + if (false === $resource) { + throw new Exception\UnopenableStreamException( + \sprintf('Unable to open file "%s" in mode "%s"', $filename, $mode) + ); + } - return new Stream($resource); - } + return new Stream($resource); + } - /** - * {@inheritDoc} - */ - public function createStreamFromResource($resource) : StreamInterface - { - return new Stream($resource); - } + /** + * {@inheritDoc} + */ + public function createStreamFromResource($resource) : StreamInterface + { + return new Stream($resource); + } } diff --git a/tests/StreamFactoryTest.php b/tests/StreamFactoryTest.php index 6112eb4..bfd9bfd 100644 --- a/tests/StreamFactoryTest.php +++ b/tests/StreamFactoryTest.php @@ -10,59 +10,59 @@ class StreamFactoryTest extends TestCase { - public function testConstructor() - { - $factory = new StreamFactory(); + public function testConstructor() + { + $factory = new StreamFactory(); - $this->assertInstanceOf(StreamFactoryInterface::class, $factory); - } + $this->assertInstanceOf(StreamFactoryInterface::class, $factory); + } - public function testCreateStream() - { - $content = 'Hello, world!'; + public function testCreateStream() + { + $content = 'Hello, world!'; - $stream = (new StreamFactory)->createStream($content); + $stream = (new StreamFactory)->createStream($content); - $this->assertInstanceOf(StreamInterface::class, $stream); + $this->assertInstanceOf(StreamInterface::class, $stream); - $this->assertTrue($stream->isReadable()); + $this->assertTrue($stream->isReadable()); - $this->assertTrue($stream->isWritable()); + $this->assertTrue($stream->isWritable()); - $this->assertEquals('php://temp', $stream->getMetadata('uri')); + $this->assertEquals('php://temp', $stream->getMetadata('uri')); - $this->assertEquals(0, $stream->tell()); + $this->assertEquals(0, $stream->tell()); - $this->assertEquals($content, (string) $stream); + $this->assertEquals($content, (string) $stream); - $stream->close(); - } + $stream->close(); + } - public function testCreateStreamFromFile() - { - $stream = (new StreamFactory)->createStreamFromFile('php://memory', 'r+b'); + public function testCreateStreamFromFile() + { + $stream = (new StreamFactory)->createStreamFromFile('php://memory', 'r+b'); - $this->assertInstanceOf(StreamInterface::class, $stream); + $this->assertInstanceOf(StreamInterface::class, $stream); - $stream->close(); - } + $stream->close(); + } - public function testCreateStreamFromResource() - { - $resource = \fopen('php://memory', 'r+b'); + public function testCreateStreamFromResource() + { + $resource = \fopen('php://memory', 'r+b'); - $stream = (new StreamFactory)->createStreamFromResource($resource); + $stream = (new StreamFactory)->createStreamFromResource($resource); - $this->assertInstanceOf(StreamInterface::class, $stream); + $this->assertInstanceOf(StreamInterface::class, $stream); - \fclose($resource); - } + \fclose($resource); + } - public function testCreateStreamFromUnopenableFile() - { - $this->expectException(UnopenableStreamException::class); - $this->expectExceptionMessage(\sprintf('Unable to open file "%s/nonexistent.file" in mode "r"', __DIR__)); + public function testCreateStreamFromUnopenableFile() + { + $this->expectException(UnopenableStreamException::class); + $this->expectExceptionMessage(\sprintf('Unable to open file "%s/nonexistent.file" in mode "r"', __DIR__)); - (new StreamFactory)->createStreamFromFile(__DIR__ . '/nonexistent.file', 'r'); - } + (new StreamFactory)->createStreamFromFile(__DIR__ . '/nonexistent.file', 'r'); + } } diff --git a/tests/StreamTest.php b/tests/StreamTest.php index 268d727..0acd4da 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -13,363 +13,363 @@ class StreamTest extends TestCase { - private $handle; + private $handle; - protected function setUp() : void - { - $this->handle = \fopen('php://memory', 'r+b'); - } + protected function setUp() : void + { + $this->handle = \fopen('php://memory', 'r+b'); + } - protected function tearDown() : void - { - if (\is_resource($this->handle)) { - \fclose($this->handle); - } - } + protected function tearDown() : void + { + if (\is_resource($this->handle)) { + \fclose($this->handle); + } + } - public function testConstructor() - { - $stream = new Stream($this->handle); + public function testConstructor() + { + $stream = new Stream($this->handle); - $this->assertInstanceOf(StreamInterface::class, $stream); - $this->assertStreamResourceEquals($stream, $this->handle); - } + $this->assertInstanceOf(StreamInterface::class, $stream); + $this->assertStreamResourceEquals($stream, $this->handle); + } - public function testConstructorWithInvalidResource() - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Invalid stream resource'); + public function testConstructorWithInvalidResource() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid stream resource'); - new Stream(''); - } + new Stream(''); + } - public function testDetach() - { - $stream = new Stream($this->handle); + public function testDetach() + { + $stream = new Stream($this->handle); - $this->assertEquals($this->handle, $stream->detach()); - $this->assertStreamResourceEquals($stream, null); - $this->assertNull($stream->detach()); - } + $this->assertEquals($this->handle, $stream->detach()); + $this->assertStreamResourceEquals($stream, null); + $this->assertNull($stream->detach()); + } - public function testClose() - { - $stream = new Stream($this->handle); + public function testClose() + { + $stream = new Stream($this->handle); - $stream->close(); - $this->assertStreamResourceEquals($stream, null); - $this->assertFalse(\is_resource($this->handle)); - } + $stream->close(); + $this->assertStreamResourceEquals($stream, null); + $this->assertFalse(\is_resource($this->handle)); + } - public function testEof() - { - $stream = new Stream($this->handle); + public function testEof() + { + $stream = new Stream($this->handle); - while (! \feof($this->handle)) { - \fread($this->handle, 1024); - } + while (! \feof($this->handle)) { + \fread($this->handle, 1024); + } - $this->assertTrue($stream->eof()); - \rewind($this->handle); - $this->assertFalse($stream->eof()); - } + $this->assertTrue($stream->eof()); + \rewind($this->handle); + $this->assertFalse($stream->eof()); + } - public function testTell() - { - $string = 'Hello, world!'; - $length = \strlen($string); + public function testTell() + { + $string = 'Hello, world!'; + $length = \strlen($string); - $stream = new Stream($this->handle); + $stream = new Stream($this->handle); - \rewind($this->handle); - $this->assertEquals(0, $stream->tell()); + \rewind($this->handle); + $this->assertEquals(0, $stream->tell()); - \fwrite($this->handle, $string, $length); - $this->assertEquals($length, $stream->tell()); + \fwrite($this->handle, $string, $length); + $this->assertEquals($length, $stream->tell()); - \rewind($this->handle); - $this->assertEquals(0, $stream->tell()); - } + \rewind($this->handle); + $this->assertEquals(0, $stream->tell()); + } - public function testTellUnresourceable() - { - $this->expectException(UntellableStreamException::class); - $this->expectExceptionMessage('Stream is not resourceable'); + public function testTellUnresourceable() + { + $this->expectException(UntellableStreamException::class); + $this->expectExceptionMessage('Stream is not resourceable'); - $stream = new Stream($this->handle); + $stream = new Stream($this->handle); - $stream->close(); - $stream->tell(); - } + $stream->close(); + $stream->tell(); + } - public function testIsSeekable() - { - $stream = new Stream($this->handle); + public function testIsSeekable() + { + $stream = new Stream($this->handle); - $this->assertTrue($stream->isSeekable()); + $this->assertTrue($stream->isSeekable()); - $stream->detach(); - $this->assertFalse($stream->isSeekable()); - } + $stream->detach(); + $this->assertFalse($stream->isSeekable()); + } - public function testRewind() - { - $string = 'Hello, world!'; - $length = \strlen($string); + public function testRewind() + { + $string = 'Hello, world!'; + $length = \strlen($string); - $stream = new Stream($this->handle); + $stream = new Stream($this->handle); - \fwrite($this->handle, $string, $length); - $stream->rewind(); - $this->assertEquals(0, \ftell($this->handle)); - } + \fwrite($this->handle, $string, $length); + $stream->rewind(); + $this->assertEquals(0, \ftell($this->handle)); + } - public function testRewindUnresourceable() - { - $this->expectException(UnseekableStreamException::class); - $this->expectExceptionMessage('Stream is not resourceable'); + public function testRewindUnresourceable() + { + $this->expectException(UnseekableStreamException::class); + $this->expectExceptionMessage('Stream is not resourceable'); - $stream = new Stream($this->handle); + $stream = new Stream($this->handle); - $stream->close(); - $stream->rewind(); - } + $stream->close(); + $stream->rewind(); + } - public function testSeek() - { - $string = 'Hello, world!'; - $length = \strlen($string); + public function testSeek() + { + $string = 'Hello, world!'; + $length = \strlen($string); - $stream = new Stream($this->handle); + $stream = new Stream($this->handle); - \fwrite($this->handle, $string, $length); - \rewind($this->handle); - $stream->seek($length, \SEEK_SET); - $this->assertEquals($length, \ftell($this->handle)); - } + \fwrite($this->handle, $string, $length); + \rewind($this->handle); + $stream->seek($length, \SEEK_SET); + $this->assertEquals($length, \ftell($this->handle)); + } - public function testSeekUnresourceable() - { - $this->expectException(UnseekableStreamException::class); - $this->expectExceptionMessage('Stream is not resourceable'); + public function testSeekUnresourceable() + { + $this->expectException(UnseekableStreamException::class); + $this->expectExceptionMessage('Stream is not resourceable'); - $stream = new Stream($this->handle); + $stream = new Stream($this->handle); - $stream->close(); - $stream->seek(0, \SEEK_SET); - } + $stream->close(); + $stream->seek(0, \SEEK_SET); + } - public function testIsWritable() - { - $stream = new Stream(\STDOUT); - $this->assertTrue($stream->isWritable()); + public function testIsWritable() + { + $stream = new Stream(\STDOUT); + $this->assertTrue($stream->isWritable()); - $stream = new Stream(\STDIN); - $this->assertFalse($stream->isWritable()); - } + $stream = new Stream(\STDIN); + $this->assertFalse($stream->isWritable()); + } - public function testWrite() - { - $string = 'Hello, world!'; - $length = \strlen($string); + public function testWrite() + { + $string = 'Hello, world!'; + $length = \strlen($string); - $stream = new Stream($this->handle); + $stream = new Stream($this->handle); - $this->assertEquals($length, $stream->write($string)); + $this->assertEquals($length, $stream->write($string)); - \rewind($this->handle); - $this->assertEquals($string, \fread($this->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'); + public function testWriteUnresourceable() + { + $this->expectException(UnwritableStreamException::class); + $this->expectExceptionMessage('Stream is not resourceable'); - $stream = new Stream($this->handle); + $stream = new Stream($this->handle); - $stream->close(); - $stream->write('0', 1); - } + $stream->close(); + $stream->write('0', 1); + } - public function testWriteUnwritable() - { - $this->expectException(UnwritableStreamException::class); - $this->expectExceptionMessage('Stream is not writable'); + public function testWriteUnwritable() + { + $this->expectException(UnwritableStreamException::class); + $this->expectExceptionMessage('Stream is not writable'); - $stream = new Stream(\STDIN); - $stream->write('0', 1); - } + $stream = new Stream(\STDIN); + $stream->write('0', 1); + } - public function testIsReadable() - { - $stream = new Stream(\STDIN); - $this->assertTrue($stream->isReadable()); + public function testIsReadable() + { + $stream = new Stream(\STDIN); + $this->assertTrue($stream->isReadable()); - $stream = new Stream(\STDOUT); - $this->assertFalse($stream->isReadable()); - } - - public function testRead() - { - $string = 'Hello, world!'; - $length = \strlen($string); + $stream = new Stream(\STDOUT); + $this->assertFalse($stream->isReadable()); + } + + public function testRead() + { + $string = 'Hello, world!'; + $length = \strlen($string); - $stream = new Stream($this->handle); + $stream = new Stream($this->handle); - \fwrite($this->handle, $string); - \rewind($this->handle); - $this->assertEquals($string, $stream->read($length)); - } + \fwrite($this->handle, $string); + \rewind($this->handle); + $this->assertEquals($string, $stream->read($length)); + } - public function testReadUnresourceable() - { - $this->expectException(UnreadableStreamException::class); - $this->expectExceptionMessage('Stream is not resourceable'); + public function testReadUnresourceable() + { + $this->expectException(UnreadableStreamException::class); + $this->expectExceptionMessage('Stream is not resourceable'); - $stream = new Stream($this->handle); + $stream = new Stream($this->handle); - $stream->close(); - $stream->read(1); - } - - public function testReadUnreadable() - { - $this->expectException(UnreadableStreamException::class); - $this->expectExceptionMessage('Stream is not readable'); + $stream->close(); + $stream->read(1); + } + + public function testReadUnreadable() + { + $this->expectException(UnreadableStreamException::class); + $this->expectExceptionMessage('Stream is not readable'); - $stream = new Stream(\STDOUT); - $stream->read(1); - } + $stream = new Stream(\STDOUT); + $stream->read(1); + } - public function testGetContents() - { - $string = 'Hello, world!'; - $length = \strlen($string); + public function testGetContents() + { + $string = 'Hello, world!'; + $length = \strlen($string); - $stream = new Stream($this->handle); + $stream = new Stream($this->handle); - \fwrite($this->handle, $string); - \rewind($this->handle); - $this->assertEquals($string, $stream->getContents()); - } + \fwrite($this->handle, $string); + \rewind($this->handle); + $this->assertEquals($string, $stream->getContents()); + } - public function testGetContentsUnresourceable() - { - $this->expectException(UnreadableStreamException::class); - $this->expectExceptionMessage('Stream is not resourceable'); + public function testGetContentsUnresourceable() + { + $this->expectException(UnreadableStreamException::class); + $this->expectExceptionMessage('Stream is not resourceable'); - $stream = new Stream($this->handle); - - $stream->close(); - $stream->getContents(); - } - - public function testGetContentsUnreadable() - { - $this->expectException(UnreadableStreamException::class); - $this->expectExceptionMessage('Stream is not readable'); + $stream = new Stream($this->handle); + + $stream->close(); + $stream->getContents(); + } + + public function testGetContentsUnreadable() + { + $this->expectException(UnreadableStreamException::class); + $this->expectExceptionMessage('Stream is not readable'); - $stream = new Stream(\STDOUT); - $stream->getContents(); - } + $stream = new Stream(\STDOUT); + $stream->getContents(); + } - public function testGetMetadata() - { - $stream = new Stream($this->handle); - - $this->assertEquals( - \stream_get_meta_data($this->handle), - $stream->getMetadata() - ); - } - - public function testGetMetadataWithKey() - { - $stream = new Stream($this->handle); - - $this->assertEquals( - 'php://memory', - $stream->getMetadata('uri') - ); - - $this->assertEquals( - null, - $stream->getMetadata('undefined') - ); - } - - public function testGetMetadataUnresourceable() - { - $stream = new Stream($this->handle); - - $stream->close(); - $this->assertNull($stream->getMetadata()); - } - - public function testGetSize() - { - $string = 'Hello, world!'; - $length = \strlen($string); - - $stream = new Stream($this->handle); + public function testGetMetadata() + { + $stream = new Stream($this->handle); + + $this->assertEquals( + \stream_get_meta_data($this->handle), + $stream->getMetadata() + ); + } + + public function testGetMetadataWithKey() + { + $stream = new Stream($this->handle); + + $this->assertEquals( + 'php://memory', + $stream->getMetadata('uri') + ); + + $this->assertEquals( + null, + $stream->getMetadata('undefined') + ); + } + + public function testGetMetadataUnresourceable() + { + $stream = new Stream($this->handle); + + $stream->close(); + $this->assertNull($stream->getMetadata()); + } + + public function testGetSize() + { + $string = 'Hello, world!'; + $length = \strlen($string); + + $stream = new Stream($this->handle); - \fwrite($this->handle, $string); - $this->assertEquals($length, $stream->getSize()); - - \ftruncate($this->handle, 0); - $this->assertEquals(0, $stream->getSize()); - } - - public function testGetSizeUnresourceable() - { - $stream = new Stream($this->handle); - - $stream->close(); - $this->assertNull($stream->getSize()); - } - - public function testToString() - { - $string = 'Hello, world!'; - $length = \strlen($string); - - $stream = new Stream($this->handle); - - \fwrite($this->handle, $string); - $this->assertEquals($string, (string) $stream); - } - - public function testToStringUnresourceable() - { - $stream = new Stream($this->handle); - - $stream->close(); - $this->assertEquals('', (string) $stream); - } - - public function testToStringUnreadable() - { - $stream = new Stream(\STDOUT); - - $this->assertEquals('', (string) $stream); - } - - public function testExceptions() - { - $this->assertInstanceOf(\RuntimeException::class, new UnopenableStreamException('')); - $this->assertInstanceOf(\RuntimeException::class, new UnreadableStreamException('')); - $this->assertInstanceOf(\RuntimeException::class, new UnseekableStreamException('')); - $this->assertInstanceOf(\RuntimeException::class, new UntellableStreamException('')); - $this->assertInstanceOf(\RuntimeException::class, new UnwritableStreamException('')); - } - - private function assertStreamResourceEquals(StreamInterface $stream, $expected) - { - $property = new \ReflectionProperty($stream, 'resource'); - - $property->setAccessible(true); + \fwrite($this->handle, $string); + $this->assertEquals($length, $stream->getSize()); + + \ftruncate($this->handle, 0); + $this->assertEquals(0, $stream->getSize()); + } + + public function testGetSizeUnresourceable() + { + $stream = new Stream($this->handle); + + $stream->close(); + $this->assertNull($stream->getSize()); + } + + public function testToString() + { + $string = 'Hello, world!'; + $length = \strlen($string); + + $stream = new Stream($this->handle); + + \fwrite($this->handle, $string); + $this->assertEquals($string, (string) $stream); + } + + public function testToStringUnresourceable() + { + $stream = new Stream($this->handle); + + $stream->close(); + $this->assertEquals('', (string) $stream); + } + + public function testToStringUnreadable() + { + $stream = new Stream(\STDOUT); + + $this->assertEquals('', (string) $stream); + } + + public function testExceptions() + { + $this->assertInstanceOf(\RuntimeException::class, new UnopenableStreamException('')); + $this->assertInstanceOf(\RuntimeException::class, new UnreadableStreamException('')); + $this->assertInstanceOf(\RuntimeException::class, new UnseekableStreamException('')); + $this->assertInstanceOf(\RuntimeException::class, new UntellableStreamException('')); + $this->assertInstanceOf(\RuntimeException::class, new UnwritableStreamException('')); + } + + private function assertStreamResourceEquals(StreamInterface $stream, $expected) + { + $property = new \ReflectionProperty($stream, 'resource'); + + $property->setAccessible(true); - return $this->assertEquals($property->getValue($stream), $expected); - } + return $this->assertEquals($property->getValue($stream), $expected); + } }