Skip to content

Commit

Permalink
Add support for PSR-7 StreamInterface
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
pcrov committed Aug 2, 2018
1 parent e2c8e9d commit f97fb9a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"php": "^7.0",
"ext-intl": "*",
"pcrov/unicode": "^0.1"
"pcrov/unicode": "^0.1",
"psr/http-message": "^1"
},
"require-dev": {
"phpunit/phpunit": "^6.5",
Expand Down
35 changes: 35 additions & 0 deletions src/InputStream/Psr7Stream.php
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;
}
}
}
11 changes: 11 additions & 0 deletions src/JsonReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace pcrov\JsonReader;

use pcrov\JsonReader\InputStream\IOException;
use pcrov\JsonReader\InputStream\Psr7Stream;
use pcrov\JsonReader\InputStream\Stream;
use pcrov\JsonReader\InputStream\Uri;
use pcrov\JsonReader\InputStream\StringInput;
use pcrov\JsonReader\Parser\JsonParser;
use pcrov\JsonReader\Parser\Lexer;
use pcrov\JsonReader\Parser\Parser;
use Psr\Http\Message\StreamInterface;

class JsonReader
{
Expand Down Expand Up @@ -103,6 +105,15 @@ public function open(string $uri)
$this->init(new JsonParser(new Lexer(new Uri($uri))));
}

/**
* @return void
* @throws IOException if a given stream is not readable.
*/
public function psr7Stream(StreamInterface $stream)
{
$this->init(new JsonParser(new Lexer(new Psr7Stream($stream))));
}

/**
* @param resource $stream Readable file stream resource.
* @return void
Expand Down
48 changes: 48 additions & 0 deletions test/InputStream/Psr7StreamTest.php
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);
}
}
13 changes: 13 additions & 0 deletions test/JsonReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use pcrov\JsonReader\Parser\Parser;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;

class JsonReaderTest extends TestCase
{
Expand Down Expand Up @@ -74,6 +75,18 @@ public function testOpen()
while ($reader->read());
}

public function testPsr7Stream()
{
$reader = $this->reader;
$psr7 = $this->createMock(StreamInterface::class);
$psr7->method("isReadable")->willReturn(true);
$reader->psr7Stream($psr7);
$this->assertSame(0, $reader->depth());
$this->assertSame(JsonReader::NONE, $reader->type());
$this->assertNull($reader->name());
$this->assertNull($reader->value());
}

public function testStream()
{
$reader = $this->reader;
Expand Down

0 comments on commit f97fb9a

Please sign in to comment.