-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DataStream and StreamWrapper tests
- Loading branch information
1 parent
0c4b498
commit fcbbbee
Showing
3 changed files
with
179 additions
and
29 deletions.
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,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)); | ||
} | ||
} |
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,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')]) | ||
); | ||
} | ||
} |