Browse Source

PHPUnit package updated, some extended visualization of tests results enabled, paths to Laravel classes changed, small fixes for phpunit 8.0 added to tests

pull/40/head
Paul Rock 6 years ago
parent
commit
ef55a691b7
  1. 1
      .gitignore
  2. 9
      composer.json
  3. 1
      phpunit.xml
  4. 6
      tests/APIConnectorTest.php
  5. 22
      tests/APILengthCoDecTest.php
  6. 47
      tests/ClientTest.php
  7. 51
      tests/ConfigTest.php
  8. 4
      tests/Helpers/ArrayHelperTest.php
  9. 5
      tests/Helpers/BinaryStringHelperTest.php
  10. 2
      tests/Helpers/TypeHelperTest.php
  11. 18
      tests/QueryTest.php
  12. 14
      tests/ResponseIteratorTest.php
  13. 66
      tests/Streams/ResourceStreamTest.php
  14. 43
      tests/Streams/StringStreamTest.php

1
.gitignore

@ -1,3 +1,4 @@
/.idea/ /.idea/
/vendor/ /vendor/
/composer.lock /composer.lock
/.phpunit.result.cache

9
composer.json

@ -33,10 +33,10 @@
"extra": { "extra": {
"laravel": { "laravel": {
"providers": [ "providers": [
"RouterOS\\Laravel\\ClientServiceProvider"
"RouterOS\\Laravel\\ServiceProvider"
], ],
"aliases": { "aliases": {
"RouterOS": "RouterOS\\Laravel\\ClientFacade"
"RouterOS": "RouterOS\\Laravel\\Facade"
} }
} }
}, },
@ -45,7 +45,8 @@
"ext-sockets": "*" "ext-sockets": "*"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^7.0",
"orchestra/testbench": "^3.0"
"limedeck/phpunit-detailed-printer": "^5.0",
"orchestra/testbench": "^4.0|^5.0",
"phpunit/phpunit": "^8.0"
} }
} }

1
phpunit.xml

@ -6,6 +6,7 @@
convertErrorsToExceptions="true" convertErrorsToExceptions="true"
convertNoticesToExceptions="true" convertNoticesToExceptions="true"
convertWarningsToExceptions="true" convertWarningsToExceptions="true"
printerClass="LimeDeck\Testing\Printer"
processIsolation="false" processIsolation="false"
stopOnFailure="true"> stopOnFailure="true">
<filter> <filter>

6
tests/APIConnectorTest.php

