-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for PSR-7 StreamInterface
Closes #10
- Loading branch information
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace pcrov\JsonReader\InputStream; | ||
|
||
use Psr\Http\Message\StreamInterface; | ||
|
||
final class Psr7Stream implements InputStream | ||
{ | ||
const CHUNK_SIZE = 8192; | ||
|
||
private $stream; | ||
|
||
/** | ||
* @throws IOException if the given stream is not readable. | ||
*/ | ||
public function __construct(StreamInterface $stream) | ||
{ | ||
if (!$stream->isReadable()) { | ||
throw new IOException("Stream must be readable."); | ||
} | ||
|
||
$this->stream = $stream; | ||
} | ||
|
||
public function read() | ||
{ | ||
$stream = $this->stream; | ||
|
||
try { | ||
return $stream->eof() ? null : $stream->read(self::CHUNK_SIZE); | ||
} catch (\RuntimeException $e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace pcrov\JsonReader\InputStream; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
class Psr7StreamTest extends TestCase | ||
{ | ||
public function testReadReturnsString() | ||
{ | ||
$psr7 = $this->createMock(StreamInterface::class); | ||
$psr7->method("isReadable")->willReturn(true); | ||
$psr7->method("eof")->willReturn(false); | ||
$psr7->method("read")->willReturn("foo"); | ||
|
||
$this->assertSame("foo", (new Psr7Stream($psr7))->read()); | ||
} | ||
|
||
public function testReadEofReturnsNull() | ||
{ | ||
$psr7 = $this->createMock(StreamInterface::class); | ||
$psr7->method("isReadable")->willReturn(true); | ||
$psr7->method("eof")->willReturn(true); | ||
|
||
$this->assertNull((new Psr7Stream($psr7))->read()); | ||
} | ||
|
||
public function testRuntimeExceptionReturnsNull() | ||
{ | ||
$psr7 = $this->createMock(StreamInterface::class); | ||
$psr7->method("isReadable")->willReturn(true); | ||
$psr7->method("eof")->willReturn(false); | ||
$psr7->method("read")->willThrowException(new \RuntimeException()); | ||
|
||
$this->assertNull((new Psr7Stream($psr7))->read()); | ||
} | ||
|
||
public function testUnreadableStreamThrows() | ||
{ | ||
$this->expectException(IOException::class); | ||
$this->expectExceptionMessage("Stream must be readable."); | ||
|
||
$psr7 = $this->createMock(StreamInterface::class); | ||
$psr7->method("isReadable")->willReturn(false); | ||
new Psr7Stream($psr7); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters