Browse Source

additional methods added to query class, array of attributes support added, exceptions was added, QueryInterface updated, new Query tests added

tags/0.7
Paul Rock 7 years ago
parent
commit
3ebb93171c
  1. 23
      src/Interfaces/QueryInterface.php
  2. 43
      src/Query.php
  3. 34
      tests/QueryTest.php

23
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;
}

43
src/Query.php

@ -2,6 +2,7 @@
namespace RouterOS;
use RouterOS\Exceptions\QueryException;
use RouterOS\Interfaces\QueryInterface;
/**
@ -30,10 +31,13 @@ class Query implements QueryInterface
* Query constructor.
*
* @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);

34
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,6 +19,16 @@ 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');
@ -25,6 +36,14 @@ class QueryTest extends TestCase
$this->assertEquals($test, 'test');
}
public function testSetEndpoint()
{
$obj = new Query('test');
$obj->setEndpoint('zzz');
$test = $obj->getEndpoint();
$this->assertEquals($test, 'zzz');
}
public function testGetAttributes()
{
$obj = new Query('test');
@ -32,6 +51,14 @@ class QueryTest extends TestCase
$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();
}
}
Loading…
Cancel
Save