Skip to content

Commit

Permalink
Added ApiTest
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrulhazim committed Nov 5, 2024
1 parent f7d1369 commit 8780674
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

use CleaniqueCoders\KongAdminApi\Api\ApiClient;
use CleaniqueCoders\KongAdminApi\Api\ApiRequest;
use CleaniqueCoders\KongAdminApi\Api\ApiResponse;
use CleaniqueCoders\KongAdminApi\Configuration;
use CleaniqueCoders\KongAdminApi\Connector;
use DateTime;

Check warning on line 8 in tests/ApiTest.php

View workflow job for this annotation

GitHub Actions / P8.3 - prefer-lowest - ubuntu-latest

The use statement with non-compound name 'DateTime' has no effect
use Saloon\Enums\Method;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;

// Set up reusable configuration and connector
beforeEach(function () {
$this->configuration = new Configuration(
base: 'https://kong-admin.com',
uri: 'api',
apiKey: 'test-api-key',
keyName: 'apikey'
);

$this->connector = new Connector($this->configuration);
});

it('initializes the ApiClient with Connector', function () {
$client = new ApiClient($this->connector);

expect($client)->toBeInstanceOf(ApiClient::class);
});

it('sends an API request and processes the response', function () {
$mockClient = new MockClient([
MockResponse::make(['result' => 'success'], 200, [
'Content-Type' => 'application/json',
'apikey' => 'test-api-key',
]),
]);

$this->connector->withMockClient($mockClient);

$client = new ApiClient($this->connector);
$client->setPath('example-path');

$response = $client->sendRequest(Method::GET);

expect($response)->toBeInstanceOf(ApiResponse::class)
->and($response->getStatusCode())->toBe('200')
->and($response->getStatusPhrase())->toBe('OK')
->and($response->getData())->toBe(['result' => 'success'])
->and($response->toArray())->toMatchArray([
'status' => ['code' => '200', 'phrase' => 'OK'],
'data' => ['result' => 'success'],
]);
});

it('sets and retrieves data in ApiRequest', function () {
$request = new ApiRequest;
$request->setMethod(Method::POST)
->setEndpoint('example-endpoint')
->body()->set(['key' => 'value']);

expect($request->getMethod())->toBe(Method::POST)
->and($request->getEndpoint())->toBe('example-endpoint')
->and($request->body()->all())->toBe(['key' => 'value']);
});

it('initializes ApiResponse with correct data and converts to array', function () {
$statusCode = '200';
$statusPhrase = 'OK';
$data = ['result' => 'success'];
$respondedAt = new DateTime;

$response = new ApiResponse($statusCode, $statusPhrase, $data, $respondedAt);

expect($response->getStatusCode())->toBe($statusCode)
->and($response->getStatusPhrase())->toBe($statusPhrase)
->and($response->getData())->toBe($data)
->and($response->getRespondedAt())->toBeInstanceOf(DateTime::class)
->and($response->toArray())->toMatchArray([
'status' => ['code' => '200', 'phrase' => 'OK'],
'data' => $data,
'meta' => [
'responded_at' => $respondedAt->format('Y-m-d H:i:s'),
],
]);
});

0 comments on commit 8780674

Please sign in to comment.