Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrulhazim committed Nov 5, 2024
1 parent 3fd9dc7 commit f6299ae
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 67 deletions.
76 changes: 49 additions & 27 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,69 @@

class Configuration
{
protected string $url;

protected string $key;

protected string $key_name;
protected string $base;
protected string $uri;
protected string $apiKey;
protected string $keyName;
protected array $headers;
protected bool $verify;

/**
* Constructor to initialize the Admin API URL and API key.
* Constructor to initialize the configuration.
*
* @param string $url URL for Kong Admin API
* @param string $key API key for authentication
* @param string $key_name API key Name for authentication
* @param string $base Base URL for Kong Admin
* @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 bool $verify Whether to verify SSL certificates
*/
public function __construct(string $url, string $key, string $key_name = 'apikey')
public function __construct(
string $base,
string $uri,
string $apiKey,
string $keyName = 'apikey',
array $headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/json',
],
bool $verify = false
) {
$this->base = $base;
$this->uri = $uri;
$this->apiKey = $apiKey;
$this->keyName = $keyName;
$this->headers = $headers;
$this->verify = $verify;
}

public function getBase(): string
{
$this->url = $url;
$this->key = $key;
$this->key_name = $key_name;
return $this->base;
}

/**
* Get the Admin API URL.
*/
public function getUrl(): string
public function getUri(): string
{
return $this->url;
return $this->uri;
}

/**
* Get the API key for authentication.
*/
public function getKey(): string
public function getApiKey(): string
{
return $this->key;
return $this->apiKey;
}

/**
* Get the API Key Name use for authentication.
*/
public function getKeyName(): string
{
return $this->key_name;
return $this->keyName;
}

public function getHeaders(): array
{
return $this->headers;
}

public function shouldVerify(): bool
{
return $this->verify;
}
}
40 changes: 0 additions & 40 deletions tests/ConfigTest.php

This file was deleted.

46 changes: 46 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use CleaniqueCoders\KongAdminApi\Configuration;

it('can initialize Configuration with all parameters', function () {
$base = 'http://localhost';
$uri = '/admin';
$apiKey = 'test-api-key';
$keyName = 'custom-api-key';
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/json',
];
$verify = true;

$config = new Configuration($base, $uri, $apiKey, $keyName, $headers, $verify);

expect($config->getBase())->toBe($base);
expect($config->getUri())->toBe($uri);
expect($config->getApiKey())->toBe($apiKey);
expect($config->getKeyName())->toBe($keyName);
expect($config->getHeaders())->toBe($headers);
expect($config->shouldVerify())->toBeTrue();
});

it('uses default keyName, headers, and verify if not provided', function () {
$base = 'http://localhost';
$uri = '/admin';
$apiKey = 'test-api-key';

$config = new Configuration($base, $uri, $apiKey);

expect($config->getBase())->toBe($base);
expect($config->getUri())->toBe($uri);
expect($config->getApiKey())->toBe($apiKey);
expect($config->getKeyName())->toBe('apikey'); // Default key name
expect($config->getHeaders())->toBe([
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/json',
]); // Default headers
expect($config->shouldVerify())->toBeFalse(); // Default verify
});

it('throws an error when required parameters are missing', function () {
new Configuration();
})->throws(TypeError::class);

0 comments on commit f6299ae

Please sign in to comment.