diff --git a/README.md b/README.md index c374280..5d04e81 100644 --- a/README.md +++ b/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 diff --git a/src/Query.php b/src/Query.php index 5b2ccb8..ffe0d93 100644 --- a/src/Query.php +++ b/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) . ']'); } } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index dd24ea4..26964c6 100644 --- a/tests/QueryTest.php +++ b/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);