Browse Source

tests cleanup and update to level of logic, coverage increased

pull/40/head
Paul Rock 6 years ago
parent
commit
ab632d79cb
  1. 2
      tests/APIConnectorTest.php
  2. 5
      tests/APILengthCoDecTest.php
  3. 185
      tests/ClientTest.php
  4. 4
      tests/ConfigTest.php
  5. 12
      tests/QueryTest.php
  6. 37
      tests/ResponseIteratorTest.php

2
tests/APIConnectorTest.php

@ -38,7 +38,7 @@ class APIConnectorTest extends TestCase
{ {
return [ return [
[new ResourceStream(fopen(__FILE__, 'rb')),], // Myself, sure I exists [new ResourceStream(fopen(__FILE__, 'rb')),], // Myself, sure I exists
[new ResourceStream(fsockopen('tcp://' . getenv('ROS_HOST'), getenv('ROS_PORT_MODERN'))),], // Socket
[new ResourceStream(fsockopen('tcp://' . getenv('ROS_HOST'), getenv('ROS_PORT_MODERN')))], // Socket
[new ResourceStream(STDIN), false], // Try it, but do not close STDIN please !!! [new ResourceStream(STDIN), false], // Try it, but do not close STDIN please !!!
[new StringStream('Hello World !!!')], // Try it, but do not close STDIN please !!! [new StringStream('Hello World !!!')], // Try it, but do not close STDIN please !!!
[new StringStream('')], // Try it, but do not close STDIN please !!! [new StringStream('')], // Try it, but do not close STDIN please !!!

5
tests/APILengthCoDecTest.php

@ -2,9 +2,8 @@
namespace RouterOS\Tests; namespace RouterOS\Tests;
use DomainException;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Constraint\IsType;
use RouterOS\APILengthCoDec; use RouterOS\APILengthCoDec;
use RouterOS\Streams\StringStream; use RouterOS\Streams\StringStream;
use RouterOS\Helpers\BinaryStringHelper; use RouterOS\Helpers\BinaryStringHelper;
@ -24,7 +23,7 @@ class APILengthCoDecTest extends TestCase
*/ */
public function testEncodeLengthNegative($length): void public function testEncodeLengthNegative($length): void
{ {
$this->expectException(\DomainException::class);
$this->expectException(DomainException::class);
APILengthCoDec::encodeLength($length); APILengthCoDec::encodeLength($length);
} }

185
tests/ClientTest.php

@ -16,7 +16,12 @@ class ClientTest extends TestCase
/** /**
* @var array * @var array
*/ */
public $router;
public $config;
/**
* @var \RouterOS\Client
*/
public $client;
/** /**
* @var int * @var int
@ -30,14 +35,15 @@ class ClientTest extends TestCase
public function setUp(): void public function setUp(): void
{ {
parent::setUp();
$this->router = [
'user' => getenv('ROS_USER'),
'pass' => getenv('ROS_PASS'),
'host' => getenv('ROS_HOST'),
$this->config = [
'user' => getenv('ROS_USER'),
'pass' => getenv('ROS_PASS'),
'host' => getenv('ROS_HOST'),
'ssh_port' => (int) getenv('ROS_SSH_PORT'),
]; ];
$this->client = new Client($this->config);
$this->port_modern = (int) getenv('ROS_PORT_MODERN'); $this->port_modern = (int) getenv('ROS_PORT_MODERN');
$this->port_legacy = (int) getenv('ROS_PORT_LEGACY'); $this->port_legacy = (int) getenv('ROS_PORT_LEGACY');
} }
@ -47,9 +53,9 @@ class ClientTest extends TestCase
try { try {
$config = new Config(); $config = new Config();
$config $config
->set('user', $this->router['user'])
->set('pass', $this->router['pass'])
->set('host', $this->router['host']);
->set('user', $this->config['user'])
->set('pass', $this->config['pass'])
->set('host', $this->config['host']);
$obj = new Client($config); $obj = new Client($config);
$this->assertIsObject($obj); $this->assertIsObject($obj);
@ -63,7 +69,7 @@ class ClientTest extends TestCase
public function testConstruct2(): void public function testConstruct2(): void
{ {
try { try {
$config = new Config($this->router);
$config = new Config($this->config);
$obj = new Client($config); $obj = new Client($config);
$this->assertIsObject($obj); $this->assertIsObject($obj);
$socket = $obj->getSocket(); $socket = $obj->getSocket();
@ -76,7 +82,7 @@ class ClientTest extends TestCase
public function testConstruct3(): void public function testConstruct3(): void
{ {
try { try {
$obj = new Client($this->router);
$obj = new Client($this->config);
$this->assertIsObject($obj); $this->assertIsObject($obj);
$socket = $obj->getSocket(); $socket = $obj->getSocket();
$this->assertIsResource($socket); $this->assertIsResource($socket);
@ -85,13 +91,26 @@ class ClientTest extends TestCase
} }
} }
public function testConstructEx(): void
public function testConstructException(): void
{ {
$this->expectException(ConfigException::class); $this->expectException(ConfigException::class);
$obj = new Client([
'user' => $this->router['user'],
'pass' => $this->router['pass'],
new Client([
'user' => $this->config['user'],
'pass' => $this->config['pass'],
]);
}
public function testConstructExceptionBadHost(): void
{
$this->expectException(ClientException::class);
new Client([
'host' => '127.0.0.1',
'port' => 123456,
'attempts' => 0,
'user' => $this->config['user'],
'pass' => $this->config['pass'],
]); ]);
} }
@ -99,9 +118,9 @@ class ClientTest extends TestCase
{ {
try { try {
$obj = new Client([ $obj = new Client([
'user' => $this->router['user'],
'pass' => $this->router['pass'],
'host' => $this->router['host'],
'user' => $this->config['user'],
'pass' => $this->config['pass'],
'host' => $this->config['host'],
'port' => $this->port_legacy, 'port' => $this->port_legacy,
'legacy' => true 'legacy' => true
]); ]);
@ -120,9 +139,9 @@ class ClientTest extends TestCase
{ {
try { try {
$obj = new Client([ $obj = new Client([
'user' => $this->router['user'],
'pass' => $this->router['pass'],
'host' => $this->router['host'],
'user' => $this->config['user'],
'pass' => $this->config['pass'],
'host' => $this->config['host'],
'port' => $this->port_legacy, 'port' => $this->port_legacy,
'legacy' => false 'legacy' => false
]); ]);
@ -132,15 +151,14 @@ class ClientTest extends TestCase
} }
} }
public function testConstructWrongPass(): void public function testConstructWrongPass(): void
{ {
$this->expectException(ClientException::class); $this->expectException(ClientException::class);
$obj = new Client([
'user' => $this->router['user'],
new Client([
'user' => $this->config['user'],
'pass' => 'admin2', 'pass' => 'admin2',
'host' => $this->router['host'],
'host' => $this->config['host'],
'attempts' => 2 'attempts' => 2
]); ]);
} }
@ -148,10 +166,11 @@ class ClientTest extends TestCase
public function testConstructWrongNet(): void public function testConstructWrongNet(): void
{ {
$this->expectException(ClientException::class); $this->expectException(ClientException::class);
$obj = new Client([
'user' => $this->router['user'],
'pass' => $this->router['pass'],
'host' => $this->router['host'],
new Client([
'user' => $this->config['user'],
'pass' => $this->config['pass'],
'host' => $this->config['host'],
'port' => 11111, 'port' => 11111,
'attempts' => 2 'attempts' => 2
]); ]);
@ -159,41 +178,33 @@ class ClientTest extends TestCase
public function testQueryRead(): void public function testQueryRead(): void
{ {
$config = new Config();
$config
->set('user', $this->router['user'])
->set('pass', $this->router['pass'])
->set('host', $this->router['host']);
$obj = new Client($config);
/* /*
* Build query with where * Build query with where
*/ */
$read = $obj->query('/system/package/print', ['name'])->read();
$this->assertCount(13, $read);
$read = $this->client->query('/system/package/print', ['name'])->read();
$this->assertNotEmpty($read);
$read = $obj->query('/system/package/print', ['.id', '*1'])->read();
$read = $this->client->query('/system/package/print', ['.id', '*1'])->read();
$this->assertCount(1, $read); $this->assertCount(1, $read);
$read = $obj->query('/system/package/print', ['.id', '=', '*1'])->read();
$read = $this->client->query('/system/package/print', ['.id', '=', '*1'])->read();
$this->assertCount(1, $read); $this->assertCount(1, $read);
$read = $obj->query('/system/package/print', [['name']])->read();
$this->assertCount(13, $read);
$read = $this->client->query('/system/package/print', [['name']])->read();
$this->assertNotEmpty($read);
$read = $obj->query('/system/package/print', [['.id', '*1']])->read();
$read = $this->client->query('/system/package/print', [['.id', '*1']])->read();
$this->assertCount(1, $read); $this->assertCount(1, $read);
$read = $obj->query('/system/package/print', [['.id', '=', '*1']])->read();
$read = $this->client->query('/system/package/print', [['.id', '=', '*1']])->read();
$this->assertCount(1, $read); $this->assertCount(1, $read);
/* /*
* Build query with operations * Build query with operations
*/ */
$read = $obj->query('/interface/print', [
$read = $this->client->query('/interface/print', [
['type', 'ether'], ['type', 'ether'],
['type', 'vlan'] ['type', 'vlan']
], '|')->read(); ], '|')->read();
@ -204,68 +215,72 @@ class ClientTest extends TestCase
* Build query with tag * Build query with tag
*/ */
$read = $obj->query('/system/package/print', null, null, 'zzzz')->read();
$this->assertCount(13, $read);
$read = $this->client->query('/system/package/print', null, null, 'zzzz')->read();
// $this->assertCount(13, $read);
$this->assertEquals('zzzz', $read[0]['tag']); $this->assertEquals('zzzz', $read[0]['tag']);
} }
public function testReadAsIterator(): void public function testReadAsIterator(): void
{ {
$obj = new Client($this->router);
$obj = $obj->query('/system/package/print')->readAsIterator();
$this->assertIsObject($obj);
$result = $this->client->query('/system/package/print')->readAsIterator();
$this->assertIsObject($result);
} }
public function testWriteReadString(): void public function testWriteReadString(): void
{ {
$obj = new Client([
'user' => $this->router['user'],
'pass' => $this->router['pass'],
'host' => $this->router['host'],
]);
$readTrap = $obj->query('/interface')->read(false);
$readTrap = $this->client->query('/interface')->read(false);
$this->assertCount(3, $readTrap); $this->assertCount(3, $readTrap);
$this->assertEquals('!trap', $readTrap[0]); $this->assertEquals('!trap', $readTrap[0]);
} }
public function testFatal(): void public function testFatal(): void
{ {
$obj = new Client([
'user' => $this->router['user'],
'pass' => $this->router['pass'],
'host' => $this->router['host'],
]);
$readTrap = $obj->query('/quit')->read();
$readTrap = $this->client->query('/quit')->read();
$this->assertCount(2, $readTrap); $this->assertCount(2, $readTrap);
$this->assertEquals('!fatal', $readTrap[0]); $this->assertEquals('!fatal', $readTrap[0]);
} }
public function testQueryEx1(): void
public function queryExceptionDataProvider(): array
{ {
$this->expectException(ClientException::class);
$obj = new Client([
'user' => $this->router['user'],
'pass' => $this->router['pass'],
'host' => $this->router['host'],
]);
$obj->query('/quiet', ['a', 'b', 'c', 'd']);
return [
// Wrong amount of parameters
['exception' => ClientException::class, 'endpoint' => '/quiet', 'attributes' => [[]]],
['exception' => ClientException::class, 'endpoint' => '/quiet', 'attributes' => [[], ['a', 'b', 'c']]],
['exception' => ClientException::class, 'endpoint' => '/quiet', 'attributes' => ['a', 'b', 'c', 'd']],
['exception' => ClientException::class, 'endpoint' => '/quiet', 'attributes' => [['a', 'b', 'c', 'd']]],
['exception' => ClientException::class, 'endpoint' => '/quiet', 'attributes' => [['a', 'b', 'c', 'd'], ['a', 'b', 'c']]],
// Wrong type of endpoint
['exception' => QueryException::class, 'endpoint' => 1, 'attributes' => null],
];
} }
public function testQueryEx2(): void
/**
* @dataProvider queryExceptionDataProvider
*
* @param string $exception
* @param mixed $endpoint
* @param mixed $attributes
*
* @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\ConfigException
* @throws \RouterOS\Exceptions\QueryException
*/
public function testQueryException(string $exception, $endpoint, $attributes): void
{ {
$this->expectException(ClientException::class);
$this->expectException($exception);
$this->client->query($endpoint, $attributes);
}
$obj = new Client([
'user' => $this->router['user'],
'pass' => $this->router['pass'],
'host' => $this->router['host'],
]);
public function testExportMethod(): void
{
$result = $this->client->export();
$this->assertNotEmpty($result);
}
$obj->query('/quiet', [[]]);
public function testExportQuery(): void
{
$result = $this->client->query('/export');
$this->assertNotEmpty($result);
} }
} }

