From 3ebb93171c1aaae83a2dff8fdceba7b8f81f2411 Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Wed, 13 Feb 2019 17:04:04 +0300 Subject: [PATCH] additional methods added to query class, array of attributes support added, exceptions was added, QueryInterface updated, new Query tests added --- src/Interfaces/QueryInterface.php | 23 ++++++++++++++++++-- src/Query.php | 45 ++++++++++++++++++++++++++++++++++----- tests/QueryTest.php | 38 +++++++++++++++++++++++++++++++-- 3 files changed, 97 insertions(+), 9 deletions(-) diff --git a/src/Interfaces/QueryInterface.php b/src/Interfaces/QueryInterface.php index cef93fd..7da279f 100644 --- a/src/Interfaces/QueryInterface.php +++ b/src/Interfaces/QueryInterface.php @@ -28,16 +28,35 @@ interface QueryInterface public function getAttributes(): array; /** + * Set array of attributes + * + * @param array $attributes + * @since 0.7 + * @return \RouterOS\Query + */ + public function setAttributes(array $attributes): Query; + + /** * Get endpoint of current query * - * @return string + * @return string|null + */ + public function getEndpoint(); + + /** + * Set endpoint of query + * + * @param string $endpoint + * @since 0.7 + * @return \RouterOS\Query */ - public function getEndpoint(): string; + public function setEndpoint(string $endpoint): Query; /** * Build body of query * * @return array + * @throws \RouterOS\Exceptions\QueryException */ public function getQuery(): array; } diff --git a/src/Query.php b/src/Query.php index 76ac3bc..89e7733 100644 --- a/src/Query.php +++ b/src/Query.php @@ -2,6 +2,7 @@ namespace RouterOS; +use RouterOS\Exceptions\QueryException; use RouterOS\Interfaces\QueryInterface; /** @@ -29,11 +30,14 @@ class Query implements QueryInterface /** * Query constructor. * - * @param string $endpoint Path of endpoint + * @param string $endpoint Path of endpoint + * @param array $attributes List of attributes which should be set */ - public function __construct(string $endpoint) + public function __construct(string $endpoint = null, array $attributes = []) { - $this->_endpoint = $endpoint; + // TODO: Endpoint may be array, first line will be endpoint, any other attributes + $this->setEndpoint($endpoint); + $this->setAttributes($attributes); } /** @@ -59,22 +63,53 @@ class Query implements QueryInterface } /** + * Set array of attributes + * + * @param array $attributes + * @since 0.7 + * @return \RouterOS\Query + */ + public function setAttributes(array $attributes): Query + { + $this->_attributes = $attributes; + return $this; + } + + /** * Get endpoint of current query * - * @return string + * @return string|null */ - public function getEndpoint(): string + public function getEndpoint() { return $this->_endpoint; } /** + * Set endpoint of query + * + * @param string|null $endpoint + * @since 0.7 + * @return \RouterOS\Query + */ + public function setEndpoint(string $endpoint = null): Query + { + $this->_endpoint = $endpoint; + return $this; + } + + /** * Build body of query * * @return array + * @throws \RouterOS\Exceptions\QueryException */ public function getQuery(): array { + if ($this->getEndpoint() === null) { + throw new QueryException('Endpoint of query is not set'); + } + $endpoint = $this->getEndpoint(); $attributes = $this->getAttributes(); array_unshift($attributes, $endpoint); diff --git a/tests/QueryTest.php b/tests/QueryTest.php index f7dda3b..570d3c7 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -3,6 +3,7 @@ namespace RouterOS\Tests; use PHPUnit\Framework\TestCase; +use RouterOS\Exceptions\QueryException; use RouterOS\Query; class QueryTest extends TestCase @@ -18,20 +19,46 @@ class QueryTest extends TestCase } } + public function test__construct_arr() + { + try { + $obj = new Query('test', ['line1', 'line2', 'line3']); + $this->assertInternalType('object', $obj); + } catch (\Exception $e) { + $this->assertContains('Must be initialized ', $e->getMessage()); + } + } + public function testGetEndpoint() { - $obj = new Query('test'); + $obj = new Query('test'); $test = $obj->getEndpoint(); $this->assertEquals($test, 'test'); } - public function testGetAttributes() + public function testSetEndpoint() { $obj = new Query('test'); + $obj->setEndpoint('zzz'); + $test = $obj->getEndpoint(); + $this->assertEquals($test, 'zzz'); + } + + public function testGetAttributes() + { + $obj = new Query('test'); $test = $obj->getAttributes(); $this->assertCount(0, $test); } + public function testSetAttributes() + { + $obj = new Query('test'); + $obj->setAttributes(['line1', 'line2', 'line3']); + $test = $obj->getAttributes(); + $this->assertCount(3, $test); + } + public function testAdd() { $obj = new Query('test'); @@ -53,4 +80,11 @@ class QueryTest extends TestCase $this->assertEquals($query[1], 'line'); } + public function testGetQueryEx() + { + $this->expectException(QueryException::class); + + $obj = new Query(null); + $obj->getQuery(); + } }