|
|
|
@ -4,13 +4,12 @@ namespace RouterOS\Tests\Streams; |
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
use PHPUnit\Framework\Constraint\IsType; |
|
|
|
|
|
|
|
use RouterOS\Streams\ResourceStream; |
|
|
|
use RouterOS\Exceptions\StreamException; |
|
|
|
|
|
|
|
/** |
|
|
|
* Limit code coverage to the class RouterOS\APIStream |
|
|
|
* @coversDefaultClass RouterOS\Streams\ResourceStream |
|
|
|
* |
|
|
|
* @coversDefaultClass \RouterOS\Streams\ResourceStream |
|
|
|
*/ |
|
|
|
class ResourceStreamTest extends TestCase |
|
|
|
{ |
|
|
|
@ -20,6 +19,8 @@ class ResourceStreamTest extends TestCase |
|
|
|
* @covers ::__construct |
|
|
|
* @expectedException \InvalidArgumentException |
|
|
|
* @dataProvider constructNotResourceProvider |
|
|
|
* |
|
|
|
* @param $notResource |
|
|
|
*/ |
|
|
|
|
|
|
|
public function test__constructNotResource($notResource) |
|
|
|
@ -32,16 +33,16 @@ class ResourceStreamTest extends TestCase |
|
|
|
* |
|
|
|
* returns data not of type resource |
|
|
|
*/ |
|
|
|
public function constructNotResourceProvider() |
|
|
|
public function constructNotResourceProvider(): array |
|
|
|
{ |
|
|
|
return [ |
|
|
|
[0], // integer
|
|
|
|
[3.14], // float
|
|
|
|
['a string'], // string
|
|
|
|
[ |
|
|
|
[ 0 , 3.14 ] // Array
|
|
|
|
[0, 3.14] // Array
|
|
|
|
], |
|
|
|
[ new \stdClass() ], // Object
|
|
|
|
[new \stdClass()], // Object
|
|
|
|
// What else ?
|
|
|
|
]; |
|
|
|
} |
|
|
|
@ -51,18 +52,18 @@ class ResourceStreamTest extends TestCase |
|
|
|
* |
|
|
|
* @covers ::__construct |
|
|
|
* @dataProvider constructProvider |
|
|
|
* |
|
|
|
* @param resource $resource Cannot typehint, PHP refuse it |
|
|
|
* @param bool $closeResource shall we close the resource ? |
|
|
|
*/ |
|
|
|
public function test_construct($resource, bool $closeResource=true) |
|
|
|
public function test_construct($resource, bool $closeResource = true) |
|
|
|
{ |
|
|
|
$resourceStream = new ResourceStream($resource); |
|
|
|
|
|
|
|
$stream = $this->getObjectAttribute($resourceStream, 'stream'); |
|
|
|
$this->assertInternalType(IsType::TYPE_RESOURCE, $stream); |
|
|
|
|
|
|
|
if ($closeResource) |
|
|
|
{ |
|
|
|
if ($closeResource) { |
|
|
|
fclose($resource); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -70,14 +71,14 @@ class ResourceStreamTest extends TestCase |
|
|
|
/** |
|
|
|
* Data provider for test__construct |
|
|
|
* |
|
|
|
* returns data of type resource |
|
|
|
* @return array data of type resource |
|
|
|
*/ |
|
|
|
public function constructProvider() |
|
|
|
public function constructProvider(): array |
|
|
|
{ |
|
|
|
return [ |
|
|
|
[ fopen(__FILE__, 'r'), ], // Myself, sure I exists
|
|
|
|
[ fsockopen('tcp://127.0.0.1', 18728), ], // Socket
|
|
|
|
[ STDIN, false ], // Try it, but do not close STDIN please !!!
|
|
|
|
[fopen(__FILE__, 'rb'),], // Myself, sure I exists
|
|
|
|
[fsockopen('tcp://127.0.0.1', 18728),], // Socket
|
|
|
|
[STDIN, false], // Try it, but do not close STDIN please !!!
|
|
|
|
// What else ?
|
|
|
|
]; |
|
|
|
} |
|
|
|
@ -87,23 +88,26 @@ class ResourceStreamTest extends TestCase |
|
|
|
* |
|
|
|
* @covers ::read |
|
|
|
* @dataProvider readProvider |
|
|
|
* @param resource $resource Cannot typehint, PHP refuse it |
|
|
|
* @param string $expected the rsult we should have |
|
|
|
* |
|
|
|
* @param ResourceStream $stream Cannot typehint, PHP refuse it |
|
|
|
* @param string $expected the result we should have |
|
|
|
* @throws \RouterOS\Exceptions\StreamException |
|
|
|
* @throws \InvalidArgumentException |
|
|
|
*/ |
|
|
|
public function test__read(ResourceStream $stream, string $expected) |
|
|
|
{ |
|
|
|
$this->assertSame($expected, $stream->read(strlen($expected))); |
|
|
|
} |
|
|
|
|
|
|
|
public function readProvider() |
|
|
|
public function readProvider(): array |
|
|
|
{ |
|
|
|
$resource = fopen(__FILE__, 'r'); |
|
|
|
$resource = fopen(__FILE__, 'rb'); |
|
|
|
$me = new ResourceStream($resource); |
|
|
|
|
|
|
|
return [ |
|
|
|
[ $me, '<'], // Read for byte
|
|
|
|
[ $me, '?php'], // Read following bytes. File statrts with "<php"
|
|
|
|
[$me, '<'], // Read for byte
|
|
|
|
[$me, '?php'], // Read following bytes. File statrts with "<php"
|
|
|
|
]; |
|
|
|
fclose($resource); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -112,43 +116,50 @@ class ResourceStreamTest extends TestCase |
|
|
|
* @covers ::read |
|
|
|
* @dataProvider readBadLengthProvider |
|
|
|
* @expectedException \InvalidArgumentException |
|
|
|
* @param resource $resource Cannot typehint, PHP refuse it |
|
|
|
* |
|
|
|
* @param ResourceStream $stream Cannot typehint, PHP refuse it |
|
|
|
* @param int $length |
|
|
|
* @throws \RouterOS\Exceptions\StreamException |
|
|
|
* @throws \InvalidArgumentException |
|
|
|
*/ |
|
|
|
public function test__readBadLength(ResourceStream $stream, int $length) |
|
|
|
{ |
|
|
|
$stream->read($length); |
|
|
|
} |
|
|
|
|
|
|
|
public function readBadLengthProvider() |
|
|
|
public function readBadLengthProvider(): array |
|
|
|
{ |
|
|
|
$resource = fopen(__FILE__, 'r'); |
|
|
|
$resource = fopen(__FILE__, 'rb'); |
|
|
|
$me = new ResourceStream($resource); |
|
|
|
|
|
|
|
return [ |
|
|
|
[ $me, 0 ], |
|
|
|
[ $me, -1 ], |
|
|
|
[$me, 0], |
|
|
|
[$me, -1], |
|
|
|
]; |
|
|
|
fclose($resource); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Test read to invalid resource |
|
|
|
* |
|
|
|
* @covers ::read |
|
|
|
* @dataProvider readBadResourceProvider |
|
|
|
* @expectedException RouterOS\Exceptions\StreamException |
|
|
|
* @param resource $resource Cannot typehint, PHP refuse it |
|
|
|
* @expectedException \RouterOS\Exceptions\StreamException |
|
|
|
* |
|
|
|
* @param ResourceStream $stream Cannot typehint, PHP refuse it |
|
|
|
* @param int $length |
|
|
|
*/ |
|
|
|
public function test__readBadResource(ResourceStream $stream, int $length) |
|
|
|
{ |
|
|
|
$stream->read($length); |
|
|
|
} |
|
|
|
|
|
|
|
public function readBadResourceProvider() |
|
|
|
public function readBadResourceProvider(): array |
|
|
|
{ |
|
|
|
$resource = fopen(__FILE__, 'r'); |
|
|
|
$resource = fopen(__FILE__, 'rb'); |
|
|
|
$me = new ResourceStream($resource); |
|
|
|
fclose($resource); |
|
|
|
return [ |
|
|
|
[ $me, 1 ], |
|
|
|
[$me, 1], |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
@ -157,22 +168,24 @@ class ResourceStreamTest extends TestCase |
|
|
|
* |
|
|
|
* @covers ::write |
|
|
|
* @dataProvider writeProvider |
|
|
|
* @param ResourceStram $resource to test |
|
|
|
* |
|
|
|
* @param ResourceStream $stream to test |
|
|
|
* @param string $toWrite the writed string |
|
|
|
* @throws \RouterOS\Exceptions\StreamException |
|
|
|
*/ |
|
|
|
public function test__write(ResourceStream $stream, string $toWrite) |
|
|
|
{ |
|
|
|
$this->assertEquals(strlen($toWrite) , $stream->write($toWrite)); |
|
|
|
$this->assertEquals(strlen($toWrite), $stream->write($toWrite)); |
|
|
|
} |
|
|
|
|
|
|
|
public function writeProvider() |
|
|
|
public function writeProvider(): array |
|
|
|
{ |
|
|
|
$resource = fopen("/dev/null", 'w'); |
|
|
|
$resource = fopen('/dev/null', 'wb'); |
|
|
|
$null = new ResourceStream($resource); |
|
|
|
|
|
|
|
return [ |
|
|
|
[ $null, 'yyaagagagag'], // Take that
|
|
|
|
[$null, 'yyaagagagag'], // Take that
|
|
|
|
]; |
|
|
|
fclose($resource); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -180,22 +193,24 @@ class ResourceStreamTest extends TestCase |
|
|
|
* |
|
|
|
* @covers ::write |
|
|
|
* @dataProvider writeBadResourceProvider |
|
|
|
* @expectedException RouterOS\Exceptions\StreamException |
|
|
|
* @param resource $resource to test |
|
|
|
* @param string $toWrite the writed string |
|
|
|
* @expectedException \RouterOS\Exceptions\StreamException |
|
|
|
* |
|
|
|
* @param ResourceStream $stream to test |
|
|
|
* @param string $toWrite the written string |
|
|
|
*/ |
|
|
|
public function test__writeBadResource(ResourceStream $stream, string $toWrite) |
|
|
|
{ |
|
|
|
$stream->write($toWrite); |
|
|
|
} |
|
|
|
|
|
|
|
public function writeBadResourceProvider() |
|
|
|
public function writeBadResourceProvider(): array |
|
|
|
{ |
|
|
|
$resource = fopen('/dev/null', 'w'); |
|
|
|
$resource = fopen('/dev/null', 'wb'); |
|
|
|
$me = new ResourceStream($resource); |
|
|
|
fclose($resource); |
|
|
|
|
|
|
|
return [ |
|
|
|
[ $me, 'sasasaas' ], // Take that
|
|
|
|
[$me, 'sasasaas'], // Take that
|
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
@ -204,8 +219,9 @@ class ResourceStreamTest extends TestCase |
|
|
|
* |
|
|
|
* @covers ::close |
|
|
|
* @dataProvider doubleCloseProvider |
|
|
|
* @expectedException RouterOS\Exceptions\StreamException |
|
|
|
* @param resource $resource to test |
|
|
|
* @expectedException \RouterOS\Exceptions\StreamException |
|
|
|
* |
|
|
|
* @param ResourceStream $stream to test |
|
|
|
*/ |
|
|
|
public function test_doubleClose(ResourceStream $stream) |
|
|
|
{ |
|
|
|
@ -213,10 +229,10 @@ class ResourceStreamTest extends TestCase |
|
|
|
$stream->close(); |
|
|
|
} |
|
|
|
|
|
|
|
public function doubleCloseProvider() |
|
|
|
public function doubleCloseProvider(): array |
|
|
|
{ |
|
|
|
return [ |
|
|
|
[ new ResourceStream(fopen('/dev/null', 'w')), 'sasasaas' ], // Take that
|
|
|
|
[new ResourceStream(fopen('/dev/null', 'wb')), 'sasasaas'], // Take that
|
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
@ -226,9 +242,10 @@ class ResourceStreamTest extends TestCase |
|
|
|
* @covers ::close |
|
|
|
* @covers ::write |
|
|
|
* @dataProvider writeClosedResourceProvider |
|
|
|
* @expectedException RouterOS\Exceptions\StreamException |
|
|
|
* @param resource $resource to test |
|
|
|
* @param string $toWrite the writed string |
|
|
|
* @expectedException \RouterOS\Exceptions\StreamException |
|
|
|
* |
|
|
|
* @param ResourceStream $stream to test |
|
|
|
* @param string $toWrite the written string |
|
|
|
*/ |
|
|
|
public function test_close(ResourceStream $stream, string $toWrite) |
|
|
|
{ |
|
|
|
@ -236,10 +253,10 @@ class ResourceStreamTest extends TestCase |
|
|
|
$stream->write($toWrite); |
|
|
|
} |
|
|
|
|
|
|
|
public function writeClosedResourceProvider() |
|
|
|
public function writeClosedResourceProvider(): array |
|
|
|
{ |
|
|
|
return [ |
|
|
|
[ new ResourceStream(fopen('/dev/null', 'w')), 'sasasaas' ], // Take that
|
|
|
|
[new ResourceStream(fopen('/dev/null', 'wb')), 'sasasaas'], // Take that
|
|
|
|
]; |
|
|
|
} |
|
|
|
|