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:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user