Files
wc-licensed-product-client/tests/Exception/LicenseExceptionTest.php
magdev af735df260 Add PHPUnit test suite
- Add PHPUnit 11.0 as dev dependency
- Add phpunit.xml configuration
- Add DTO tests (LicenseInfo, LicenseStatus, ActivationResult)
- Add Exception tests (factory method, all exception types)
- Add LicenseClient tests with mocked HTTP responses
- Update README with testing instructions
- Update CHANGELOG

32 tests, 93 assertions

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-22 16:05:28 +01:00

122 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Magdev\WcLicensedProductClient\Tests\Exception;
use Magdev\WcLicensedProductClient\Exception\ActivationFailedException;
use Magdev\WcLicensedProductClient\Exception\DomainMismatchException;
use Magdev\WcLicensedProductClient\Exception\LicenseException;
use Magdev\WcLicensedProductClient\Exception\LicenseExpiredException;
use Magdev\WcLicensedProductClient\Exception\LicenseInactiveException;
use Magdev\WcLicensedProductClient\Exception\LicenseInvalidException;
use Magdev\WcLicensedProductClient\Exception\LicenseNotFoundException;
use Magdev\WcLicensedProductClient\Exception\LicenseRevokedException;
use Magdev\WcLicensedProductClient\Exception\MaxActivationsReachedException;
use Magdev\WcLicensedProductClient\Exception\RateLimitExceededException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(LicenseException::class)]
#[CoversClass(LicenseNotFoundException::class)]
#[CoversClass(LicenseExpiredException::class)]
#[CoversClass(LicenseRevokedException::class)]
#[CoversClass(LicenseInactiveException::class)]
#[CoversClass(LicenseInvalidException::class)]
#[CoversClass(DomainMismatchException::class)]
#[CoversClass(MaxActivationsReachedException::class)]
#[CoversClass(ActivationFailedException::class)]
#[CoversClass(RateLimitExceededException::class)]
final class LicenseExceptionTest extends TestCase
{
#[Test]
#[DataProvider('exceptionMappingProvider')]
public function itCreatesCorrectExceptionFromApiResponse(
string $errorCode,
string $expectedClass,
): void {
$data = [
'error' => $errorCode,
'message' => 'Test message for ' . $errorCode,
];
$exception = LicenseException::fromApiResponse($data, 403);
self::assertInstanceOf($expectedClass, $exception);
self::assertSame('Test message for ' . $errorCode, $exception->getMessage());
self::assertSame($errorCode, $exception->errorCode);
self::assertSame(403, $exception->getCode());
}
public static function exceptionMappingProvider(): array
{
return [
'license_not_found' => ['license_not_found', LicenseNotFoundException::class],
'license_expired' => ['license_expired', LicenseExpiredException::class],
'license_revoked' => ['license_revoked', LicenseRevokedException::class],
'license_inactive' => ['license_inactive', LicenseInactiveException::class],
'license_invalid' => ['license_invalid', LicenseInvalidException::class],
'domain_mismatch' => ['domain_mismatch', DomainMismatchException::class],
'max_activations_reached' => ['max_activations_reached', MaxActivationsReachedException::class],
'activation_failed' => ['activation_failed', ActivationFailedException::class],
];
}
#[Test]
public function itCreatesRateLimitExceptionWithRetryAfter(): void
{
$data = [
'error' => 'rate_limit_exceeded',
'message' => 'Too many requests.',
'retry_after' => 45,
];
$exception = LicenseException::fromApiResponse($data, 429);
self::assertInstanceOf(RateLimitExceededException::class, $exception);
self::assertSame(45, $exception->retryAfter);
}
#[Test]
public function itCreatesGenericExceptionForUnknownErrorCode(): void
{
$data = [
'error' => 'unknown_error',
'message' => 'Something went wrong.',
];
$exception = LicenseException::fromApiResponse($data, 500);
self::assertInstanceOf(LicenseException::class, $exception);
self::assertNotInstanceOf(LicenseNotFoundException::class, $exception);
self::assertSame('unknown_error', $exception->errorCode);
}
#[Test]
public function itHandlesMissingMessageInApiResponse(): void
{
$data = [
'error' => 'license_not_found',
];
$exception = LicenseException::fromApiResponse($data, 404);
self::assertSame('Unknown license error', $exception->getMessage());
}
#[Test]
public function itHandlesMissingErrorCodeInApiResponse(): void
{
$data = [
'message' => 'Some error occurred.',
];
$exception = LicenseException::fromApiResponse($data, 500);
self::assertInstanceOf(LicenseException::class, $exception);
self::assertNull($exception->errorCode);
}
}