Skip to content

Commit

Permalink
Merge pull request #87 from l0gicgate/AddExceptionsToUnusableNonBuffe…
Browse files Browse the repository at this point in the history
…redBodyMethods

Add exceptions to unusable NonBufferedBody methods
  • Loading branch information
l0gicgate committed May 3, 2019
2 parents 8ce2c78 + 77be549 commit 01bb662
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/NonBufferedBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Slim\Psr7;

use Psr\Http\Message\StreamInterface;
use RuntimeException;

class NonBufferedBody implements StreamInterface
{
Expand All @@ -26,6 +27,7 @@ public function __toString(): string
*/
public function close(): void
{
throw new RuntimeException('A NonBufferedBody is not closable.');
}

/**
Expand Down Expand Up @@ -73,13 +75,15 @@ public function isSeekable(): bool
*/
public function seek($offset, $whence = SEEK_SET)
{
throw new RuntimeException('A NonBufferedBody is not seekable.');
}

/**
* {@inheritdoc}
*/
public function rewind(): void
{
throw new RuntimeException('A NonBufferedBody is not rewindable.');
}

/**
Expand Down Expand Up @@ -120,7 +124,7 @@ public function isReadable(): bool
*/
public function read($length): string
{
return '';
throw new RuntimeException('A NonBufferedBody is not readable.');
}

/**
Expand Down
39 changes: 37 additions & 2 deletions tests/NonBufferedBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -78,7 +78,6 @@ public function testWithAddedHeader()
], HeaderStack::stack());
}


public function testWithoutHeader()
{
(new Response())
Expand All @@ -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);
}
}

0 comments on commit 01bb662

Please sign in to comment.