Skip to content

Commit

Permalink
Add additional tests to DataStreamTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonhebert committed Apr 24, 2020
1 parent fcbbbee commit 30f87bc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/Stream/DataStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ public static function get($input)

/**
* @param resource $stream
*
* @return bool|null
*/
public static function close($stream): void
public static function close($stream): ?bool
{
if (!is_resource($stream)) {
return;
return null;
}

extension_loaded('zlib') ? gzclose($stream) : fclose($stream);
return extension_loaded('zlib') ? gzclose($stream) : fclose($stream);
}

/**
Expand Down
97 changes: 96 additions & 1 deletion tests/DataStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
namespace JsonCollectionParser\Tests;

use JsonCollectionParser\Stream\DataStream;
use JsonCollectionParser\Stream\StreamException;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use stdClass;

class DataStreamTest extends TestCase
{
Expand All @@ -15,6 +20,65 @@ public function tearDown()
Mockery::close();
}

/**
* @throws ReflectionException
*/
public function testFailsToParseUnsupportedObjectType()
{
$objectMethod = self::getProtectedMethod('object');
$dataStream = new DataStream();

$this->expectException(StreamException::class);
$this->expectExceptionMessage(
'Unable to create stream from `stdClass`, must be one of `MessageInterface` or `StreamInterface`.'
);

$objectMethod->invokeArgs($dataStream, [new stdClass()]);
}

/**
* @throws ReflectionException
*/
public function testFailsToParseObjectTypeWithInvalidStreamInterface()
{
$objectMethod = self::getProtectedMethod('object');
$dataStream = new DataStream();

$message = Mockery::mock(
MessageInterface::class,
[
'getBody' => new stdClass(),
]
);

$this->expectException(StreamException::class);
$this->expectExceptionMessage(
'Unable to create a stream from `stdClass`, must be `StreamInterface`.'
);

$objectMethod->invokeArgs($dataStream, [$message]);
}

/**
* @throws ReflectionException
*/
public function testFailsToParseUnreadableStreamType()
{

$objectMethod = self::getProtectedMethod('streamWrapper');
$dataStream = new DataStream();

$message = Mockery::mock(
StreamInterface::class,
['isReadable' => false]
);

$this->expectException(StreamException::class);
$this->expectExceptionMessage('Failed to open stream from `' . get_class($message) . '`');

$objectMethod->invokeArgs($dataStream, [$message]);
}

public function testCanParseMessageInterface()
{
$message = Mockery::mock(
Expand Down Expand Up @@ -44,6 +108,13 @@ public function testCanParseStreamInterface()
$this->assertTrue(is_resource($resource));
}

public function testCantParseInvalidStream()
{
$this->expectException(StreamException::class);

$resource = DataStream::get(new stdClass());
}

public function testCanCloseStream()
{
$stream = Mockery::mock(
Expand All @@ -55,8 +126,32 @@ public function testCanCloseStream()

$this->assertTrue(is_resource($resource));

DataStream::close($resource);
$closed = DataStream::close($resource);

$this->assertTrue($closed);
$this->assertFalse(is_resource($resource));
}

public function testCantCloseInvalidStream()
{
$closed = DataStream::close(new stdClass());

$this->assertNull($closed);
}

/**
* @param string $name
*
* @return ReflectionMethod
* @throws ReflectionException
*/
protected static function getProtectedMethod(string $name)
{
$class = new ReflectionClass(DataStream::class);
$method = $class->getMethod($name);

$method->setAccessible(true);

return $method;
}
}

0 comments on commit 30f87bc

Please sign in to comment.