feat(cms,asset): add comprehensive test suite and finalize modules

- Add comprehensive test suite for CMS and Asset modules using Pest Framework
- Implement ContentTypeService::delete() protection against deletion of in-use content types
- Add CannotDeleteContentTypeInUseException for better error handling
- Fix DerivatPipelineRegistry::getAllPipelines() to handle object uniqueness correctly
- Fix VariantName::getScale() to correctly parse scales with file extensions
- Update CMS module documentation with new features, exceptions, and test coverage
- Add CmsTestHelpers and AssetTestHelpers for test data factories
- Fix BlockTypeRegistry to be immutable after construction
- Update ContentTypeService to check for associated content before deletion
- Improve BlockRendererRegistry initialization

Test coverage:
- Value Objects: All CMS and Asset value objects
- Services: ContentService, ContentTypeService, SlugGenerator, BlockValidator, ContentLocalizationService, AssetService, DeduplicationService, MetadataExtractor
- Repositories: All database repositories with mocked connections
- Rendering: Block renderers and ContentRenderer
- Controllers: API endpoints for both modules

254 tests passing, 38 remaining (mostly image processing pipeline tests)
This commit is contained in:
2025-11-10 02:12:28 +01:00
parent 74d50a29cc
commit 2d53270056
53 changed files with 5699 additions and 15 deletions

View File

@@ -0,0 +1,126 @@
<?php
declare(strict_types=1);
use App\Domain\Asset\Entities\Asset;
use App\Domain\Asset\Repositories\DatabaseAssetRepository;
use App\Domain\Asset\ValueObjects\AssetId;
use App\Framework\Core\ValueObjects\Hash;
use App\Framework\Core\ValueObjects\HashAlgorithm;
use App\Framework\Database\ConnectionInterface;
use App\Framework\Database\ResultInterface;
describe('DatabaseAssetRepository', function () {
beforeEach(function () {
$this->clock = new \App\Framework\DateTime\SystemClock();
$this->connection = Mockery::mock(ConnectionInterface::class);
$this->repository = new DatabaseAssetRepository($this->connection);
});
it('saves asset to database', function () {
$asset = AssetTestHelpers::createAsset($this->clock);
$this->connection->shouldReceive('execute')
->once()
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
->andReturn(1);
$this->repository->save($asset);
});
it('finds asset by id', function () {
$assetId = AssetId::generate($this->clock);
$row = [
'id' => $assetId->toString(),
'bucket' => 'media',
'key' => 'orig/2025/01/15/test.jpg',
'mime' => 'image/jpeg',
'bytes' => 1024,
'sha256' => Hash::create('test', HashAlgorithm::SHA256)->toString(),
'meta' => json_encode(['width' => 1920, 'height' => 1080]),
'created_at' => '2025-01-15 10:00:00',
];
$result = Mockery::mock(ResultInterface::class);
$result->shouldReceive('fetch')
->once()
->andReturn($row);
$this->connection->shouldReceive('query')
->once()
->andReturn($result);
$found = $this->repository->findById($assetId);
expect($found)->toBeInstanceOf(Asset::class);
expect($found->id->equals($assetId))->toBeTrue();
});
it('finds asset by SHA256 hash', function () {
$hash = Hash::create('test-content', HashAlgorithm::SHA256);
$row = [
'id' => AssetId::generate($this->clock)->toString(),
'bucket' => 'media',
'key' => 'orig/2025/01/15/test.jpg',
'mime' => 'image/jpeg',
'bytes' => 1024,
'sha256' => $hash->toString(),
'meta' => json_encode([]),
'created_at' => '2025-01-15 10:00:00',
];
$result = Mockery::mock(ResultInterface::class);
$result->shouldReceive('fetch')
->once()
->andReturn($row);
$this->connection->shouldReceive('query')
->once()
->andReturn($result);
$found = $this->repository->findBySha256($hash);
expect($found)->toBeInstanceOf(Asset::class);
expect($found->sha256->equals($hash))->toBeTrue();
});
it('finds assets by bucket', function () {
$bucket = \App\Framework\Storage\ValueObjects\BucketName::fromString('media');
$row = [
'id' => AssetId::generate($this->clock)->toString(),
'bucket' => 'media',
'key' => 'orig/2025/01/15/test.jpg',
'mime' => 'image/jpeg',
'bytes' => 1024,
'sha256' => Hash::create('test', HashAlgorithm::SHA256)->toString(),
'meta' => json_encode([]),
'created_at' => '2025-01-15 10:00:00',
];
$result = Mockery::mock(ResultInterface::class);
$result->shouldReceive('fetchAll')
->once()
->andReturn([$row]);
$this->connection->shouldReceive('query')
->once()
->andReturn($result);
$found = $this->repository->findByBucket($bucket);
expect($found)->toBeArray();
expect($found)->toHaveCount(1);
});
it('deletes asset', function () {
$assetId = AssetId::generate($this->clock);
$this->connection->shouldReceive('execute')
->once()
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
->andReturn(1);
$this->repository->delete($assetId);
});
});

View File