4
tests/ConfigTest.php

@ -76,7 +76,7 @@ class ConfigTest extends TestCase
$obj->delete('wrong'); $obj->delete('wrong');
} }
public function testSetEx1(): void
public function testSetExceptionWrongType(): 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(): void
public function testSetExceptionWrongKey(): void
{ {
$this->expectException(ConfigException::class); $this->expectException(ConfigException::class);

12
tests/QueryTest.php

@ -104,6 +104,18 @@ class QueryTest extends TestCase
$this->assertEquals($attrs[1], '?key2=value2'); $this->assertEquals($attrs[1], '?key2=value2');
} }
public function testEqual(): void
{
$obj = new Query('test');
$obj->equal('key1', 'value1');
$obj->equal('key2', 'value2');
$attrs = $obj->getAttributes();
$this->assertCount(2, $attrs);
$this->assertEquals($attrs[1], '=key2=value2');
}
public function testTag(): void public function testTag(): void
{ {
$obj = new Query('/test/test'); $obj = new Query('/test/test');

37
tests/ResponseIteratorTest.php

@ -4,38 +4,34 @@ namespace RouterOS\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use RouterOS\Client; use RouterOS\Client;
use RouterOS\ResponseIterator;
class ResponseIteratorTest extends TestCase class ResponseIteratorTest extends TestCase
{ {
public function testConstruct(): void
/**
* @var \RouterOS\Client
*/
private $client;
public function setUp(): void
{ {
$obj = new Client([
$this->client = new Client([
'user' => getenv('ROS_USER'), 'user' => getenv('ROS_USER'),
'pass' => getenv('ROS_PASS'), 'pass' => getenv('ROS_PASS'),
'host' => getenv('ROS_HOST'), 'host' => getenv('ROS_HOST'),
]); ]);
$obj = $obj->query('/system/package/print')->readAsIterator();
$this->assertIsObject($obj);
} }
public function testReadWrite(): void public function testReadWrite(): void
{ {
$obj = new Client([
'user' => getenv('ROS_USER'),
'pass' => getenv('ROS_PASS'),
'host' => getenv('ROS_HOST'),
]);
$readTrap = $obj->query('/system/package/print')->readAsIterator();
// Read from RAW
$this->assertCount(13, $readTrap);
$readTrap = $this->client->query('/system/logging/print')->readAsIterator();
$this->assertNotEmpty($readTrap);
$readTrap = $obj->query('/ip/address/print')->readAsIterator();
$readTrap = $this->client->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->query('/system/package/print')->readAsIterator();
$readTrap = $this->client->query('/system/logging/print')->readAsIterator();
$key = $readTrap->key(); $key = $readTrap->key();
$this->assertEquals(0, $key); $this->assertEquals(0, $key);
$current = $readTrap->current(); $current = $readTrap->current();
@ -62,14 +58,9 @@ class ResponseIteratorTest extends TestCase
public function testSerialize(): void public function testSerialize(): void
{ {
$obj = new Client([
'user' => getenv('ROS_USER'),
'pass' => getenv('ROS_PASS'),
'host' => getenv('ROS_HOST'),
]);
$read = $obj->query('/queue/simple/print')->readAsIterator();
$read = $this->client->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);
} }

Loading…
Cancel
Save