@ -25,7 +25,7 @@ class APIConnectorTest extends TestCase
* @param StreamInterface $stream Cannot typehint, PHP refuse it * @param StreamInterface $stream Cannot typehint, PHP refuse it
* @param bool $closeResource shall we close the resource ? * @param bool $closeResource shall we close the resource ?
*/ */
public function test_construct(StreamInterface $stream, bool $closeResource = false)
public function testConstruct(StreamInterface $stream, bool $closeResource = false): void
{ {
$apiStream = new APIConnector($stream); $apiStream = new APIConnector($stream);
$this->assertInstanceOf(APIConnector::class, $apiStream); $this->assertInstanceOf(APIConnector::class, $apiStream);
@ -53,7 +53,7 @@ class APIConnectorTest extends TestCase
* @param APIConnector $connector * @param APIConnector $connector
* @param string $expected * @param string $expected
*/ */
public function test__readWord(APIConnector $connector, string $expected)
public function testReadWord(APIConnector $connector, string $expected): void
{ {
$this->assertSame($expected, $connector->readWord()); $this->assertSame($expected, $connector->readWord());
} }
@ -79,7 +79,7 @@ class APIConnectorTest extends TestCase
* @param string $toWrite * @param string $toWrite
* @param int $expected * @param int $expected
*/ */
public function test_writeWord(APIConnector $connector, string $toWrite, int $expected)
public function testWriteWord(APIConnector $connector, string $toWrite, int $expected): void
{ {
$this->assertEquals($expected, $connector->writeWord($toWrite)); $this->assertEquals($expected, $connector->writeWord($toWrite));
} }

22
tests/APILengthCoDecTest.php

@ -18,11 +18,13 @@ class APILengthCoDecTest extends TestCase
{ {
/** /**
* @dataProvider encodeLengthNegativeProvider * @dataProvider encodeLengthNegativeProvider
* @expectedException \DomainException
* @covers ::encodeLength * @covers ::encodeLength
*
* @param $length
*/ */
public function test__encodeLengthNegative($length)
public function testEncodeLengthNegative($length): void
{ {
$this->expectException(\DomainException::class);
APILengthCoDec::encodeLength($length); APILengthCoDec::encodeLength($length);
} }
@ -37,8 +39,11 @@ class APILengthCoDecTest extends TestCase
/** /**
* @dataProvider encodedLengthProvider * @dataProvider encodedLengthProvider
* @covers ::encodeLength * @covers ::encodeLength
*
* @param $expected
* @param $length
*/ */
public function test__encodeLength($expected, $length)
public function testEncodeLength($expected, $length): void
{ {
$this->assertEquals(BinaryStringHelper::IntegerToNBOBinaryString((int) $expected), APILengthCoDec::encodeLength($length)); $this->assertEquals(BinaryStringHelper::IntegerToNBOBinaryString((int) $expected), APILengthCoDec::encodeLength($length));
} }
@ -76,8 +81,11 @@ class APILengthCoDecTest extends TestCase
/** /**
* @dataProvider encodedLengthProvider * @dataProvider encodedLengthProvider
* @covers ::decodeLength * @covers ::decodeLength
*
* @param $encodedLength
* @param $expected
*/ */
public function test__decodeLength($encodedLength, $expected)
public function testDecodeLength($encodedLength, $expected): void
{ {
// We have to provide $encodedLength as a "bytes" stream // We have to provide $encodedLength as a "bytes" stream
$stream = new StringStream(BinaryStringHelper::IntegerToNBOBinaryString($encodedLength)); $stream = new StringStream(BinaryStringHelper::IntegerToNBOBinaryString($encodedLength));
@ -87,10 +95,12 @@ class APILengthCoDecTest extends TestCase
/** /**
* @dataProvider decodeLengthControlWordProvider * @dataProvider decodeLengthControlWordProvider
* @covers ::decodeLength * @covers ::decodeLength
* @expectedException \UnexpectedValueException
*
* @param string $encodedLength
*/ */
public function test_decodeLengthControlWord(string $encodedLength)
public function testDecodeLengthControlWord(string $encodedLength): void
{ {
$this->expectException(\UnexpectedValueException::class);
APILengthCoDec::decodeLength(new StringStream($encodedLength)); APILengthCoDec::decodeLength(new StringStream($encodedLength));
} }

47
tests/ClientTest.php

@ -2,6 +2,7 @@
namespace RouterOS\Tests; namespace RouterOS\Tests;
use Exception;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use RouterOS\Client; use RouterOS\Client;
use RouterOS\Exceptions\ConfigException; use RouterOS\Exceptions\ConfigException;
@ -27,7 +28,7 @@ class ClientTest extends TestCase
*/ */
public $port_legacy; public $port_legacy;
public function setUp()
public function setUp(): void
{ {
parent::setUp(); parent::setUp();
@ -41,7 +42,7 @@ class ClientTest extends TestCase
$this->port_legacy = (int) getenv('ROS_PORT_LEGACY'); $this->port_legacy = (int) getenv('ROS_PORT_LEGACY');
} }
public function test__construct(): void
public function testConstruct(): void
{ {
try { try {
$config = new Config(); $config = new Config();
@ -54,12 +55,12 @@ class ClientTest extends TestCase
$this->assertIsObject($obj); $this->assertIsObject($obj);
$socket = $obj->getSocket(); $socket = $obj->getSocket();
$this->assertIsResource($socket); $this->assertIsResource($socket);
} catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
} catch (Exception $e) {
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
} }
} }
public function test__construct2(): void
public function testConstruct2(): void
{ {
try { try {
$config = new Config($this->router); $config = new Config($this->router);
@ -67,24 +68,24 @@ class ClientTest extends TestCase
$this->assertIsObject($obj); $this->assertIsObject($obj);
$socket = $obj->getSocket(); $socket = $obj->getSocket();
$this->assertIsResource($socket); $this->assertIsResource($socket);
} catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
} catch (Exception $e) {
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
} }
} }
public function test__construct3(): void
public function testConstruct3(): void
{ {
try { try {
$obj = new Client($this->router); $obj = new Client($this->router);
$this->assertIsObject($obj); $this->assertIsObject($obj);
$socket = $obj->getSocket(); $socket = $obj->getSocket();
$this->assertIsResource($socket); $this->assertIsResource($socket);
} catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
} catch (Exception $e) {
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
} }
} }
public function test__constructEx(): void
public function testConstructEx(): void
{ {
$this->expectException(ConfigException::class); $this->expectException(ConfigException::class);
@ -94,7 +95,7 @@ class ClientTest extends TestCase
]); ]);
} }
public function test__constructLegacy(): void
public function testConstructLegacy(): void
{ {
try { try {
$obj = new Client([ $obj = new Client([
@ -105,8 +106,8 @@ class ClientTest extends TestCase
'legacy' => true 'legacy' => true
]); ]);
$this->assertIsObject($obj); $this->assertIsObject($obj);
} catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
} catch (Exception $e) {
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
} }
} }
@ -115,7 +116,7 @@ class ClientTest extends TestCase
* *
* login() method recognise legacy router response and swap to legacy mode * login() method recognise legacy router response and swap to legacy mode
*/ */
public function test__constructLegacy2(): void
public function testConstructLegacy2(): void
{ {
try { try {
$obj = new Client([ $obj = new Client([
@ -126,13 +127,13 @@ class ClientTest extends TestCase
'legacy' => false 'legacy' => false
]); ]);
$this->assertIsObject($obj); $this->assertIsObject($obj);
} catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
} catch (Exception $e) {
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
} }
} }
public function test__constructWrongPass(): void
public function testConstructWrongPass(): void
{ {
$this->expectException(ClientException::class); $this->expectException(ClientException::class);
@ -144,13 +145,9 @@ class ClientTest extends TestCase
]); ]);
} }
/**
* @expectedException ClientException
*/
public function test__constructWrongNet(): void
public function testConstructWrongNet(): void
{ {
$this->expectException(ClientException::class); $this->expectException(ClientException::class);
$obj = new Client([ $obj = new Client([
'user' => $this->router['user'], 'user' => $this->router['user'],
'pass' => $this->router['pass'], 'pass' => $this->router['pass'],
@ -216,7 +213,7 @@ class ClientTest extends TestCase
{ {
$obj = new Client($this->router); $obj = new Client($this->router);
$obj = $obj->write('/system/package/print')->readAsIterator();
$obj = $obj->query('/system/package/print')->readAsIterator();
$this->assertIsObject($obj); $this->assertIsObject($obj);
} }
@ -228,7 +225,7 @@ class ClientTest extends TestCase
'host' => $this->router['host'], 'host' => $this->router['host'],
]); ]);
$readTrap = $obj->wr('/interface', false);
$readTrap = $obj->query('/interface')->read(false);
$this->assertCount(3, $readTrap); $this->assertCount(3, $readTrap);
$this->assertEquals('!trap', $readTrap[0]); $this->assertEquals('!trap', $readTrap[0]);
} }

