Skip to content

Commit

Permalink
Add DataStream and StreamWrapper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonhebert committed Apr 24, 2020
1 parent 0c4b498 commit fcbbbee
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 29 deletions.
29 changes: 0 additions & 29 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,35 +112,6 @@ public function testReceiveAsObjectChunks()
$this->assertEquals($correctData, $this->items);
}

public function testCanParseMessageInterface()
{
$message = Mockery::mock(
MessageInterface::class,
[
'getBody' => Mockery::mock(
StreamInterface::class,
['isReadable' => true]
),
]
);

$resource = DataStream::get($message);

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

public function testCanParseStreamInterface()
{
$stream = Mockery::mock(
StreamInterface::class,
['isReadable' => true]
);

$resource = DataStream::get($stream);

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

/**
* @param array $item
*/
Expand Down
62 changes: 62 additions & 0 deletions tests/DataStreamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace JsonCollectionParser\Tests;

use JsonCollectionParser\Stream\DataStream;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;

class DataStreamTest extends TestCase
{
public function tearDown()
{
Mockery::close();
}

public function testCanParseMessageInterface()
{
$message = Mockery::mock(
MessageInterface::class,
[
'getBody' => Mockery::mock(
StreamInterface::class,
['isReadable' => true]
),
]
);

$resource = DataStream::get($message);

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

public function testCanParseStreamInterface()
{
$stream = Mockery::mock(
StreamInterface::class,
['isReadable' => true]
);

$resource = DataStream::get($stream);

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

public function testCanCloseStream()
{
$stream = Mockery::mock(
StreamInterface::class,
['isReadable' => true]
);

$resource = DataStream::get($stream);

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

DataStream::close($resource);

$this->assertFalse(is_resource($resource));
}
}
117 changes: 117 additions & 0 deletions tests/StreamWrapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace JsonCollectionParser\Tests;

use JsonCollectionParser\Stream\StreamWrapper;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;
use stdClass;

class StreamWrapperTest extends TestCase
{
public function setUp()
{
if (in_array(StreamWrapper::NAME, stream_get_wrappers())) {
return;
}

stream_wrapper_register(StreamWrapper::NAME, StreamWrapper::class);
}

public function tearDown()
{
if (in_array(StreamWrapper::NAME, stream_get_wrappers())) {
stream_wrapper_unregister(StreamWrapper::NAME);
}

Mockery::close();
}

public function testCanOpenStream()
{
$this->assertTrue(
is_resource(
$this->openStream(
Mockery::mock(
StreamInterface::class,
[
'isReadable' => true,
]
)
)
)
);
}

public function testCanReadStream()
{
$txt = 'testing';

$resource = $this->openStream(
Mockery::mock(
StreamInterface::class,
[
'isReadable' => true,
'eof' => true,
]
)
->shouldReceive('read')
->andReturn($txt)
->getMock()
);

$this->assertTrue(is_resource($resource));
$this->assertSame($txt, fread($resource, strlen($txt)));
}

public function testCanReadStreamEOF()
{
$resource = $this->openStream(
Mockery::mock(
StreamInterface::class,
[
'isReadable' => true,
'eof' => true,
]
)
);

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

public function testFailOpeningInvalidStream()
{
$this->assertFalse($this->openStream(new stdClass()));
}

public function testFailOpeningUnreadableStream()
{
$this->assertFalse(
$this->openStream(
Mockery::mock(
StreamInterface::class,
[
'isReadable' => false,
]
)
)
);
}

/**
* @param mixed $stream
*
* @return bool|resource
*/
protected function openStream($stream)
{
return @fopen(
StreamWrapper::NAME . '://stream',
'rb',
false,
stream_context_create([StreamWrapper::NAME => compact('stream')])
);
}
}

0 comments on commit fcbbbee

Please sign in to comment.