diff --git a/src/AbstractDocument.php b/src/AbstractDocument.php new file mode 100644 index 0000000..f6f9275 --- /dev/null +++ b/src/AbstractDocument.php @@ -0,0 +1,69 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi; + +/** + * abstract document + * + * @author VEBER Arnaud + */ +abstract class AbstractDocument implements DocumentInterface +{ + /** + * get fields + * + * @return array + */ + abstract protected function getFields(): array; + + /** + * to json + * + * @return string + */ + public function toJson(): string + { + $document = []; + + foreach ($this->getFields() as $field) { + $value = $this->$field; + + if (is_null($value)) { + continue; + } + + if (is_array($value)) { + foreach (array_keys($value) as $key) { + $document[sprintf('%1$s.%2$s', $field, $key)] = $value[$key]; + } + + continue; + } + + $document[$this->toSnakeCase($field)] = $this->$field; + } + + return json_encode($document); + } + + /** + * to snake case + * + * @param string $string + * + * @return string + */ + private function toSnakeCase(string $string): string + { + return mb_strtolower(preg_replace('/(.)(?=[A-Z])/', '$1_', $string)); + } +} diff --git a/src/Document/Api.php b/src/Document/Api.php new file mode 100644 index 0000000..dd2db18 --- /dev/null +++ b/src/Document/Api.php @@ -0,0 +1,219 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi\Document; + +use Unikorp\KongAdminApi\AbstractDocument; + +/** + * api + * + * @author VEBER Arnaud + */ +class Api extends AbstractDocument +{ + /** + * name + * @var string $name + */ + protected $name = null; + + /** + * request host + * @var string $requestHost + */ + protected $requestHost = null; + + /** + * request path + * @var string $requestPath + */ + protected $requestPath = null; + + /** + * strip request path + * @var bool $stripRequestPath + */ + protected $stripRequestPath = false; + + /** + * preserve host + * @var bool $preserveHost + */ + protected $preserveHost = false; + + /** + * upstream url + * @var string $upstreamUrl + */ + protected $upstreamUrl = null; + + /** + * set name + * + * @param string $name + * + * @return this + */ + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + /** + * get name + * + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * set request host + * + * @param string $requestHost + * + * @return this + */ + public function setRequestHost(string $requestHost): self + { + $this->requestHost = $requestHost; + + return $this; + } + + /** + * get request host + * + * @return string + */ + public function getRequestHost(): string + { + return $this->requestHost; + } + + /** + * set request path + * + * @param string $requestPath + * + * @return this + */ + public function setRequestPath(string $requestPath): self + { + $this->requestPath = $requestPath; + + return $this; + } + + /** + * get request path + * + * @return string + */ + public function getRequestPath(): string + { + return $this->requestPath; + } + + /** + * set strip request path + * + * @param bool $stripRequestPath + * + * @return this + */ + public function setStripRequestPath(bool $stripRequestPath): self + { + $this->stripRequestPath = $stripRequestPath; + + return $this; + } + + /** + * get strip request path + * + * @return bool + */ + public function getStripRequestPath(): bool + { + return $this->stripRequestPath; + } + + /** + * set preserve host + * + * @param bool $preserveHost + * + * @return this + */ + public function setPreserveHost(bool $preserveHost): self + { + $this->preserveHost = $preserveHost; + + return $this; + } + + /** + * get preserve host + * + * @return bool + */ + public function getPreserveHost(): bool + { + return $this->preserveHost; + } + + /** + * set upstream url + * + * @param string $upstreamUrl + * + * @return this + */ + public function setUpstreamUrl(string $upstreamUrl): self + { + $this->upstreamUrl = $upstreamUrl; + + return $this; + } + + /** + * get upstream url + * + * @return string + */ + public function getUpstreamUrl(): string + { + return $this->upstreamUrl; + } + + /** + * get fields + * + * @return array + */ + protected function getFields(): array + { + return [ + 'name', + 'requestHost', + 'requestPath', + 'stripRequestPath', + 'preserveHost', + 'upstreamUrl', + ]; + } +} diff --git a/src/Document/Cluster.php b/src/Document/Cluster.php new file mode 100644 index 0000000..055c121 --- /dev/null +++ b/src/Document/Cluster.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi\Document; + +use Unikorp\KongAdminApi\AbstractDocument; + +/** + * cluster + * + * @author VEBER Arnaud + */ +class Cluster extends AbstractDocument +{ + /** + * name + * @var string $name + */ + protected $name = null; + + /** + * set name + * + * @param string $name + * + * @return this + */ + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + /** + * get name + * + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * get fields + * + * @return array + */ + protected function getFields(): array + { + return [ + 'name', + ]; + } +} diff --git a/src/Document/Consumer.php b/src/Document/Consumer.php new file mode 100644 index 0000000..f5ab237 --- /dev/null +++ b/src/Document/Consumer.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi\Document; + +use Unikorp\KongAdminApi\AbstractDocument; + +/** + * consumer + * + * @author VEBER Arnaud + */ +class Consumer extends AbstractDocument +{ + /** + * username + * @var string $username + */ + protected $username = null; + + /** + * custom id + * @var string $customId + */ + protected $customId = null; + + /** + * set username + * + * @param string $username + * + * @return this + */ + public function setUsername(string $username): self + { + $this->username = $username; + + return $this; + } + + /** + * get username + * + * @return string + */ + public function getUsername(): string + { + return $this->username; + } + + /** + * set custom id + * + * @param string $customId + * + * @return this + */ + public function setCustomId(string $customId): self + { + $this->customId = $customId; + + return $this; + } + + /** + * get custom id + * + * @return string + */ + public function getCustomId(): string + { + return $this->customId; + } + + /** + * get fields + * + * @return array + */ + protected function getFields(): array + { + return [ + 'username', + 'customId', + ]; + } +} diff --git a/src/Document/Information.php b/src/Document/Information.php new file mode 100644 index 0000000..2dd4703 --- /dev/null +++ b/src/Document/Information.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi\Document; + +use Unikorp\KongAdminApi\AbstractDocument; + +/** + * information + * + * @author VEBER Arnaud + */ +class Information extends AbstractDocument +{ + /** + * get fields + * + * @return array + */ + protected function getFields(): array + { + return []; + } +} diff --git a/src/Document/Plugin.php b/src/Document/Plugin.php new file mode 100644 index 0000000..c47e84b --- /dev/null +++ b/src/Document/Plugin.php @@ -0,0 +1,149 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi\Document; + +use Unikorp\KongAdminApi\AbstractDocument; + +/** + * plugin + * + * @author VEBER Arnaud + */ +class Plugin extends AbstractDocument +{ + /** + * name + * @var string $name + */ + protected $name = null; + + /** + * consumer id + * @var string $consumerId + */ + protected $consumerId = null; + + /** + * config + * @var array $config + */ + protected $config = []; + + /** + * set name + * + * @param string $name + * + * @return this + */ + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + /** + * get name + * + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * set consumer id + * + * @param string $consumerId + * + * @return this + */ + public function setConsumerId(string $consumerId): self + { + $this->consumerId = $consumerId; + + return $this; + } + + /** + * get consumer id + * + * @return string + */ + public function getConsumerId(): string + { + return $this->consumerId; + } + + /** + * add config + * + * @param string $name + * @param mixed $value + * + * @return this + */ + public function addConfig(string $name, $value): self + { + if (isset($this->config[$name])) { + throw new \RuntimeException(sprintf('Config for name `%1$s` already set', $name)); + } + + $this->config[$name] = $value; + + return $this; + } + + /** + * remove config + * + * @param string $name + * + * @return this + */ + public function removeConfig(string $name): self + { + if (isset($this->config[$name])) { + unset($this->config[$name]); + } + + return $this; + } + + /** + * get config + * + * @param string $name + * + * @return mixed + */ + public function getConfig(string $name) + { + return $this->config[$name]; + } + + /** + * get fields + * + * @return array + */ + protected function getFields(): array + { + return [ + 'name', + 'consumerId', + 'config', + ]; + } +} diff --git a/src/DocumentInterface.php b/src/DocumentInterface.php new file mode 100644 index 0000000..6ca9a75 --- /dev/null +++ b/src/DocumentInterface.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi; + +/** + * document interface + * + * @author VEBER Arnaud + */ +interface DocumentInterface +{ + /** + * to json + * + * @return string + */ + public function toJson(): string; +} diff --git a/tests/Document/ApiTest.php b/tests/Document/ApiTest.php new file mode 100644 index 0000000..a3e6e4c --- /dev/null +++ b/tests/Document/ApiTest.php @@ -0,0 +1,287 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi\Tests\Document; + +use Unikorp\KongAdminApi\Document\Api as Document; +use PHPUnit\Framework\TestCase; + +/** + * api test + * + * @author VEBER Arnaud + */ +class ApiTest extends TestCase +{ + /** + * document + * @var \Unikorp\KongAdminApi\Document\Api $document + */ + private $document = null; + + /** + * set up + * + * @return void + * + * @coversNothing + */ + protected function setUp() + { + $this->document = new Document(); + } + + /** + * tear down + * + * @return void + * + * @coversNothing + */ + protected function tearDown() + { + $this->document = null; + } + + /** + * test set name + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::setName + */ + public function testSetName() + { + // asserts + $this->document->setName('test'); + $this->assertSame('test', $this->readAttribute($this->document, 'name')); + } + + /** + * test get name + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::getName + */ + public function testGetName() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `name` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('name'); + $reflectionProperty->setAccessible(true); + + // assert + $reflectionProperty->setValue($this->document, 'test'); + $this->assertSame('test', $this->document->getName()); + } + + /** + * test set request host + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::setRequestHost + */ + public function testSetRequestHost() + { + // asserts + $this->document->setRequestHost('test'); + $this->assertSame('test', $this->readAttribute($this->document, 'requestHost')); + } + + /** + * test get request host + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::getRequestHost + */ + public function testGetRequestHost() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `request host` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('requestHost'); + $reflectionProperty->setAccessible(true); + + // assert + $reflectionProperty->setValue($this->document, 'test'); + $this->assertSame('test', $this->document->getRequestHost()); + } + + /** + * test set request path + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::setRequestPath + */ + public function testSetRequestPath() + { + // asserts + $this->document->setRequestPath('test'); + $this->assertSame('test', $this->readAttribute($this->document, 'requestPath')); + } + + /** + * test get request path + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::getRequestPath + */ + public function testGetRequestPath() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `request path` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('requestPath'); + $reflectionProperty->setAccessible(true); + + // assert + $reflectionProperty->setValue($this->document, 'test'); + $this->assertSame('test', $this->document->getRequestPath()); + } + + /** + * test set strip request path + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::setStripRequestPath + */ + public function testSetStripRequestPath() + { + // asserts + $this->document->setStripRequestPath(true); + $this->assertSame(true, $this->readAttribute($this->document, 'stripRequestPath')); + } + + /** + * test get strip request path + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::getStripRequestPath + */ + public function testGetStripRequestPath() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `strip request path` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('stripRequestPath'); + $reflectionProperty->setAccessible(true); + + // assert + $reflectionProperty->setValue($this->document, true); + $this->assertSame(true, $this->document->getStripRequestPath()); + } + + /** + * test set preserve host + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::setPreserveHost + */ + public function testSetPreserveHost() + { + // asserts + $this->document->setPreserveHost(true); + $this->assertSame(true, $this->readAttribute($this->document, 'preserveHost')); + } + + /** + * test get preserve host + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::getPreserveHost + */ + public function testGetPreserveHost() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `preserve host` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('preserveHost'); + $reflectionProperty->setAccessible(true); + + // assert + $reflectionProperty->setValue($this->document, true); + $this->assertSame(true, $this->document->getPreserveHost()); + } + + /** + * test set upstream url + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::setUpstreamUrl + */ + public function testSetUpstreamUrl() + { + // asserts + $this->document->setUpstreamUrl('test'); + $this->assertSame('test', $this->readAttribute($this->document, 'upstreamUrl')); + } + + /** + * test get upstream url + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Api::getUpstreamUrl + */ + public function testGetUpstreamUrl() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `upstream url` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('upstreamUrl'); + $reflectionProperty->setAccessible(true); + + // assert + $reflectionProperty->setValue($this->document, 'test'); + $this->assertSame('test', $this->document->getUpstreamUrl()); + } + + /** + * test to json + * + * @return void + * + * @covers \Unikorp\KongAdminApi\AbstractDocument::toJson + * @covers \Unikorp\KongAdminApi\Document\Api::getFields + * @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase + */ + public function testToJson() + { + $this->document + ->setName('name') + ->setRequestHost('requestHost') + ->setStripRequestPath(true) + ->setPreserveHost(true) + ->setUpstreamUrl('upstreamUrl'); + + $this->assertSame( + '{"name":"name","request_host":"requestHost","strip_request_path":true,"preserve_host":true,"upstream_url":"upstreamUrl"}', + $this->document->toJson() + ); + } +} diff --git a/tests/Document/ClusterTest.php b/tests/Document/ClusterTest.php new file mode 100644 index 0000000..1212e92 --- /dev/null +++ b/tests/Document/ClusterTest.php @@ -0,0 +1,105 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi\Tests\Document; + +use Unikorp\KongAdminApi\Document\Cluster as Document; +use PHPUnit\Framework\TestCase; + +/** + * cluster test + * + * @author VEBER Arnaud + */ +class ClusterTest extends TestCase +{ + /** + * document + * @var \Unikorp\KongAdminApi\Document\Cluster $document + */ + private $document = null; + + /** + * set up + * + * @return void + * + * @coversNothing + */ + protected function setUp() + { + $this->document = new Document(); + } + + /** + * tear down + * + * @return void + * + * @coversNothing + */ + protected function tearDown() + { + $this->document = null; + } + + /** + * test set name + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Cluster::setName + */ + public function testSetName() + { + // asserts + $this->document->setName('test'); + $this->assertSame('test', $this->readAttribute($this->document, 'name')); + } + + /** + * test get name + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Cluster::getName + */ + public function testGetName() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `name` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('name'); + $reflectionProperty->setAccessible(true); + + // assert + $reflectionProperty->setValue($this->document, 'test'); + $this->assertSame('test', $this->document->getName()); + } + + /** + * test to json + * + * @return void + * + * @covers \Unikorp\KongAdminApi\AbstractDocument::toJson + * @covers \Unikorp\KongAdminApi\Document\Cluster::getFields + * @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase + */ + public function testToJson() + { + $this->document + ->setName('name'); + + $this->assertSame('{"name":"name"}', $this->document->toJson()); + } +} diff --git a/tests/Document/ConsumerTest.php b/tests/Document/ConsumerTest.php new file mode 100644 index 0000000..60c754c --- /dev/null +++ b/tests/Document/ConsumerTest.php @@ -0,0 +1,148 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi\Tests\Document; + +use Unikorp\KongAdminApi\Document\Consumer as Document; +use PHPUnit\Framework\TestCase; + +/** + * consumer test + * + * @author VEBER Arnaud + */ +class ConsumerTest extends TestCase +{ + /** + * document + * @var \Unikorp\KongAdminApi\Document\Consumer $document + */ + private $document = null; + + /** + * set up + * + * @return void + * + * @coversNothing + */ + protected function setUp() + { + $this->document = new Document(); + } + + /** + * tear down + * + * @return void + * + * @coversNothing + */ + protected function tearDown() + { + $this->document = null; + } + + /** + * test set username + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Consumer::setUsername + */ + public function testSetUsername() + { + // asserts + $this->document->setUsername('test'); + $this->assertSame('test', $this->readAttribute($this->document, 'username')); + } + + /** + * test get username + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Consumer::getUsername + */ + public function testGetUsername() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `username` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('username'); + $reflectionProperty->setAccessible(true); + + // assert + $reflectionProperty->setValue($this->document, 'test'); + $this->assertSame('test', $this->document->getUsername()); + } + + /** + * test set custom id + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Consumer::setCustomId + */ + public function testCustomId() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `custom id` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('customId'); + $reflectionProperty->setAccessible(true); + + // asserts + $this->document->setCustomId('test'); + $this->assertSame('test', $reflectionProperty->getValue($this->document)); + } + + /** + * test get custom id + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Consumer::getCustomId + */ + public function testGetCustomId() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `custom id` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('customId'); + $reflectionProperty->setAccessible(true); + + // assert + $reflectionProperty->setValue($this->document, 'test'); + $this->assertSame('test', $this->document->getCustomId()); + } + + /** + * test to json + * + * @return void + * + * @covers \Unikorp\KongAdminApi\AbstractDocument::toJson + * @covers \Unikorp\KongAdminApi\Document\Consumer::getFields + * @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase + */ + public function testToJson() + { + $this->document + ->setUsername('username') + ->setCustomId('customId'); + + $this->assertSame('{"username":"username","custom_id":"customId"}', $this->document->toJson()); + } +} diff --git a/tests/Document/InformationTest.php b/tests/Document/InformationTest.php new file mode 100644 index 0000000..6cbd901 --- /dev/null +++ b/tests/Document/InformationTest.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi\Tests\Document; + +use Unikorp\KongAdminApi\Document\Information as Document; +use PHPUnit\Framework\TestCase; + +/** + * information test + * + * @author VEBER Arnaud + */ +class InformationTest extends TestCase +{ + /** + * document + * @var \Unikorp\KongAdminApi\Document\Information $document + */ + private $document = null; + + /** + * set up + * + * @return void + * + * @coversNothing + */ + protected function setUp() + { + $this->document = new Document(); + } + + /** + * tear down + * + * @return void + * + * @coversNothing + */ + protected function tearDown() + { + $this->document = null; + } + + /** + * test to json + * + * @return void + * + * @covers \Unikorp\KongAdminApi\AbstractDocument::toJson + * @covers \Unikorp\KongAdminApi\Document\Information::getFields + */ + public function testToJson() + { + $this->assertSame('[]', $this->document->toJson()); + } +} diff --git a/tests/Document/PluginTest.php b/tests/Document/PluginTest.php new file mode 100644 index 0000000..dcd2955 --- /dev/null +++ b/tests/Document/PluginTest.php @@ -0,0 +1,231 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Unikorp\KongAdminApi\Tests\Document; + +use Unikorp\KongAdminApi\Document\Plugin as Document; +use PHPUnit\Framework\TestCase; + +/** + * plugin test + * + * @author VEBER Arnaud + */ +class PluginTest extends TestCase +{ + /** + * document + * @var \Unikorp\KongAdminApi\Document\Plugin $document + */ + private $document = null; + + /** + * set up + * + * @return void + * + * @coversNothing + */ + protected function setUp() + { + $this->document = new Document(); + } + + /** + * tear down + * + * @return void + * + * @coversNothing + */ + protected function tearDown() + { + $this->document = null; + } + + /** + * test set name + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Plugin::setName + */ + public function testSetName() + { + // asserts + $this->document->setName('test'); + $this->assertSame('test', $this->readAttribute($this->document, 'name')); + } + + /** + * test get name + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Plugin::getName + */ + public function testGetName() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `name` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('name'); + $reflectionProperty->setAccessible(true); + + // assert + $reflectionProperty->setValue($this->document, 'test'); + $this->assertSame('test', $this->document->getName()); + } + + /** + * test set consumer id + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Plugin::setConsumerId + */ + public function testSetConsumerId() + { + // asserts + $this->document->setConsumerId('test'); + $this->assertSame('test', $this->readAttribute($this->document, 'consumerId')); + } + + /** + * test get consumer id + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Plugin::getConsumerId + */ + public function testGetConsumerId() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `consumer id` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('consumerId'); + $reflectionProperty->setAccessible(true); + + // assert + $reflectionProperty->setValue($this->document, 'test'); + $this->assertSame('test', $this->document->getConsumerId()); + } + + /** + * test add config + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Plugin::addConfig + */ + public function testAddConfig() + { + // assert + $this->document + ->addConfig('test', 'test') + ->addConfig('something_else', 'something_else'); + $this->assertSame( + ['test' => 'test', 'something_else' => 'something_else'], + $this->readAttribute($this->document, 'config') + ); + } + + /** + * test add config when name already set + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Plugin::addConfig + * + * @expectedException \RuntimeException + * @expectedExceptionMessage Config for name `test` already set + */ + public function testAddConfigWhenNameAlreadySet() + { + // assert + $this->document + ->addConfig('test', 'first') + ->addConfig('test', 'second'); + } + + /** + * test remove config + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Plugin::removeConfig + */ + public function testRemoveConfig() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `config` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('config'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue( + $this->document, + ['test' => 'test', 'something_else' => 'something_else'] + ); + + // assert + $this->document->removeConfig('test'); + $this->assertSame(['something_else' => 'something_else'], $this->readAttribute($this->document, 'config')); + } + + /** + * test get config + * + * @return void + * + * @covers \Unikorp\KongAdminApi\Document\Plugin::getConfig + */ + public function testGetConfig() + { + // reflect `document` + $reflectionClass = new \ReflectionClass($this->document); + + // set `config` property from `document` accessible + $reflectionProperty = $reflectionClass->getProperty('config'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue($this->document, ['test' => 'test']); + + // assert + $this->assertSame('test', $this->document->getConfig('test')); + } + + /** + * test to json + * + * @return void + * + * @covers \Unikorp\KongAdminApi\AbstractDocument::toJson + * @covers \Unikorp\KongAdminApi\Document\Plugin::getFields + * @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase + */ + public function testToJson() + { + $this->document + ->setName('name') + ->setConsumerId('consumerId') + ->addConfig('test', true) + ->addConfig('something_else', 'something_else'); + + $this->assertSame( + '{"name":"name","consumer_id":"consumerId","config.test":true,"config.something_else":"something_else"}', + $this->document->toJson() + ); + } +}