Files
server/tests/php/Unit/SystemStore/SystemStoreContractsTest.php
T
2026-08-28 20:25:58 -04:00

84 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXT\Unit\SystemStore;
use DateTimeImmutable;
use InvalidArgumentException;
use KTXF\Resource\Provider\ProviderInterface;
use KTXF\Resource\Provider\ResourceProviderBaseInterface;
use KTXF\Resource\Provider\ResourceServiceBaseInterface;
use KTXF\SystemStore\BlobInfo;
use KTXF\SystemStore\Provider\ProviderBaseInterface;
use KTXF\SystemStore\Service\SystemStoreServiceInterface;
use KTXF\SystemStore\WriteCondition;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
final class SystemStoreContractsTest extends TestCase
{
#[Test]
public function registersTheSystemStoreProviderType(): void
{
self::assertSame('system-store', ProviderInterface::TYPE_SYSTEM_STORE);
}
#[Test]
public function providerAndServiceContractsExtendResourceContracts(): void
{
self::assertTrue((new ReflectionClass(ProviderBaseInterface::class))->isSubclassOf(ResourceProviderBaseInterface::class));
self::assertTrue((new ReflectionClass(SystemStoreServiceInterface::class))->isSubclassOf(ResourceServiceBaseInterface::class));
}
#[Test]
public function listingIsAProviderPaginatedIterable(): void
{
$method = (new ReflectionClass(SystemStoreServiceInterface::class))->getMethod('list');
self::assertSame('iterable', (string) $method->getReturnType());
self::assertCount(1, $method->getParameters());
}
#[Test]
public function createsSupportedWriteConditions(): void
{
self::assertSame(WriteCondition::NONE, WriteCondition::none()->mode);
self::assertSame(WriteCondition::IF_ABSENT, WriteCondition::ifAbsent()->mode);
$condition = WriteCondition::ifMatch('revision-1');
self::assertSame(WriteCondition::IF_MATCH, $condition->mode);
self::assertSame('revision-1', $condition->etag);
}
#[Test]
public function rejectsAnEmptyIfMatchEtag(): void
{
$this->expectException(InvalidArgumentException::class);
WriteCondition::ifMatch('');
}
#[Test]
public function serializesMetadata(): void
{
$metadata = new BlobInfo(
key: 'previews/example.webp',
mimeType: 'image/webp',
size: 123,
etag: 'revision-1',
modifiedAt: new DateTimeImmutable('2026-08-28T12:00:00+00:00'),
attributes: ['variant' => 'popover'],
);
self::assertSame([
'key' => 'previews/example.webp',
'mime' => 'image/webp',
'size' => 123,
'etag' => 'revision-1',
'modifiedAt' => '2026-08-28T12:00:00+00:00',
'attributes' => ['variant' => 'popover'],
], $metadata->jsonSerialize());
}
}