97 lines
2.7 KiB
PHP
97 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Framework\Api\ApiException;
|
|
use App\Framework\Http\Headers;
|
|
use App\Framework\Http\Status;
|
|
use App\Framework\HttpClient\ClientResponse;
|
|
|
|
describe('ApiException', function () {
|
|
it('constructs with message, code, and response', function () {
|
|
$response = new ClientResponse(
|
|
status: Status::BAD_REQUEST,
|
|
headers: new Headers([]),
|
|
body: '{"error": "Invalid request"}'
|
|
);
|
|
|
|
$exception = new ApiException(
|
|
message: 'API Error: Invalid request',
|
|
code: 400,
|
|
response: $response
|
|
);
|
|
|
|
expect($exception->getMessage())->toBe('API Error: Invalid request');
|
|
expect($exception->getCode())->toBe(400);
|
|
expect($exception->getResponse())->toBe($response);
|
|
});
|
|
|
|
it('returns response data as array', function () {
|
|
$response = new ClientResponse(
|
|
status: Status::BAD_REQUEST,
|
|
headers: new Headers([]),
|
|
body: '{"error": "Invalid request", "field": "email"}'
|
|
);
|
|
|
|
$exception = new ApiException(
|
|
message: 'API Error',
|
|
code: 400,
|
|
response: $response
|
|
);
|
|
|
|
$data = $exception->getResponseData();
|
|
|
|
expect($data)->toBeArray();
|
|
expect($data['error'])->toBe('Invalid request');
|
|
expect($data['field'])->toBe('email');
|
|
});
|
|
|
|
it('returns null for invalid JSON response', function () {
|
|
$response = new ClientResponse(
|
|
status: Status::INTERNAL_SERVER_ERROR,
|
|
headers: new Headers([]),
|
|
body: 'Invalid JSON {'
|
|
);
|
|
|
|
$exception = new ApiException(
|
|
message: 'API Error',
|
|
code: 500,
|
|
response: $response
|
|
);
|
|
|
|
expect($exception->getResponseData())->toBeNull();
|
|
});
|
|
|
|
it('returns null for empty response body', function () {
|
|
$response = new ClientResponse(
|
|
status: Status::NO_CONTENT,
|
|
headers: new Headers([]),
|
|
body: ''
|
|
);
|
|
|
|
$exception = new ApiException(
|
|
message: 'API Error',
|
|
code: 204,
|
|
response: $response
|
|
);
|
|
|
|
expect($exception->getResponseData())->toBeNull();
|
|
});
|
|
|
|
it('extends FrameworkException', function () {
|
|
$response = new ClientResponse(
|
|
status: Status::BAD_REQUEST,
|
|
headers: new Headers([]),
|
|
body: '{}'
|
|
);
|
|
|
|
$exception = new ApiException(
|
|
message: 'Test',
|
|
code: 400,
|
|
response: $response
|
|
);
|
|
|
|
expect($exception)->toBeInstanceOf(\App\Framework\Exception\FrameworkException::class);
|
|
});
|
|
});
|