|
|
@ -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); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |