From 1157e2343193f74db12379f9356b4f938dad7c9b Mon Sep 17 00:00:00 2001 From: l0gicgate Date: Fri, 3 May 2019 13:44:31 -0600 Subject: [PATCH 1/2] add exception throwing in unusable NonBufferedBody methods --- src/NonBufferedBody.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/NonBufferedBody.php b/src/NonBufferedBody.php index 0839526..e479880 100644 --- a/src/NonBufferedBody.php +++ b/src/NonBufferedBody.php @@ -10,6 +10,7 @@ namespace Slim\Psr7; use Psr\Http\Message\StreamInterface; +use RuntimeException; class NonBufferedBody implements StreamInterface { @@ -26,6 +27,7 @@ public function __toString(): string */ public function close(): void { + throw new RuntimeException('A NonBufferedBody is not closable.'); } /** @@ -73,6 +75,7 @@ public function isSeekable(): bool */ public function seek($offset, $whence = SEEK_SET) { + throw new RuntimeException('A NonBufferedBody is not seekable.'); } /** @@ -80,6 +83,7 @@ public function seek($offset, $whence = SEEK_SET) */ public function rewind(): void { + throw new RuntimeException('A NonBufferedBody is not rewindable.'); } /** @@ -120,7 +124,7 @@ public function isReadable(): bool */ public function read($length): string { - return ''; + throw new RuntimeException('A NonBufferedBody is not readable.'); } /** From 77be5498414c0c973d887eab7059e869b8f7ff21 Mon Sep 17 00:00:00 2001 From: l0gicgate Date: Fri, 3 May 2019 13:44:42 -0600 Subject: [PATCH 2/2] add test coverage for new exceptions throwns --- tests/NonBufferedBodyTest.php | 39 +++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/tests/NonBufferedBodyTest.php b/tests/NonBufferedBodyTest.php index 6e985ff..10e890d 100644 --- a/tests/NonBufferedBodyTest.php +++ b/tests/NonBufferedBodyTest.php @@ -10,6 +10,7 @@ namespace Slim\Tests\Psr7; use PHPUnit\Framework\TestCase; +use RuntimeException; use Slim\Psr7\NonBufferedBody; use Slim\Psr7\Response; use Slim\Tests\Psr7\Assets\HeaderStack; @@ -37,7 +38,6 @@ public function testTheStreamContract() self::assertFalse($body->isSeekable(), 'Cannot seek'); self::assertTrue($body->isWritable(), 'Body is writable'); self::assertFalse($body->isReadable(), 'Body is not readable'); - self::assertSame('', $body->read(10), 'Data cannot be retrieved once written'); self::assertSame('', $body->getContents(), 'Data cannot be retrieved once written'); self::assertNull($body->getMetadata(), 'Metadata mechanism is not implemented'); } @@ -78,7 +78,6 @@ public function testWithAddedHeader() ], HeaderStack::stack()); } - public function testWithoutHeader() { (new Response()) @@ -88,4 +87,40 @@ public function testWithoutHeader() self::assertSame([], HeaderStack::stack()); } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessage A NonBufferedBody is not closable. + */ + public function testCloseThrowsRuntimeException() + { + (new NonBufferedBody())->close(); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessage A NonBufferedBody is not seekable. + */ + public function testSeekThrowsRuntimeException() + { + (new NonBufferedBody())->seek(10); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessage A NonBufferedBody is not rewindable. + */ + public function testRewindThrowsRuntimeException() + { + (new NonBufferedBody())->rewind(); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessage A NonBufferedBody is not readable. + */ + public function testReadThrowsRuntimeException() + { + (new NonBufferedBody())->read(10); + } }