diff --git a/src/Client.php b/src/Client.php index da5a165..752fdb2 100644 --- a/src/Client.php +++ b/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 diff --git a/src/Helpers/ArrayHelper.php b/src/Helpers/ArrayHelper.php index 49b0d0c..8283e31 100644 --- a/src/Helpers/ArrayHelper.php +++ b/src/Helpers/ArrayHelper.php @@ -37,6 +37,6 @@ class ArrayHelper $output[] = $key; } } - return !empty($output) ? $output : true; + return !empty($output) ? implode(',', $output) : true; } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 9eac2ff..d48b40c 100644 --- a/tests/ClientTest.php +++ b/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')); + } } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 716c9c5..dd24ea4 100644 --- a/tests/QueryTest.php +++ b/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(); } }