From a476813044d6d5128dec00c9a2dfbcd282ab3764 Mon Sep 17 00:00:00 2001 From: Nasrul Hazim Bin Mohamad Date: Tue, 5 Nov 2024 12:05:38 +0800 Subject: [PATCH] Update configuration --- src/Configuration.php | 55 ++++++++++++++++++++++++++++++++++++- tests/ConfigurationTest.php | 9 +++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index 0396189..544f2ad 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -2,18 +2,43 @@ namespace CleaniqueCoders\KongAdminApi; +/** + * Class Configuration + * + * This class holds configuration settings for connecting to the Kong Admin API. + */ class Configuration { + /** + * Base URL for Kong Admin. + */ protected string $base; + /** + * URI for Kong Admin API. + */ protected string $uri; + /** + * API key for authentication. + */ protected string $apiKey; + /** + * API key name for authentication. + */ protected string $keyName; + /** + * Headers for API requests. + * + * @var array + */ protected array $headers; + /** + * Whether to verify SSL certificates. + */ protected bool $verify; /** @@ -23,7 +48,7 @@ class Configuration * @param string $uri URI for Kong Admin API * @param string $apiKey API key for authentication * @param string $keyName API key name for authentication - * @param array $headers Headers for requests + * @param array $headers Headers for requests * @param bool $verify Whether to verify SSL certificates */ public function __construct( @@ -45,33 +70,61 @@ public function __construct( $this->verify = $verify; } + /** + * Get the base URL for Kong Admin. + */ public function getBase(): string { return $this->base; } + /** + * Get the URI for Kong Admin API. + */ public function getUri(): string { return $this->uri; } + /** + * Get the API key for authentication. + */ public function getApiKey(): string { return $this->apiKey; } + /** + * Get the API key name used for authentication. + */ public function getKeyName(): string { return $this->keyName; } + /** + * Get the headers for API requests. + * + * @return array + */ public function getHeaders(): array { return $this->headers; } + /** + * Determine if SSL certificates should be verified. + */ public function shouldVerify(): bool { return $this->verify; } + + /** + * Get the full URL for the Kong Admin API, combining base and URI. + */ + public function getUrl(): string + { + return rtrim($this->getBase(), '/').'/'.trim($this->getUri(), '/'); + } } diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index 90c002c..25e1595 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -21,9 +21,10 @@ expect($config->getKeyName())->toBe($keyName); expect($config->getHeaders())->toBe($headers); expect($config->shouldVerify())->toBeTrue(); + expect($config->getUrl())->toBe('http://localhost/admin'); }); -it('uses default keyName, headers, and verify if not provided', function () { +it('uses default values for keyName, headers, and verify if not provided', function () { $base = 'http://localhost'; $uri = '/admin'; $apiKey = 'test-api-key'; @@ -39,6 +40,12 @@ 'Accept' => 'application/json', ]); // Default headers expect($config->shouldVerify())->toBeFalse(); // Default verify + expect($config->getUrl())->toBe('http://localhost/admin'); // URL combination +}); + +it('correctly combines base and uri in getUrl', function () { + $config = new Configuration('http://localhost/', '/admin', 'test-api-key'); + expect($config->getUrl())->toBe('http://localhost/admin'); }); it('throws an error when required parameters are missing', function () {