@@ -0,0 +1,123 @@
<?php
declare(strict_types=1);
use App\Domain\Asset\Entities\AssetTag;
use App\Domain\Asset\Repositories\DatabaseAssetTagRepository;
use App\Domain\Asset\ValueObjects\AssetId;
use App\Framework\Database\ConnectionInterface;
use App\Framework\Database\ResultInterface;
describe('DatabaseAssetTagRepository', function () {
beforeEach(function () {
$this->clock = new \App\Framework\DateTime\SystemClock();
$this->connection = Mockery::mock(ConnectionInterface::class);
$this->repository = new DatabaseAssetTagRepository($this->connection);
});
it('adds tag to asset', function () {
$assetId = AssetId::generate($this->clock);
$tag = 'hero';
$this->connection->shouldReceive('execute')
->once()
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
->andReturn(1);
$this->repository->addTag($assetId, $tag);
});
it('adds multiple tags to asset', function () {
$assetId = AssetId::generate($this->clock);
$tags = ['hero', 'landing-page'];
$this->connection->shouldReceive('execute')
->twice()
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
->andReturn(1);
$this->repository->addTags($assetId, $tags);
});
it('removes tag from asset', function () {
$assetId = AssetId::generate($this->clock);
$tag = 'hero';
$this->connection->shouldReceive('execute')
->once()
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
->andReturn(1);
$this->repository->removeTag($assetId, $tag);
});
it('removes multiple tags from asset', function () {
$assetId = AssetId::generate($this->clock);
$tags = ['hero', 'landing-page'];
$this->connection->shouldReceive('execute')
->twice()
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
->andReturn(1);
$this->repository->removeTags($assetId, $tags);
});
it('finds asset tags by tag name', function () {
$tag = 'hero';
$assetId = AssetId::generate($this->clock);
$row = [
'asset_id' => $assetId->toString(),
'tag' => 'hero',
];
$result = Mockery::mock(ResultInterface::class);
$result->shouldReceive('fetchAll')
->once()
->andReturn([$row]);
$this->connection->shouldReceive('query')
->once()
->andReturn($result);
$found = $this->repository->findByTag($tag);
expect($found)->toBeArray();
expect($found)->toHaveCount(1);
expect($found[0])->toBeInstanceOf(AssetTag::class);
});
it('gets all tags for asset', function () {
$assetId = AssetId::generate($this->clock);
$row = [
'asset_id' => $assetId->toString(),
'tag' => 'hero',
];
$result = Mockery::mock(ResultInterface::class);
$result->shouldReceive('fetchAll')
->once()
->andReturn([$row]);
$this->connection->shouldReceive('query')
->once()
->andReturn($result);
$found = $this->repository->getTags($assetId);
expect($found)->toBeArray();
expect($found)->toHaveCount(1);
});
it('deletes all tags for asset', function () {
$assetId = AssetId::generate($this->clock);
$this->connection->shouldReceive('execute')
->once()
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
->andReturn(1);
$this->repository->deleteByAsset($assetId);
});
});

View File

@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
use App\Domain\Asset\Entities\AssetVariant;
use App\Domain\Asset\Repositories\DatabaseAssetVariantRepository;
use App\Domain\Asset\ValueObjects\AssetId;
use App\Domain\Asset\ValueObjects\VariantName;
use App\Framework\Database\ConnectionInterface;
use App\Framework\Database\ResultInterface;
describe('DatabaseAssetVariantRepository', function () {
beforeEach(function () {
$this->clock = new \App\Framework\DateTime\SystemClock();
$this->connection = Mockery::mock(ConnectionInterface::class);
$this->repository = new DatabaseAssetVariantRepository($this->connection);
});
it('saves variant to database', function () {
$assetId = AssetId::generate($this->clock);
$variant = AssetTestHelpers::createAssetVariant($assetId);
$this->connection->shouldReceive('execute')
->once()
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
->andReturn(1);
$this->repository->save($variant);
});
it('finds variants by asset id', function () {
$assetId = AssetId::generate($this->clock);
$row = [
'asset_id' => $assetId->toString(),
'variant' => '1200w.webp',
'bucket' => 'variants',
'key' => 'variants/2025/01/15/test/1200w.webp',
'mime' => 'image/webp',
'bytes' => 512,
'meta' => json_encode(['width' => 1200, 'height' => 675]),
];
$result = Mockery::mock(ResultInterface::class);
$result->shouldReceive('fetchAll')
->once()
->andReturn([$row]);
$this->connection->shouldReceive('query')
->once()
->andReturn($result);
$found = $this->repository->findByAsset($assetId);
expect($found)->toBeArray();
expect($found)->toHaveCount(1);
expect($found[0])->toBeInstanceOf(AssetVariant::class);
});
it('finds specific variant by asset id and variant name', function () {
$assetId = AssetId::generate($this->clock);
$variantName = VariantName::fromString('1200w.webp');
$row = [
'asset_id' => $assetId->toString(),
'variant' => '1200w.webp',
'bucket' => 'variants',
'key' => 'variants/2025/01/15/test/1200w.webp',
'mime' => 'image/webp',
'bytes' => 512,
'meta' => json_encode(['width' => 1200, 'height' => 675]),
];
$result = Mockery::mock(ResultInterface::class);
$result->shouldReceive('fetch')
->once()
->andReturn($row);
$this->connection->shouldReceive('query')
->once()
->andReturn($result);
$found = $this->repository->findByAssetAndVariant($assetId, $variantName);
expect($found)->toBeInstanceOf(AssetVariant::class);
expect($found->variant->equals($variantName))->toBeTrue();
});
it('deletes all variants for asset', function () {
$assetId = AssetId::generate($this->clock);
$this->connection->shouldReceive('execute')
->once()
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
->andReturn(1);
$this->repository->deleteByAsset($assetId);
});
});