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>
This commit is contained in:
2026-01-22 16:05:28 +01:00
parent 47317abf56
commit af735df260
11 changed files with 2335 additions and 2 deletions

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Magdev\WcLicensedProductClient\Tests\Dto;
use Magdev\WcLicensedProductClient\Dto\ActivationResult;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(ActivationResult::class)]
final class ActivationResultTest extends TestCase
{
#[Test]
public function itCanBeCreatedFromArrayWithSuccess(): void
{
$data = [
'success' => true,
'message' => 'License activated successfully.',
];
$result = ActivationResult::fromArray($data);
self::assertTrue($result->success);
self::assertSame('License activated successfully.', $result->message);
}
#[Test]
public function itCanBeCreatedFromArrayWithAlreadyActivated(): void
{
$data = [
'success' => true,
'message' => 'License is already activated for this domain.',
];
$result = ActivationResult::fromArray($data);
self::assertTrue($result->success);
self::assertSame('License is already activated for this domain.', $result->message);
}
}

View File

@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace Magdev\WcLicensedProductClient\Tests\Dto;
use Magdev\WcLicensedProductClient\Dto\LicenseInfo;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(LicenseInfo::class)]
final class LicenseInfoTest extends TestCase
{
#[Test]
public function itCanBeCreatedFromArray(): void
{
$data = [
'product_id' => 123,
'expires_at' => '2027-01-21',
'version_id' => 5,
];
$licenseInfo = LicenseInfo::fromArray($data);
self::assertSame(123, $licenseInfo->productId);
self::assertInstanceOf(\DateTimeImmutable::class, $licenseInfo->expiresAt);
self::assertSame('2027-01-21', $licenseInfo->expiresAt->format('Y-m-d'));
self::assertSame(5, $licenseInfo->versionId);
}
#[Test]
public function itCanBeCreatedWithNullExpiresAt(): void
{
$data = [
'product_id' => 456,
'expires_at' => null,
'version_id' => null,
];
$licenseInfo = LicenseInfo::fromArray($data);
self::assertSame(456, $licenseInfo->productId);
self::assertNull($licenseInfo->expiresAt);
self::assertNull($licenseInfo->versionId);
}
#[Test]
public function itCanBeCreatedWithoutOptionalFields(): void
{
$data = [
'product_id' => 789,
];
$licenseInfo = LicenseInfo::fromArray($data);
self::assertSame(789, $licenseInfo->productId);
self::assertNull($licenseInfo->expiresAt);
self::assertNull($licenseInfo->versionId);
}
#[Test]
public function itIdentifiesLifetimeLicense(): void
{
$lifetime = LicenseInfo::fromArray(['product_id' => 1, 'expires_at' => null]);
$expiring = LicenseInfo::fromArray(['product_id' => 2, 'expires_at' => '2027-12-31']);
self::assertTrue($lifetime->isLifetime());
self::assertFalse($expiring->isLifetime());
}
}

View File

@@ -0,0 +1,118 @@
<?php
declare(strict_types=1);
namespace Magdev\WcLicensedProductClient\Tests\Dto;
use Magdev\WcLicensedProductClient\Dto\LicenseState;
use Magdev\WcLicensedProductClient\Dto\LicenseStatus;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(LicenseStatus::class)]
#[CoversClass(LicenseState::class)]
final class LicenseStatusTest extends TestCase
{
#[Test]
public function itCanBeCreatedFromArray(): void
{
$data = [
'valid' => true,
'status' => 'active',
'domain' => 'example.com',
'expires_at' => '2027-01-21',
'activations_count' => 1,
'max_activations' => 3,
];
$status = LicenseStatus::fromArray($data);
self::assertTrue($status->valid);
self::assertSame(LicenseState::Active, $status->status);
self::assertSame('example.com', $status->domain);
self::assertInstanceOf(\DateTimeImmutable::class, $status->expiresAt);
self::assertSame('2027-01-21', $status->expiresAt->format('Y-m-d'));
self::assertSame(1, $status->activationsCount);
self::assertSame(3, $status->maxActivations);
}
#[Test]
#[DataProvider('licenseStateProvider')]
public function itParsesAllLicenseStates(string $stateString, string $expectedStateValue): void
{
$data = [
'valid' => false,
'status' => $stateString,
'domain' => 'test.com',
'expires_at' => null,
'activations_count' => 0,
'max_activations' => 1,
];
$status = LicenseStatus::fromArray($data);
self::assertSame($expectedStateValue, $status->status->value);
}
public static function licenseStateProvider(): array
{
return [
'active' => ['active', 'active'],
'inactive' => ['inactive', 'inactive'],
'expired' => ['expired', 'expired'],
'revoked' => ['revoked', 'revoked'],
];
}
#[Test]
public function itIdentifiesLifetimeLicense(): void
{
$lifetime = LicenseStatus::fromArray([
'valid' => true,
'status' => 'active',
'domain' => 'test.com',
'expires_at' => null,
'activations_count' => 1,
'max_activations' => 1,
]);
$expiring = LicenseStatus::fromArray([
'valid' => true,
'status' => 'active',
'domain' => 'test.com',
'expires_at' => '2027-12-31',
'activations_count' => 1,
'max_activations' => 1,
]);
self::assertTrue($lifetime->isLifetime());
self::assertFalse($expiring->isLifetime());
}
#[Test]
public function itChecksAvailableActivations(): void
{
$hasAvailable = LicenseStatus::fromArray([
'valid' => true,
'status' => 'active',
'domain' => 'test.com',
'expires_at' => null,
'activations_count' => 1,
'max_activations' => 3,
]);
$noAvailable = LicenseStatus::fromArray([
'valid' => true,
'status' => 'active',
'domain' => 'test.com',
'expires_at' => null,
'activations_count' => 3,
'max_activations' => 3,
]);
self::assertTrue($hasAvailable->hasAvailableActivations());
self::assertFalse($noAvailable->hasAvailableActivations());
}
}