Files
michaelschiemer/tests/Unit/Domain/Cms/ValueObjects/ContentBlocksTest.php
Michael Schiemer 2d53270056 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)
2025-11-10 02:12:28 +01:00

178 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
use App\Domain\Cms\ValueObjects\BlockData;
use App\Domain\Cms\ValueObjects\BlockId;
use App\Domain\Cms\ValueObjects\BlockType;
use App\Domain\Cms\ValueObjects\ContentBlock;
use App\Domain\Cms\ValueObjects\ContentBlocks;
describe('ContentBlocks', function () {
it('can be created as empty', function () {
$blocks = ContentBlocks::empty();
expect($blocks->isEmpty())->toBeTrue();
expect($blocks->count())->toBe(0);
});
it('can be created from array', function () {
$blocks = ContentBlocks::fromArray([
[
'id' => 'hero-1',
'type' => 'hero',
'data' => ['title' => 'Hero'],
],
[
'id' => 'text-1',
'type' => 'text',
'data' => ['content' => 'Text'],
],
]);
expect($blocks->count())->toBe(2);
expect($blocks->isEmpty())->toBeFalse();
});
it('can add blocks', function () {
$blocks = ContentBlocks::empty();
$block = ContentBlock::create(
type: BlockType::hero(),
blockId: BlockId::fromString('hero-1'),
data: BlockData::fromArray(['title' => 'Hero'])
);
$newBlocks = $blocks->add($block);
expect($newBlocks->count())->toBe(1);
expect($blocks->count())->toBe(0); // Original unchanged
});
it('throws exception when adding duplicate block ID', function () {
$block = ContentBlock::create(
type: BlockType::hero(),
blockId: BlockId::fromString('hero-1'),
data: BlockData::fromArray(['title' => 'Hero'])
);
$blocks = ContentBlocks::fromArray([$block->toArray()]);
expect(fn () => $blocks->add($block))
->toThrow(\InvalidArgumentException::class);
});
it('can remove blocks', function () {
$blocks = ContentBlocks::fromArray([
[
'id' => 'hero-1',
'type' => 'hero',
'data' => ['title' => 'Hero'],
],
[
'id' => 'text-1',
'type' => 'text',
'data' => ['content' => 'Text'],
],
]);
$newBlocks = $blocks->remove(BlockId::fromString('hero-1'));
expect($newBlocks->count())->toBe(1);
expect($blocks->count())->toBe(2); // Original unchanged
expect($newBlocks->findById(BlockId::fromString('text-1')))->not->toBeNull();
});
it('can find block by ID', function () {
$blocks = ContentBlocks::fromArray([
[
'id' => 'hero-1',
'type' => 'hero',
'data' => ['title' => 'Hero'],
],
[
'id' => 'text-1',
'type' => 'text',
'data' => ['content' => 'Text'],
],
]);
$found = $blocks->findById(BlockId::fromString('hero-1'));
expect($found)->not->toBeNull();
expect($found->blockId->toString())->toBe('hero-1');
$notFound = $blocks->findById(BlockId::fromString('missing'));
expect($notFound)->toBeNull();
});
it('can be iterated', function () {
$blocks = ContentBlocks::fromArray([
[
'id' => 'hero-1',
'type' => 'hero',
'data' => ['title' => 'Hero'],
],
[
'id' => 'text-1',
'type' => 'text',
'data' => ['content' => 'Text'],
],
]);
$count = 0;
foreach ($blocks as $block) {
expect($block)->toBeInstanceOf(ContentBlock::class);
$count++;
}
expect($count)->toBe(2);
});
it('can convert to array', function () {
$blocks = ContentBlocks::fromArray([
[
'id' => 'hero-1',
'type' => 'hero',
'data' => ['title' => 'Hero'],
],
]);
$array = $blocks->toArray();
expect($array)->toBeArray();
expect($array)->toHaveCount(1);
expect($array[0]['id'])->toBe('hero-1');
});
it('throws exception when creating with duplicate block IDs', function () {
expect(fn () => ContentBlocks::fromArray([
[
'id' => 'hero-1',
'type' => 'hero',
'data' => ['title' => 'Hero'],
],
[
'id' => 'hero-1',
'type' => 'text',
'data' => ['content' => 'Text'],
],
]))->toThrow(InvalidArgumentException::class, 'Duplicate block ID');
});
it('can get all blocks', function () {
$blocks = ContentBlocks::fromArray([
[
'id' => 'hero-1',
'type' => 'hero',
'data' => ['title' => 'Hero'],
],
]);
$allBlocks = $blocks->getBlocks();
expect($allBlocks)->toBeArray();
expect($allBlocks)->toHaveCount(1);
expect($allBlocks[0])->toBeInstanceOf(ContentBlock::class);
});
});