51
tests/ConfigTest.php

@ -8,58 +8,58 @@ use RouterOS\Exceptions\ConfigException;
class ConfigTest extends TestCase class ConfigTest extends TestCase
{ {
public function test__construct()
public function testConstruct(): void
{ {
try { try {
$obj = new Config(); $obj = new Config();
$this->assertInternalType('object', $obj);
$this->assertIsObject($obj);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
} }
} }
public function testGetParameters()
public function testGetParameters(): void
{ {
$obj = new Config(); $obj = new Config();
$params = $obj->getParameters(); $params = $obj->getParameters();
$this->assertCount(5, $params); $this->assertCount(5, $params);
$this->assertEquals($params['legacy'], false);
$this->assertEquals($params['ssl'], false);
$this->assertEquals($params['timeout'], 10);
$this->assertEquals($params['attempts'], 10);
$this->assertEquals($params['delay'], 1);
$this->assertEquals(false, $params['legacy']);
$this->assertEquals(false, $params['ssl']);
$this->assertEquals(10, $params['timeout']);
$this->assertEquals(10, $params['attempts']);
$this->assertEquals(1, $params['delay']);
} }
public function testGetParameters2()
public function testGetParameters2(): void
{ {
$obj = new Config(['timeout' => 100]); $obj = new Config(['timeout' => 100]);
$params = $obj->getParameters(); $params = $obj->getParameters();
$this->assertCount(5, $params); $this->assertCount(5, $params);
$this->assertEquals($params['timeout'], 100);
$this->assertEquals(100, $params['timeout']);
} }
public function testSet()
public function testSet(): void
{ {
$obj = new Config(); $obj = new Config();
$obj->set('timeout', 111); $obj->set('timeout', 111);
$params = $obj->getParameters(); $params = $obj->getParameters();
$this->assertEquals($params['timeout'], 111);
$this->assertEquals(111, $params['timeout']);
} }
public function testSetArr()
public function testSetArr(): void
{ {
$obj = new Config([ $obj = new Config([
'timeout' => 111 'timeout' => 111
]); ]);
$params = $obj->getParameters(); $params = $obj->getParameters();
$this->assertEquals($params['timeout'], 111);
$this->assertEquals(111, $params['timeout']);
} }
public function testDelete()
public function testDelete(): void
{ {
$obj = new Config(); $obj = new Config();
$obj->delete('timeout'); $obj->delete('timeout');
@ -68,7 +68,7 @@ class ConfigTest extends TestCase
$this->assertArrayNotHasKey('timeout', $params); $this->assertArrayNotHasKey('timeout', $params);
} }
public function testDeleteEx()
public function testDeleteEx(): void
{ {
$this->expectException(ConfigException::class); $this->expectException(ConfigException::class);
@ -76,7 +76,7 @@ class ConfigTest extends TestCase
$obj->delete('wrong'); $obj->delete('wrong');
} }
public function testSetEx1()
public function testSetEx1(): void
{ {
$this->expectException(ConfigException::class); $this->expectException(ConfigException::class);
@ -84,7 +84,7 @@ class ConfigTest extends TestCase
$obj->set('delay', 'some string'); $obj->set('delay', 'some string');
} }
public function testSetEx2()
public function testSetEx2(): void
{ {
$this->expectException(ConfigException::class); $this->expectException(ConfigException::class);
@ -92,27 +92,26 @@ class ConfigTest extends TestCase
$obj->set('wrong', 'some string'); $obj->set('wrong', 'some string');
} }
public function testGet()
public function testGet(): void
{ {
$obj = new Config(); $obj = new Config();
$test1 = $obj->get('legacy'); $test1 = $obj->get('legacy');
$this->assertEquals($test1, false);
$this->assertEquals(false, $test1);
$test2 = $obj->get('port'); $test2 = $obj->get('port');
$this->assertEquals($test2, 8728);
$this->assertEquals(8728, $test2);
$obj->set('port', 10000); $obj->set('port', 10000);
$test3 = $obj->get('port'); $test3 = $obj->get('port');
$this->assertEquals($test3, 10000);
$this->assertEquals(10000, $test3);
$obj->delete('port'); $obj->delete('port');
$obj->set('ssl', true); $obj->set('ssl', true);
$test3 = $obj->get('port'); $test3 = $obj->get('port');
$this->assertEquals($test3, 8729);
$this->assertEquals(8729, $test3);
} }
public function testGetEx()
public function testGetEx(): void
{ {
$this->expectException(ConfigException::class); $this->expectException(ConfigException::class);

4
tests/Helpers/ArrayHelperTest.php

@ -7,7 +7,7 @@ use RouterOS\Helpers\ArrayHelper;
class ArrayHelperTest extends TestCase class ArrayHelperTest extends TestCase
{ {
public function testCheckIfKeyNotExist()
public function testCheckIfKeyNotExist(): void
{ {
$test1 = ArrayHelper::checkIfKeyNotExist(1, [0 => 'a', 1 => 'b', 2 => 'c']); $test1 = ArrayHelper::checkIfKeyNotExist(1, [0 => 'a', 1 => 'b', 2 => 'c']);
$this->assertFalse($test1); $this->assertFalse($test1);
@ -16,7 +16,7 @@ class ArrayHelperTest extends TestCase
$this->assertTrue($test2); $this->assertTrue($test2);
} }
public function testCheckIfKeysNotExist()
public function testCheckIfKeysNotExist(): void
{ {
$test1 = ArrayHelper::checkIfKeysNotExist([1, 2], [0 => 'a', 1 => 'b', 2 => 'c']); $test1 = ArrayHelper::checkIfKeysNotExist([1, 2], [0 => 'a', 1 => 'b', 2 => 'c']);
$this->assertTrue($test1); $this->assertTrue($test1);

5
tests/Helpers/BinaryStringHelperTest.php

@ -16,8 +16,11 @@ class BinaryStringHelperTest extends TestCase
/** /**
* @dataProvider IntegerToNBOBinaryStringProvider * @dataProvider IntegerToNBOBinaryStringProvider
* @covers ::IntegerToNBOBinaryString * @covers ::IntegerToNBOBinaryString
*
* @param $value
* @param $expected
*/ */
public function test__IntegerToNBOBinaryString($value, $expected)
public function testIntegerToNBOBinaryString($value, $expected): void
{ {
$this->assertEquals($expected, BinaryStringHelper::IntegerToNBOBinaryString($value)); $this->assertEquals($expected, BinaryStringHelper::IntegerToNBOBinaryString($value));
} }

2
tests/Helpers/TypeHelperTest.php

@ -7,7 +7,7 @@ use RouterOS\Helpers\TypeHelper;
class TypeHelperTest extends TestCase class TypeHelperTest extends TestCase
{ {
public function testCheckIfTypeMismatch()
public function testCheckIfTypeMismatch(): void
{ {
$test1 = TypeHelper::checkIfTypeMismatch(gettype(true), gettype(false)); $test1 = TypeHelper::checkIfTypeMismatch(gettype(true), gettype(false));
$this->assertFalse($test1); $this->assertFalse($test1);

18
tests/QueryTest.php

@ -8,33 +8,33 @@ use RouterOS\Query;
class QueryTest extends TestCase class QueryTest extends TestCase
{ {
public function test__construct(): void
public function testConstruct(): void
{ {
try { try {
$obj = new Query('test'); $obj = new Query('test');
$this->assertIsObject($obj); $this->assertIsObject($obj);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
} }
} }
public function test__construct_arr(): void
public function testConstructArr(): void
{ {
try { try {
$obj = new Query('test', ['line1', 'line2', 'line3']); $obj = new Query('test', ['line1', 'line2', 'line3']);
$this->assertIsObject($obj); $this->assertIsObject($obj);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
} }
} }
public function test__construct_arr2(): void
public function testConstructArr2(): void
{ {
try { try {
$obj = new Query(['test', 'line1', 'line2', 'line3']); $obj = new Query(['test', 'line1', 'line2', 'line3']);
$this->assertIsObject($obj); $this->assertIsObject($obj);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
} }
} }
@ -42,14 +42,14 @@ class QueryTest extends TestCase
{ {
$obj = new Query('test'); $obj = new Query('test');
$test = $obj->getEndpoint(); $test = $obj->getEndpoint();
$this->assertEquals($test, 'test');
$this->assertEquals('test', $test);
} }
public function testGetEndpoint2(): void public function testGetEndpoint2(): void
{ {
$obj = new Query(['zzz', 'line1', 'line2', 'line3']); $obj = new Query(['zzz', 'line1', 'line2', 'line3']);
$test = $obj->getEndpoint(); $test = $obj->getEndpoint();
$this->assertEquals($test, 'zzz');
$this->assertEquals('zzz', $test);
} }
public function testGetEndpointEx(): void public function testGetEndpointEx(): void
@ -65,7 +65,7 @@ class QueryTest extends TestCase
$obj = new Query('test'); $obj = new Query('test');
$obj->setEndpoint('zzz'); $obj->setEndpoint('zzz');
$test = $obj->getEndpoint(); $test = $obj->getEndpoint();
$this->assertEquals($test, 'zzz');
$this->assertEquals('zzz', $test);
} }
public function testGetAttributes(): void public function testGetAttributes(): void

14
tests/ResponseIteratorTest.php

@ -7,7 +7,7 @@ use RouterOS\Client;
class ResponseIteratorTest extends TestCase class ResponseIteratorTest extends TestCase
{ {
public function test__construct()
public function testConstruct(): void
{ {
$obj = new Client([ $obj = new Client([
'user' => getenv('ROS_USER'), 'user' => getenv('ROS_USER'),
@ -15,11 +15,11 @@ class ResponseIteratorTest extends TestCase
'host' => getenv('ROS_HOST'), 'host' => getenv('ROS_HOST'),
]); ]);
$obj = $obj->write('/system/package/print')->readAsIterator();
$obj = $obj->query('/system/package/print')->readAsIterator();
$this->assertIsObject($obj); $this->assertIsObject($obj);
} }
public function testReadWrite()
public function testReadWrite(): void
{ {
$obj = new Client([ $obj = new Client([
'user' => getenv('ROS_USER'), 'user' => getenv('ROS_USER'),
@ -27,15 +27,15 @@ class ResponseIteratorTest extends TestCase
'host' => getenv('ROS_HOST'), 'host' => getenv('ROS_HOST'),
]); ]);
$readTrap = $obj->write('/system/package/print')->readAsIterator();
$readTrap = $obj->query('/system/package/print')->readAsIterator();
// Read from RAW // Read from RAW
$this->assertCount(13, $readTrap); $this->assertCount(13, $readTrap);
$readTrap = $obj->write('/ip/address/print')->readAsIterator();
$readTrap = $obj->query('/ip/address/print')->readAsIterator();
$this->assertCount(1, $readTrap); $this->assertCount(1, $readTrap);
$this->assertEquals('ether1', $readTrap[0]['interface']); $this->assertEquals('ether1', $readTrap[0]['interface']);
$readTrap = $obj->write('/system/package/print')->readAsIterator();
$readTrap = $obj->query('/system/package/print')->readAsIterator();
$key = $readTrap->key(); $key = $readTrap->key();
$this->assertEquals(0, $key); $this->assertEquals(0, $key);
$current = $readTrap->current(); $current = $readTrap->current();
@ -68,7 +68,7 @@ class ResponseIteratorTest extends TestCase
'host' => getenv('ROS_HOST'), 'host' => getenv('ROS_HOST'),
]); ]);
$read = $obj->write('/queue/simple/print')->readAsIterator();
$read = $obj->query('/queue/simple/print')->readAsIterator();
$serialize = $read->serialize(); $serialize = $read->serialize();
$this->assertEquals('a:1:{i:0;a:1:{i:0;s:5:"!done";}}', $serialize); $this->assertEquals('a:1:{i:0;a:1:{i:0;s:5:"!done";}}', $serialize);
} }

66
tests/Streams/ResourceStreamTest.php

@ -2,8 +2,10 @@
namespace RouterOS\Tests\Streams; namespace RouterOS\Tests\Streams;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Constraint\IsType; use PHPUnit\Framework\Constraint\IsType;
use RouterOS\Exceptions\StreamException;
use RouterOS\Streams\ResourceStream; use RouterOS\Streams\ResourceStream;
/** /**
@ -17,14 +19,14 @@ class ResourceStreamTest extends TestCase
* Test that constructor throws an InvalidArgumentException on bad parameter type * Test that constructor throws an InvalidArgumentException on bad parameter type
* *
* @covers ::__construct * @covers ::__construct
* @expectedException \InvalidArgumentException
* @dataProvider constructNotResourceProvider * @dataProvider constructNotResourceProvider
* *
* @param $notResource * @param $notResource
*/ */
public function test__constructNotResource($notResource)
public function testConstructNotResource($notResource): void
{ {
$this->expectException(InvalidArgumentException::class);
new ResourceStream($notResource); new ResourceStream($notResource);
} }
@ -56,12 +58,17 @@ class ResourceStreamTest extends TestCase
* @param resource $resource Cannot typehint, PHP refuse it * @param resource $resource Cannot typehint, PHP refuse it
* @param bool $closeResource shall we close the resource ? * @param bool $closeResource shall we close the resource ?
*/ */
public function test_construct($resource, bool $closeResource = true)
public function testConstruct($resource, bool $closeResource = true): void
{ {
$resourceStream = new ResourceStream($resource);
$resourceStream = new class($resource) extends ResourceStream {
public function getStream()
{
return $this->stream;
}
};
$stream = $this->getObjectAttribute($resourceStream, 'stream');
$this->assertInternalType(IsType::TYPE_RESOURCE, $stream);
$stream = $resourceStream->getStream();
$this->assertIsResource($stream);
if ($closeResource) { if ($closeResource) {
fclose($resource); fclose($resource);
@ -89,12 +96,13 @@ class ResourceStreamTest extends TestCase
* @covers ::read * @covers ::read
* @dataProvider readProvider * @dataProvider readProvider
* *
* @param ResourceStream $stream Cannot typehint, PHP refuse it
* @param string $expected the result we should have
* @throws \RouterOS\Exceptions\StreamException
* @throws \InvalidArgumentException
* @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)
public function testRead(ResourceStream $stream, string $expected): void
{ {
$this->assertSame($expected, $stream->read(strlen($expected))); $this->assertSame($expected, $stream->read(strlen($expected)));
} }
@ -115,15 +123,16 @@ class ResourceStreamTest extends TestCase
* *
* @covers ::read * @covers ::read
* @dataProvider readBadLengthProvider * @dataProvider readBadLengthProvider
* @expectedException \InvalidArgumentException
* *
* @param ResourceStream $stream Cannot typehint, PHP refuse it
* @param int $length
* @param ResourceStream $stream Cannot typehint, PHP refuse it
* @param int $length
*
* @throws \RouterOS\Exceptions\StreamException * @throws \RouterOS\Exceptions\StreamException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function test__readBadLength(ResourceStream $stream, int $length)
public function testReadBadLength(ResourceStream $stream, int $length): void
{ {
$this->expectException(InvalidArgumentException::class);
$stream->read($length); $stream->read($length);
} }
@ -143,13 +152,13 @@ class ResourceStreamTest extends TestCase
* *
* @covers ::read * @covers ::read
* @dataProvider readBadResourceProvider * @dataProvider readBadResourceProvider
* @expectedException \RouterOS\Exceptions\StreamException
* *
* @param ResourceStream $stream Cannot typehint, PHP refuse it
* @param int $length
* @param ResourceStream $stream Cannot typehint, PHP refuse it
* @param int $length
*/ */
public function test__readBadResource(ResourceStream $stream, int $length)
public function testReadBadResource(ResourceStream $stream, int $length): void
{ {
$this->expectException(StreamException::class);
$stream->read($length); $stream->read($length);
} }
@ -169,11 +178,12 @@ class ResourceStreamTest extends TestCase
* @covers ::write * @covers ::write
* @dataProvider writeProvider * @dataProvider writeProvider
* *
* @param ResourceStream $stream to test
* @param string $toWrite the writed string
* @param ResourceStream $stream to test
* @param string $toWrite the writed string
*
* @throws \RouterOS\Exceptions\StreamException * @throws \RouterOS\Exceptions\StreamException
*/ */
public function test__write(ResourceStream $stream, string $toWrite)
public function testWrite(ResourceStream $stream, string $toWrite): void
{ {
$this->assertEquals(strlen($toWrite), $stream->write($toWrite)); $this->assertEquals(strlen($toWrite), $stream->write($toWrite));
} }
@ -193,13 +203,13 @@ class ResourceStreamTest extends TestCase
* *
* @covers ::write * @covers ::write
* @dataProvider writeBadResourceProvider * @dataProvider writeBadResourceProvider
* @expectedException \RouterOS\Exceptions\StreamException
* *
* @param ResourceStream $stream to test * @param ResourceStream $stream to test
* @param string $toWrite the written string * @param string $toWrite the written string
*/ */
public function test__writeBadResource(ResourceStream $stream, string $toWrite)
public function testWriteBadResource(ResourceStream $stream, string $toWrite): void
{ {
$this->expectException(StreamException::class);
$stream->write($toWrite); $stream->write($toWrite);
} }
@ -219,12 +229,12 @@ class ResourceStreamTest extends TestCase
* *
* @covers ::close * @covers ::close
* @dataProvider doubleCloseProvider * @dataProvider doubleCloseProvider
* @expectedException \RouterOS\Exceptions\StreamException
* *
* @param ResourceStream $stream to test * @param ResourceStream $stream to test
*/ */
public function test_doubleClose(ResourceStream $stream)
public function testDoubleClose(ResourceStream $stream): void
{ {
$this->expectException(StreamException::class);
$stream->close(); $stream->close();
$stream->close(); $stream->close();
} }
@ -242,13 +252,13 @@ class ResourceStreamTest extends TestCase
* @covers ::close * @covers ::close
* @covers ::write * @covers ::write
* @dataProvider writeClosedResourceProvider * @dataProvider writeClosedResourceProvider
* @expectedException \RouterOS\Exceptions\StreamException
* *
* @param ResourceStream $stream to test * @param ResourceStream $stream to test
* @param string $toWrite the written string * @param string $toWrite the written string
*/ */
public function test_close(ResourceStream $stream, string $toWrite)
public function testClose(ResourceStream $stream, string $toWrite)
{ {
$this->expectException(StreamException::class);
$stream->close(); $stream->close();
$stream->write($toWrite); $stream->write($toWrite);
} }

43
tests/Streams/StringStreamTest.php

@ -19,9 +19,9 @@ class StringStreamTest extends TestCase
* @covers ::__construct * @covers ::__construct
* @dataProvider constructProvider * @dataProvider constructProvider
* *
* @param string $string
* @param string $string
*/ */
public function test__construct(string $string)
public function testConstruct(string $string): void
{ {
$this->assertInstanceOf(StringStream::class, new StringStream($string)); $this->assertInstanceOf(StringStream::class, new StringStream($string));
} }
@ -36,19 +36,18 @@ class StringStreamTest extends TestCase
]; ];
} }
/** /**
* Test that write function returns the effective written bytes * Test that write function returns the effective written bytes
* *
* @covers ::write * @covers ::write
* @dataProvider writeProvider * @dataProvider writeProvider
* *
* @param string $string the string to write
* @param int|null $length the count if bytes to write
* @param int $expected the number of bytes that must be writen
* @param string $string the string to write
* @param int|null $length the count if bytes to write
* @param int $expected the number of bytes that must be writen
*/ */
public function test__write(string $string, $length, int $expected)
public function testWrite(string $string, $length, int $expected): void
{ {
$stream = new StringStream('Does not matters'); $stream = new StringStream('Does not matters');
if (null === $length) { if (null === $length) {
@ -79,10 +78,10 @@ class StringStreamTest extends TestCase
/** /**
* @covers ::write * @covers ::write
* @expectedException \InvalidArgumentException
*/ */
public function test__writeWithNegativeLength()
public function testWriteWithNegativeLength(): void
{ {
$this->expectException(\InvalidArgumentException::class);
$stream = new StringStream('Does not matters'); $stream = new StringStream('Does not matters');
$stream->write('PLOP', -1); $stream->write('PLOP', -1);
} }
@ -92,7 +91,7 @@ class StringStreamTest extends TestCase
* *
* @throws \RouterOS\Exceptions\StreamException * @throws \RouterOS\Exceptions\StreamException
*/ */
public function test__read()
public function testRead(): void
{ {
$stream = new StringStream('123456789'); $stream = new StringStream('123456789');
@ -105,12 +104,11 @@ class StringStreamTest extends TestCase
} }
/** /**
* @expectedException \InvalidArgumentException
*
* @throws \RouterOS\Exceptions\StreamException * @throws \RouterOS\Exceptions\StreamException
*/ */
public function test__readBadLength()
public function testReadBadLength(): void
{ {
$this->expectException(\InvalidArgumentException::class);
$stream = new StringStream('123456789'); $stream = new StringStream('123456789');
$stream->read(-1); $stream->read(-1);
} }
@ -118,14 +116,15 @@ class StringStreamTest extends TestCase
/** /**
* @covers ::read * @covers ::read
* @dataProvider readWhileEmptyProvider * @dataProvider readWhileEmptyProvider
* @expectedException \RouterOS\Exceptions\StreamException
* *
* @param StringStream $stream
* @param int $length
* @throws \RouterOS\Exceptions\StreamException
* @param StringStream $stream
* @param int $length
*
* @throws \RouterOS\Exceptions\StreamException
*/ */
public function test__readWhileEmpty(StringStream $stream, int $length)
public function testReadWhileEmpty(StringStream $stream, int $length): void
{ {
$this->expectException(\RouterOS\Exceptions\StreamException::class);
$stream->read($length); $stream->read($length);
} }
@ -133,7 +132,7 @@ class StringStreamTest extends TestCase
* @return \Generator * @return \Generator
* @throws StreamException * @throws StreamException
*/ */
public function readWhileEmptyProvider()
public function readWhileEmptyProvider(): ?\Generator
{ {
$stream = new StringStream('123456789'); $stream = new StringStream('123456789');
$stream->read(9); $stream->read(9);
@ -148,11 +147,9 @@ class StringStreamTest extends TestCase
yield [$stream, 1]; yield [$stream, 1];
} }
/**
* @expectedException \RouterOS\Exceptions\StreamException
*/
public function testReadClosed()
public function testReadClosed(): void
{ {
$this->expectException(\RouterOS\Exceptions\StreamException::class);
$stream = new StringStream('123456789'); $stream = new StringStream('123456789');
$stream->close(); $stream->close();
$stream->read(1); $stream->read(1);

Loading…
Cancel
Save