You've already forked wc-licensed-product-client
119 lines
3.5 KiB
PHP
119 lines
3.5 KiB
PHP
|
|
<?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());
|
||
|
|
}
|
||
|
|
}
|