ldc-arcadia-genesis-php-client/tests/Unit/Lib/ApiConfigTest.php

145 lines
5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Lib;
use GuzzleHttp\Psr7\Uri;
use OpenTelemetry\API\Trace\TracerInterface;
use Psr\Log\NullLogger;
use Uwil\ArcadiaApiPhpClient\Auth\ClientIdentifier;
use Uwil\ArcadiaApiPhpClient\Auth\ClientSecret;
use Uwil\ArcadiaApiPhpClient\Lib\ApiConfig;
describe('ApiConfig test', function () {
it('can instanciate ApiConfig', function () {
// GIVEN
$apiConfig = new ApiConfig(
endpoint: new Uri('https://example.com'),
apiKey: new ClientIdentifier('test_id'),
secret: new ClientSecret('test_secret'),
ignoreSelfSignedCert: true
);
expect($apiConfig)->toBeInstanceOf(ApiConfig::class);
});
it('can instance ApiConfig whit logger and tracer', function () {
// GIVEN
$logger = new NullLogger();
$tracer = $this->createMock(TracerInterface::class);
// WHEN
$apiConfig = new ApiConfig(
endpoint: new Uri('https://example.com'),
apiKey: new ClientIdentifier('test_id'),
secret: new ClientSecret('test_secret'),
ignoreSelfSignedCert: true,
logger: $logger,
tracer: $tracer
);
// THEN
expect($apiConfig)->toBeInstanceOf(ApiConfig::class);
});
});
describe('ApiConfig validation rules', function () {
it('throws an exception when endpoint is not http or https', function () {
// GIVEN
$invalidEndpoint = new Uri('ftp://example.com');
$validApiKey = new ClientIdentifier('valid-client-id');
$validSecret = new ClientSecret('valid-client-secret');
// WHEN
$createInvalidApiConfig = function () use ($invalidEndpoint, $validApiKey, $validSecret) {
new ApiConfig(
endpoint: $invalidEndpoint,
apiKey: $validApiKey,
secret: $validSecret,
ignoreSelfSignedCert: true
);
};
// THEN
expect($createInvalidApiConfig)->toThrow(\InvalidArgumentException::class);
});
it('throws an exception when curlTimeOut is not positive', function () {
// GIVEN
$invalidEndpoint = new Uri('https://example.com');
$validApiKey = new ClientIdentifier('valid-client-id');
$validSecret = new ClientSecret('valid-client-secret');
// WHEN
$createInvalidApiConfig = function () use ($invalidEndpoint, $validApiKey, $validSecret) {
new ApiConfig(
endpoint: $invalidEndpoint,
apiKey: $validApiKey,
secret: $validSecret,
ignoreSelfSignedCert: true,
curlTimeOut: 0
);
};
$createInvalidApiConfig2 = function () use ($invalidEndpoint, $validApiKey, $validSecret) {
new ApiConfig(
endpoint: $invalidEndpoint,
apiKey: $validApiKey,
secret: $validSecret,
ignoreSelfSignedCert: true,
curlTimeOut: -1
);
};
// THEN
expect($createInvalidApiConfig)->toThrow(\InvalidArgumentException::class);
expect($createInvalidApiConfig2)->toThrow(\InvalidArgumentException::class);
});
it('throws an exception when maxRetries is negative', function () {
// GIVEN
$invalidEndpoint = new Uri('https://example.com');
$validApiKey = new ClientIdentifier('valid-client-id');
$validSecret = new ClientSecret('valid-client-secret');
$invalidMaxRetries = -1;
// WHEN
$createInvalidApiConfig = function () use ($invalidEndpoint, $validApiKey, $validSecret, $invalidMaxRetries) {
new ApiConfig(
endpoint: $invalidEndpoint,
apiKey: $validApiKey,
secret: $validSecret,
ignoreSelfSignedCert: true,
curlTimeOut: 30,
maxRetries: $invalidMaxRetries
);
};
// THEN
expect($createInvalidApiConfig)->toThrow(\InvalidArgumentException::class);
});
it('throws an exception when retryDelaySeconds is negative', function () {
// GIVEN
$invalidEndpoint = new Uri('https://example.com');
$validApiKey = new ClientIdentifier('valid-client-id');
$validSecret = new ClientSecret('valid-client-secret');
$invalidRetryDelaySeconds = -1;
// WHEN
$createInvalidApiConfig = function () use ($invalidEndpoint, $validApiKey, $validSecret, $invalidRetryDelaySeconds) {
new ApiConfig(
endpoint: $invalidEndpoint,
apiKey: $validApiKey,
secret: $validSecret,
ignoreSelfSignedCert: true,
curlTimeOut: 30,
maxRetries: 3,
retryDelaySeconds: $invalidRetryDelaySeconds
);
};
// THEN
expect($createInvalidApiConfig)->toThrow(\InvalidArgumentException::class);
});
});