generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7d1369
commit 8780674
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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'), | ||
], | ||
]); | ||
}); |