Browse Source

additinal unit tests added

tags/0.8
Paul Rock 7 years ago
parent
commit
c080af6881
  1. 2
      src/Client.php
  2. 2
      src/Helpers/ArrayHelper.php
  3. 76
      tests/ClientTest.php
  4. 10
      tests/QueryTest.php

2
src/Client.php

@ -62,7 +62,7 @@ class Client implements Interfaces\ClientInterface
// Check for important keys
if (true !== $key = ArrayHelper::checkIfKeysNotExist(['host', 'user', 'pass'], $config->getParameters())) {
throw new ConfigException("Parameter '$key' of Config is not set or empty");
throw new ConfigException("One or few parameters '$key' of Config is not set or empty");
}
// Save config if everything is okay

2
src/Helpers/ArrayHelper.php

@ -37,6 +37,6 @@ class ArrayHelper
$output[] = $key;
}
}
return !empty($output) ? $output : true;
return !empty($output) ? implode(',', $output) : true;
}
}

76
tests/ClientTest.php

@ -4,6 +4,8 @@ namespace RouterOS\Tests;
use PHPUnit\Framework\TestCase;
use RouterOS\Client;
use RouterOS\Exceptions\ConfigException;
use RouterOS\Exceptions\QueryException;
use RouterOS\Query;
use RouterOS\Config;
use RouterOS\Exceptions\ClientException;
@ -32,7 +34,7 @@ class ClientTest extends TestCase
'pass' => 'admin',
'host' => '127.0.0.1'
]);
$obj = new Client($config);
$obj = new Client($config);
$this->assertInternalType('object', $obj);
$socket = $obj->getSocket();
$this->assertInternalType('resource', $socket);
@ -57,6 +59,16 @@ class ClientTest extends TestCase
}
}
public function test__constructEx()
{
$this->expectException(ConfigException::class);
$obj = new Client([
'user' => 'admin',
'pass' => 'admin',
]);
}
public function test__constructLegacy()
{
try {
@ -97,19 +109,73 @@ class ClientTest extends TestCase
$config->set('user', 'admin')->set('pass', 'admin')->set('host', '127.0.0.1');
$obj = new Client($config);
$query = new Query('/ip/address/print');
$query = new Query('/ip/address/print');
$readRaw = $obj->write($query)->read(false);
$this->assertCount(10, $readRaw);
$this->assertEquals('=.id=*1', $readRaw[1]);
$query = new Query('/ip/address/print');
$readRaw = $obj->w($query)->read(false);
$this->assertCount(10, $readRaw);
$this->assertEquals('=.id=*1', $readRaw[1]);
$query = new Query('/interface/getall');
$read = $obj->write($query)->read();
$read = $obj->write($query)->r();
$this->assertCount(1, $read);
$this->assertEquals('*1', $read[0]['.id']);
$query = new Query('/interface');
$readTrap = $obj->write($query)->read(false);
$query = new Query('/interface');
$readTrap = $obj->w($query)->r(false);
$this->assertCount(3, $readTrap);
$this->assertEquals('!trap', $readTrap[0]);
$query = new Query('/interface');
$readTrap = $obj->wr($query, false);
$this->assertCount(3, $readTrap);
$this->assertEquals('!trap', $readTrap[0]);
}
public function testWriteReadString()
{
$config = new Config();
$config->set('user', 'admin')->set('pass', 'admin')->set('host', '127.0.0.1');
$obj = new Client($config);
$readTrap = $obj->wr('/interface', false);
$this->assertCount(3, $readTrap);
$this->assertEquals('!trap', $readTrap[0]);
}
public function testWriteReadArray()
{
$config = new Config();
$config->set('user', 'admin')->set('pass', 'admin')->set('host', '127.0.0.1');
$obj = new Client($config);
$readTrap = $obj->wr(['/interface'], false);
$this->assertCount(3, $readTrap);
$this->assertEquals('!trap', $readTrap[0]);
}
public function testWriteEx()
{
$this->expectException(QueryException::class);
$config = new Config();
$config->set('user', 'admin')->set('pass', 'admin')->set('host', '127.0.0.1');
$obj = new Client($config);
$error = $obj->write($obj)->read(false);
}
public function testGetConfig()
{
$obj = new Client([
'user' => 'admin',
'pass' => 'admin',
'host' => '127.0.0.1'
]);
$config = $obj->getConfig();
$this->assertEquals('admin', $config->get('user'));
}
}

10
tests/QueryTest.php

@ -52,6 +52,14 @@ class QueryTest extends TestCase
$this->assertEquals($test, 'zzz');
}
public function testGetEndpointEx()
{
$this->expectException(QueryException::class);
$obj = new Query(null);
$test = $obj->getEndpoint();
}
public function testSetEndpoint()
{
$obj = new Query('test');
@ -100,7 +108,7 @@ class QueryTest extends TestCase
{
$this->expectException(QueryException::class);
$obj = new Query(null);
$obj = new Query([null]);
$obj->getQuery();
}
}
Loading…
Cancel
Save