Browse Source

tests of Query class updated

tags/1.0
Paul Rock 6 years ago
parent
commit
d53bafd882
  1. 7
      README.md
  2. 21
      src/Query.php
  3. 71
      tests/QueryTest.php

7
README.md

@ -51,7 +51,14 @@ var_dump($response);
$query =
(new Query('/queue/simple/print'))
->where('target', '192.168.1.1/32');
$request = $client->write($query);
// Send advanced query with operations string
$query =
(new Query('/interface/print'))
->where('type', 'ether')
->where('type', 'vlan')
->operations('|');
$request = $client->write($query);
// Read answer from RouterOS

21
src/Query.php

@ -43,6 +43,16 @@ class Query implements QueryInterface
private $_endpoint;
/**
* List of available operators for "->where()" method
*/
public const AVAILABLE_OPERATORS = [
'-', // Does not have
'=', // Equal
'>', // More than
'<' // Less than
];
/**
* Query constructor.
*
* @param array|string $endpoint Path of endpoint
@ -63,13 +73,6 @@ class Query implements QueryInterface
}
}
const AVAILABLE_OPERATORS = [
'-', // Does not have
'=', // Equal
'>', // More than
'<' // Less than
];
/**
* Where logic of query
*
@ -77,7 +80,7 @@ class Query implements QueryInterface
* @param bool|string|int $value Value which need to check (by default true)
* @param bool|string|int $operator It may be one from list [-,=,>,<]
* @return \RouterOS\Query
* @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\QueryException
* @since 1.0.0
*/
public function where(string $key, $operator = '=', $value = null): self
@ -97,7 +100,7 @@ class Query implements QueryInterface
// Overwrite key
$key = $operator . $key;
} else {
throw new ClientException('Operator "' . $operator . '" in not in allowed list [' . implode(',', self::AVAILABLE_OPERATORS) . ']');
throw new QueryException('Operator "' . $operator . '" in not in allowed list [' . implode(',', self::AVAILABLE_OPERATORS) . ']');
}
}

71
tests/QueryTest.php

@ -8,51 +8,51 @@ use RouterOS\Query;
class QueryTest extends TestCase
{
public function test__construct()
public function test__construct(): void
{
try {
$obj = new Query('test');
$this->assertInternalType('object', $obj);
$this->assertIsObject($obj);
} catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
}
}
public function test__construct_arr()
public function test__construct_arr(): void
{
try {
$obj = new Query('test', ['line1', 'line2', 'line3']);
$this->assertInternalType('object', $obj);
$this->assertIsObject($obj);
} catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
}
}
public function test__construct_arr2()
public function test__construct_arr2(): void
{
try {
$obj = new Query(['test', 'line1', 'line2', 'line3']);
$this->assertInternalType('object', $obj);
$this->assertIsObject($obj);
} catch (\Exception $e) {
$this->assertContains('Must be initialized ', $e->getMessage());
}
}
public function testGetEndpoint()
public function testGetEndpoint(): void
{
$obj = new Query('test');
$test = $obj->getEndpoint();
$this->assertEquals($test, 'test');
}
public function testGetEndpoint2()
public function testGetEndpoint2(): void
{
$obj = new Query(['zzz', 'line1', 'line2', 'line3']);
$test = $obj->getEndpoint();
$this->assertEquals($test, 'zzz');
}
public function testGetEndpointEx()
public function testGetEndpointEx(): void
{
$this->expectException(QueryException::class);
@ -60,7 +60,7 @@ class QueryTest extends TestCase
$test = $obj->getEndpoint();
}
public function testSetEndpoint()
public function testSetEndpoint(): void
{
$obj = new Query('test');
$obj->setEndpoint('zzz');
@ -68,14 +68,14 @@ class QueryTest extends TestCase
$this->assertEquals($test, 'zzz');
}
public function testGetAttributes()
public function testGetAttributes(): void
{
$obj = new Query('test');
$test = $obj->getAttributes();
$this->assertCount(0, $test);
}
public function testSetAttributes()
public function testSetAttributes(): void
{
$obj = new Query('test');
$obj->setAttributes(['line1', 'line2', 'line3']);
@ -83,7 +83,7 @@ class QueryTest extends TestCase
$this->assertCount(3, $test);
}
public function testAdd()
public function testAdd(): void
{
$obj = new Query('test');
$obj->add('line');
@ -93,7 +93,48 @@ class QueryTest extends TestCase
$this->assertEquals($attrs[0], 'line');
}
public function testGetQuery()
public function testWhere(): void
{
$obj = new Query('test');
$obj->where('key1', 'value1');
$obj->where('key2', 'value2');
$attrs = $obj->getAttributes();
$this->assertCount(2, $attrs);
$this->assertEquals($attrs[1], '?=key2=value2');
}
public function testTag(): void
{
$obj = new Query('/test/test');
$obj->where('key1', 'value1');
$obj->tag('test');
$query = $obj->getQuery();
$this->assertCount(3, $query);
$this->assertEquals($query[2], '.tag=test');
}
public function testOperator(): void
{
$obj = new Query('/test/test');
$obj->where('key1', 'value1');
$obj->operations('|');
$query = $obj->getQuery();
$this->assertCount(3, $query);
$this->assertEquals($query[2], '?#|');
}
public function testWhereEx(): void
{
$this->expectException(QueryException::class);
$obj = new Query('/richard/cheese');
$obj->where('people', 'equals', 'shit');
}
public function testGetQuery(): void
{
$obj = new Query('test');
$obj->add('line');
@ -104,7 +145,7 @@ class QueryTest extends TestCase
$this->assertEquals($query[1], 'line');
}
public function testGetQueryEx()
public function testGetQueryEx(): void
{
$this->expectException(QueryException::class);

Loading…
Cancel
Save