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:
110
tests/Unit/Domain/Cms/ValueObjects/BlockDataTest.php
Normal file
110
tests/Unit/Domain/Cms/ValueObjects/BlockDataTest.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\ValueObjects\BlockData;
|
||||
|
||||
describe('BlockData', function () {
|
||||
it('can be created from array', function () {
|
||||
$data = BlockData::fromArray(['title' => 'Test', 'content' => 'Hello']);
|
||||
|
||||
expect($data->toArray())->toBe(['title' => 'Test', 'content' => 'Hello']);
|
||||
});
|
||||
|
||||
it('can be created as empty', function () {
|
||||
$data = BlockData::empty();
|
||||
|
||||
expect($data->toArray())->toBe([]);
|
||||
expect($data->has('key'))->toBeFalse();
|
||||
});
|
||||
|
||||
it('can get values by key', function () {
|
||||
$data = BlockData::fromArray(['title' => 'Test', 'count' => 5]);
|
||||
|
||||
expect($data->get('title'))->toBe('Test');
|
||||
expect($data->get('count'))->toBe(5);
|
||||
expect($data->get('missing'))->toBeNull();
|
||||
expect($data->get('missing', 'default'))->toBe('default');
|
||||
});
|
||||
|
||||
it('can check if key exists', function () {
|
||||
$data = BlockData::fromArray(['title' => 'Test']);
|
||||
|
||||
expect($data->has('title'))->toBeTrue();
|
||||
expect($data->has('missing'))->toBeFalse();
|
||||
});
|
||||
|
||||
it('can add new key-value pair', function () {
|
||||
$data = BlockData::fromArray(['title' => 'Test']);
|
||||
$newData = $data->with('content', 'Hello');
|
||||
|
||||
expect($newData->get('title'))->toBe('Test');
|
||||
expect($newData->get('content'))->toBe('Hello');
|
||||
expect($data->has('content'))->toBeFalse(); // Original unchanged
|
||||
});
|
||||
|
||||
it('can remove key', function () {
|
||||
$data = BlockData::fromArray(['title' => 'Test', 'content' => 'Hello']);
|
||||
$newData = $data->without('title');
|
||||
|
||||
expect($newData->has('title'))->toBeFalse();
|
||||
expect($newData->has('content'))->toBeTrue();
|
||||
expect($data->has('title'))->toBeTrue(); // Original unchanged
|
||||
});
|
||||
|
||||
it('can merge with another array', function () {
|
||||
$data = BlockData::fromArray(['title' => 'Test']);
|
||||
$merged = $data->merge(['content' => 'Hello', 'title' => 'Updated']);
|
||||
|
||||
expect($merged->get('title'))->toBe('Updated');
|
||||
expect($merged->get('content'))->toBe('Hello');
|
||||
});
|
||||
|
||||
it('accepts scalar values', function () {
|
||||
$data = BlockData::fromArray([
|
||||
'string' => 'test',
|
||||
'int' => 123,
|
||||
'float' => 45.67,
|
||||
'bool' => true,
|
||||
'null' => null,
|
||||
]);
|
||||
|
||||
expect($data->get('string'))->toBe('test');
|
||||
expect($data->get('int'))->toBe(123);
|
||||
expect($data->get('float'))->toBe(45.67);
|
||||
expect($data->get('bool'))->toBeTrue();
|
||||
expect($data->get('null'))->toBeNull();
|
||||
});
|
||||
|
||||
it('accepts arrays of scalar values', function () {
|
||||
$data = BlockData::fromArray([
|
||||
'tags' => ['php', 'testing', 'pest'],
|
||||
'numbers' => [1, 2, 3],
|
||||
]);
|
||||
|
||||
expect($data->get('tags'))->toBe(['php', 'testing', 'pest']);
|
||||
expect($data->get('numbers'))->toBe([1, 2, 3]);
|
||||
});
|
||||
|
||||
it('accepts nested arrays', function () {
|
||||
$data = BlockData::fromArray([
|
||||
'config' => [
|
||||
'width' => 100,
|
||||
'height' => 200,
|
||||
],
|
||||
]);
|
||||
|
||||
expect($data->get('config'))->toBe(['width' => 100, 'height' => 200]);
|
||||
});
|
||||
|
||||
it('throws exception for non-string keys', function () {
|
||||
expect(fn () => BlockData::fromArray([0 => 'value']))
|
||||
->toThrow(InvalidArgumentException::class, 'BlockData keys must be strings');
|
||||
});
|
||||
|
||||
it('throws exception for non-serializable values', function () {
|
||||
expect(fn () => BlockData::fromArray(['key' => fopen('php://memory', 'r')]))
|
||||
->toThrow(InvalidArgumentException::class, 'BlockData value for key');
|
||||
});
|
||||
});
|
||||
|
||||
69
tests/Unit/Domain/Cms/ValueObjects/BlockIdTest.php
Normal file
69
tests/Unit/Domain/Cms/ValueObjects/BlockIdTest.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\ValueObjects\BlockId;
|
||||
|
||||
describe('BlockId', function () {
|
||||
it('can be created from valid string', function () {
|
||||
$blockId = BlockId::fromString('hero-1');
|
||||
|
||||
expect($blockId->toString())->toBe('hero-1');
|
||||
expect((string) $blockId)->toBe('hero-1');
|
||||
});
|
||||
|
||||
it('accepts lowercase letters, numbers, hyphens, and underscores', function () {
|
||||
$validIds = ['block-1', 'text_block', 'image123', 'my-block-id'];
|
||||
|
||||
foreach ($validIds as $id) {
|
||||
$blockId = BlockId::fromString($id);
|
||||
expect($blockId->toString())->toBe($id);
|
||||
}
|
||||
});
|
||||
|
||||
it('can generate unique block IDs', function () {
|
||||
$blockId1 = BlockId::generate('hero');
|
||||
$blockId2 = BlockId::generate('hero');
|
||||
|
||||
expect($blockId1->toString())->toStartWith('hero_');
|
||||
expect($blockId2->toString())->toStartWith('hero_');
|
||||
expect($blockId1->toString())->not->toBe($blockId2->toString());
|
||||
});
|
||||
|
||||
it('generates block IDs with custom prefix', function () {
|
||||
$blockId = BlockId::generate('custom');
|
||||
|
||||
expect($blockId->toString())->toStartWith('custom_');
|
||||
});
|
||||
|
||||
it('throws exception for empty string', function () {
|
||||
expect(fn () => BlockId::fromString(''))
|
||||
->toThrow(InvalidArgumentException::class, 'Block ID cannot be empty');
|
||||
});
|
||||
|
||||
it('throws exception for uppercase letters', function () {
|
||||
expect(fn () => BlockId::fromString('Block-1'))
|
||||
->toThrow(InvalidArgumentException::class, 'Block ID must contain only lowercase letters, numbers, hyphens, and underscores');
|
||||
});
|
||||
|
||||
it('throws exception for special characters', function () {
|
||||
expect(fn () => BlockId::fromString('block@1'))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('throws exception for block ID exceeding 100 characters', function () {
|
||||
$longId = str_repeat('a', 101);
|
||||
expect(fn () => BlockId::fromString($longId))
|
||||
->toThrow(InvalidArgumentException::class, 'Block ID cannot exceed 100 characters');
|
||||
});
|
||||
|
||||
it('can compare two BlockIds for equality', function () {
|
||||
$id1 = BlockId::fromString('block-1');
|
||||
$id2 = BlockId::fromString('block-1');
|
||||
$id3 = BlockId::fromString('block-2');
|
||||
|
||||
expect($id1->equals($id2))->toBeTrue();
|
||||
expect($id1->equals($id3))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
102
tests/Unit/Domain/Cms/ValueObjects/BlockSettingsTest.php
Normal file
102
tests/Unit/Domain/Cms/ValueObjects/BlockSettingsTest.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\ValueObjects\BlockSettings;
|
||||
|
||||
describe('BlockSettings', function () {
|
||||
it('can be created from array', function () {
|
||||
$settings = BlockSettings::fromArray(['fullWidth' => true, 'padding' => 20]);
|
||||
|
||||
expect($settings->toArray())->toBe(['fullWidth' => true, 'padding' => 20]);
|
||||
});
|
||||
|
||||
it('can be created as empty', function () {
|
||||
$settings = BlockSettings::empty();
|
||||
|
||||
expect($settings->toArray())->toBe([]);
|
||||
expect($settings->has('key'))->toBeFalse();
|
||||
});
|
||||
|
||||
it('can get values by key', function () {
|
||||
$settings = BlockSettings::fromArray(['fullWidth' => true, 'padding' => 20]);
|
||||
|
||||
expect($settings->get('fullWidth'))->toBeTrue();
|
||||
expect($settings->get('padding'))->toBe(20);
|
||||
expect($settings->get('missing'))->toBeNull();
|
||||
expect($settings->get('missing', 'default'))->toBe('default');
|
||||
});
|
||||
|
||||
it('can check if key exists', function () {
|
||||
$settings = BlockSettings::fromArray(['fullWidth' => true]);
|
||||
|
||||
expect($settings->has('fullWidth'))->toBeTrue();
|
||||
expect($settings->has('missing'))->toBeFalse();
|
||||
});
|
||||
|
||||
it('can add new key-value pair', function () {
|
||||
$settings = BlockSettings::fromArray(['fullWidth' => true]);
|
||||
$newSettings = $settings->with('padding', 20);
|
||||
|
||||
expect($newSettings->get('fullWidth'))->toBeTrue();
|
||||
expect($newSettings->get('padding'))->toBe(20);
|
||||
expect($settings->has('padding'))->toBeFalse(); // Original unchanged
|
||||
});
|
||||
|
||||
it('can remove key', function () {
|
||||
$settings = BlockSettings::fromArray(['fullWidth' => true, 'padding' => 20]);
|
||||
$newSettings = $settings->without('fullWidth');
|
||||
|
||||
expect($newSettings->has('fullWidth'))->toBeFalse();
|
||||
expect($newSettings->has('padding'))->toBeTrue();
|
||||
expect($settings->has('fullWidth'))->toBeTrue(); // Original unchanged
|
||||
});
|
||||
|
||||
it('accepts scalar values', function () {
|
||||
$settings = BlockSettings::fromArray([
|
||||
'string' => 'test',
|
||||
'int' => 123,
|
||||
'float' => 45.67,
|
||||
'bool' => true,
|
||||
'null' => null,
|
||||
]);
|
||||
|
||||
expect($settings->get('string'))->toBe('test');
|
||||
expect($settings->get('int'))->toBe(123);
|
||||
expect($settings->get('float'))->toBe(45.67);
|
||||
expect($settings->get('bool'))->toBeTrue();
|
||||
expect($settings->get('null'))->toBeNull();
|
||||
});
|
||||
|
||||
it('accepts arrays of scalar values', function () {
|
||||
$settings = BlockSettings::fromArray([
|
||||
'classes' => ['container', 'full-width'],
|
||||
'numbers' => [1, 2, 3],
|
||||
]);
|
||||
|
||||
expect($settings->get('classes'))->toBe(['container', 'full-width']);
|
||||
expect($settings->get('numbers'))->toBe([1, 2, 3]);
|
||||
});
|
||||
|
||||
it('accepts nested arrays', function () {
|
||||
$settings = BlockSettings::fromArray([
|
||||
'style' => [
|
||||
'margin' => 10,
|
||||
'padding' => 20,
|
||||
],
|
||||
]);
|
||||
|
||||
expect($settings->get('style'))->toBe(['margin' => 10, 'padding' => 20]);
|
||||
});
|
||||
|
||||
it('throws exception for non-string keys', function () {
|
||||
expect(fn () => BlockSettings::fromArray([0 => 'value']))
|
||||
->toThrow(InvalidArgumentException::class, 'BlockSettings keys must be strings');
|
||||
});
|
||||
|
||||
it('throws exception for non-serializable values', function () {
|
||||
expect(fn () => BlockSettings::fromArray(['key' => fopen('php://memory', 'r')]))
|
||||
->toThrow(InvalidArgumentException::class, 'BlockSettings value for key');
|
||||
});
|
||||
});
|
||||
|
||||
81
tests/Unit/Domain/Cms/ValueObjects/BlockTypeTest.php
Normal file
81
tests/Unit/Domain/Cms/ValueObjects/BlockTypeTest.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\ValueObjects\BlockType;
|
||||
|
||||
describe('BlockType', function () {
|
||||
it('can be created from string', function () {
|
||||
$blockType = BlockType::fromString('hero');
|
||||
|
||||
expect($blockType->toString())->toBe('hero');
|
||||
expect((string) $blockType)->toBe('hero');
|
||||
});
|
||||
|
||||
it('has factory methods for system block types', function () {
|
||||
expect(BlockType::hero()->toString())->toBe('hero');
|
||||
expect(BlockType::text()->toString())->toBe('text');
|
||||
expect(BlockType::image()->toString())->toBe('image');
|
||||
expect(BlockType::gallery()->toString())->toBe('gallery');
|
||||
expect(BlockType::cta()->toString())->toBe('cta');
|
||||
expect(BlockType::video()->toString())->toBe('video');
|
||||
expect(BlockType::form()->toString())->toBe('form');
|
||||
expect(BlockType::columns()->toString())->toBe('columns');
|
||||
expect(BlockType::quote()->toString())->toBe('quote');
|
||||
expect(BlockType::separator()->toString())->toBe('separator');
|
||||
});
|
||||
|
||||
it('marks system block types correctly', function () {
|
||||
expect(BlockType::hero()->isSystem())->toBeTrue();
|
||||
expect(BlockType::text()->isSystem())->toBeTrue();
|
||||
expect(BlockType::fromString('custom-block', false)->isSystem())->toBeFalse();
|
||||
});
|
||||
|
||||
it('throws exception for empty string', function () {
|
||||
expect(fn () => BlockType::fromString(''))
|
||||
->toThrow(InvalidArgumentException::class, 'Block type cannot be empty');
|
||||
});
|
||||
|
||||
it('throws exception for uppercase letters', function () {
|
||||
expect(fn () => BlockType::fromString('Hero'))
|
||||
->toThrow(InvalidArgumentException::class, 'Block type must contain only lowercase letters, numbers, hyphens, and underscores');
|
||||
});
|
||||
|
||||
it('throws exception for special characters', function () {
|
||||
expect(fn () => BlockType::fromString('block@type'))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('throws exception for block type exceeding 50 characters', function () {
|
||||
$longType = str_repeat('a', 51);
|
||||
expect(fn () => BlockType::fromString($longType))
|
||||
->toThrow(InvalidArgumentException::class, 'Block type cannot exceed 50 characters');
|
||||
});
|
||||
|
||||
it('can identify block types that require media', function () {
|
||||
expect(BlockType::hero()->requiresMedia())->toBeTrue();
|
||||
expect(BlockType::image()->requiresMedia())->toBeTrue();
|
||||
expect(BlockType::gallery()->requiresMedia())->toBeTrue();
|
||||
expect(BlockType::video()->requiresMedia())->toBeTrue();
|
||||
expect(BlockType::text()->requiresMedia())->toBeFalse();
|
||||
expect(BlockType::cta()->requiresMedia())->toBeFalse();
|
||||
});
|
||||
|
||||
it('provides labels for block types', function () {
|
||||
expect(BlockType::hero()->getLabel())->toBe('Hero Section');
|
||||
expect(BlockType::text()->getLabel())->toBe('Text Block');
|
||||
expect(BlockType::image()->getLabel())->toBe('Image');
|
||||
expect(BlockType::gallery()->getLabel())->toBe('Gallery');
|
||||
expect(BlockType::cta()->getLabel())->toBe('Call to Action');
|
||||
});
|
||||
|
||||
it('can compare two BlockTypes for equality', function () {
|
||||
$type1 = BlockType::fromString('hero');
|
||||
$type2 = BlockType::fromString('hero');
|
||||
$type3 = BlockType::fromString('text');
|
||||
|
||||
expect($type1->equals($type2))->toBeTrue();
|
||||
expect($type1->equals($type3))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
133
tests/Unit/Domain/Cms/ValueObjects/ContentBlockTest.php
Normal file
133
tests/Unit/Domain/Cms/ValueObjects/ContentBlockTest.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\ValueObjects\BlockData;
|
||||
use App\Domain\Cms\ValueObjects\BlockId;
|
||||
use App\Domain\Cms\ValueObjects\BlockSettings;
|
||||
use App\Domain\Cms\ValueObjects\BlockType;
|
||||
use App\Domain\Cms\ValueObjects\ContentBlock;
|
||||
|
||||
describe('ContentBlock', function () {
|
||||
it('can be created with required fields', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray(['title' => 'Hero Title'])
|
||||
);
|
||||
|
||||
expect($block->type->toString())->toBe('hero');
|
||||
expect($block->blockId->toString())->toBe('hero-1');
|
||||
expect($block->data->get('title'))->toBe('Hero Title');
|
||||
expect($block->settings)->toBeNull();
|
||||
});
|
||||
|
||||
it('can be created with settings', function () {
|
||||
$settings = BlockSettings::fromArray(['fullWidth' => true]);
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray(['title' => 'Hero Title']),
|
||||
settings: $settings
|
||||
);
|
||||
|
||||
expect($block->settings)->not->toBeNull();
|
||||
expect($block->settings->get('fullWidth'))->toBeTrue();
|
||||
});
|
||||
|
||||
it('can be created from array', function () {
|
||||
$block = ContentBlock::fromArray([
|
||||
'id' => 'hero-1',
|
||||
'type' => 'hero',
|
||||
'data' => ['title' => 'Hero Title'],
|
||||
'settings' => ['fullWidth' => true],
|
||||
]);
|
||||
|
||||
expect($block->blockId->toString())->toBe('hero-1');
|
||||
expect($block->type->toString())->toBe('hero');
|
||||
expect($block->data->get('title'))->toBe('Hero Title');
|
||||
expect($block->settings->get('fullWidth'))->toBeTrue();
|
||||
});
|
||||
|
||||
it('can be created from array without settings', function () {
|
||||
$block = ContentBlock::fromArray([
|
||||
'id' => 'text-1',
|
||||
'type' => 'text',
|
||||
'data' => ['content' => 'Hello World'],
|
||||
]);
|
||||
|
||||
expect($block->settings)->toBeNull();
|
||||
});
|
||||
|
||||
it('throws exception when creating from array with missing required fields', function () {
|
||||
expect(fn () => ContentBlock::fromArray(['id' => 'hero-1']))
|
||||
->toThrow(InvalidArgumentException::class, 'Invalid block data: missing required fields');
|
||||
|
||||
expect(fn () => ContentBlock::fromArray(['type' => 'hero']))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
|
||||
expect(fn () => ContentBlock::fromArray(['id' => 'hero-1', 'type' => 'hero']))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('can convert to array', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray(['title' => 'Hero Title']),
|
||||
settings: BlockSettings::fromArray(['fullWidth' => true])
|
||||
);
|
||||
|
||||
$array = $block->toArray();
|
||||
|
||||
expect($array)->toHaveKeys(['id', 'type', 'data', 'settings']);
|
||||
expect($array['id'])->toBe('hero-1');
|
||||
expect($array['type'])->toBe('hero');
|
||||
expect($array['data'])->toBe(['title' => 'Hero Title']);
|
||||
expect($array['settings'])->toBe(['fullWidth' => true]);
|
||||
});
|
||||
|
||||
it('can update data immutably', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray(['title' => 'Old Title'])
|
||||
);
|
||||
|
||||
$newData = BlockData::fromArray(['title' => 'New Title']);
|
||||
$updatedBlock = $block->withData($newData);
|
||||
|
||||
expect($updatedBlock->data->get('title'))->toBe('New Title');
|
||||
expect($block->data->get('title'))->toBe('Old Title'); // Original unchanged
|
||||
});
|
||||
|
||||
it('can update settings immutably', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray(['title' => 'Hero Title'])
|
||||
);
|
||||
|
||||
$newSettings = BlockSettings::fromArray(['fullWidth' => true]);
|
||||
$updatedBlock = $block->withSettings($newSettings);
|
||||
|
||||
expect($updatedBlock->settings)->not->toBeNull();
|
||||
expect($updatedBlock->settings->get('fullWidth'))->toBeTrue();
|
||||
expect($block->settings)->toBeNull(); // Original unchanged
|
||||
});
|
||||
|
||||
it('can remove settings', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray(['title' => 'Hero Title']),
|
||||
settings: BlockSettings::fromArray(['fullWidth' => true])
|
||||
);
|
||||
|
||||
$updatedBlock = $block->withSettings(null);
|
||||
|
||||
expect($updatedBlock->settings)->toBeNull();
|
||||
expect($block->settings)->not->toBeNull(); // Original unchanged
|
||||
});
|
||||
});
|
||||
|
||||
177
tests/Unit/Domain/Cms/ValueObjects/ContentBlocksTest.php
Normal file
177
tests/Unit/Domain/Cms/ValueObjects/ContentBlocksTest.php
Normal file
@@ -0,0 +1,177 @@
|
||||
<?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);
|
||||
});
|
||||
});
|
||||
|
||||
53
tests/Unit/Domain/Cms/ValueObjects/ContentIdTest.php
Normal file
53
tests/Unit/Domain/Cms/ValueObjects/ContentIdTest.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\ValueObjects\ContentId;
|
||||
use App\Framework\DateTime\Clock;
|
||||
use App\Framework\DateTime\SystemClock;
|
||||
|
||||
describe('ContentId', function () {
|
||||
it('can be created from valid ULID string', function () {
|
||||
$validUlid = '01ARZ3NDEKTSV4RRFFQ69G5FAV';
|
||||
$contentId = ContentId::fromString($validUlid);
|
||||
|
||||
expect($contentId->toString())->toBe($validUlid);
|
||||
expect((string) $contentId)->toBe($validUlid);
|
||||
});
|
||||
|
||||
it('throws exception for invalid ULID format', function () {
|
||||
expect(fn () => ContentId::fromString('invalid-id'))
|
||||
->toThrow(InvalidArgumentException::class, 'Invalid Content ID format');
|
||||
});
|
||||
|
||||
it('throws exception for empty string', function () {
|
||||
expect(fn () => ContentId::fromString(''))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('can generate new ContentId with Clock', function () {
|
||||
$clock = new SystemClock();
|
||||
$contentId = ContentId::generate($clock);
|
||||
|
||||
expect($contentId)->toBeInstanceOf(ContentId::class);
|
||||
expect($contentId->toString())->toMatch('/^[0-9A-Z]{26}$/');
|
||||
});
|
||||
|
||||
it('generates unique IDs', function () {
|
||||
$clock = new SystemClock();
|
||||
$id1 = ContentId::generate($clock);
|
||||
$id2 = ContentId::generate($clock);
|
||||
|
||||
expect($id1->toString())->not->toBe($id2->toString());
|
||||
});
|
||||
|
||||
it('can compare two ContentIds for equality', function () {
|
||||
$id1 = ContentId::fromString('01ARZ3NDEKTSV4RRFFQ69G5FAV');
|
||||
$id2 = ContentId::fromString('01ARZ3NDEKTSV4RRFFQ69G5FAV');
|
||||
$id3 = ContentId::fromString('01ARZ3NDEKTSV4RRFFQ69G5FAW');
|
||||
|
||||
expect($id1->equals($id2))->toBeTrue();
|
||||
expect($id1->equals($id3))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
53
tests/Unit/Domain/Cms/ValueObjects/ContentSlugTest.php
Normal file
53
tests/Unit/Domain/Cms/ValueObjects/ContentSlugTest.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\ValueObjects\ContentSlug;
|
||||
|
||||
describe('ContentSlug', function () {
|
||||
it('can be created from valid string', function () {
|
||||
$slug = ContentSlug::fromString('my-awesome-page');
|
||||
|
||||
expect($slug->toString())->toBe('my-awesome-page');
|
||||
expect((string) $slug)->toBe('my-awesome-page');
|
||||
});
|
||||
|
||||
it('accepts lowercase letters, numbers, hyphens, and underscores', function () {
|
||||
$validSlugs = ['page', 'blog-post', 'product_123', 'my-content-slug'];
|
||||
|
||||
foreach ($validSlugs as $slugStr) {
|
||||
$slug = ContentSlug::fromString($slugStr);
|
||||
expect($slug->toString())->toBe($slugStr);
|
||||
}
|
||||
});
|
||||
|
||||
it('throws exception for empty string', function () {
|
||||
expect(fn () => ContentSlug::fromString(''))
|
||||
->toThrow(InvalidArgumentException::class, 'Content slug cannot be empty');
|
||||
});
|
||||
|
||||
it('throws exception for uppercase letters', function () {
|
||||
expect(fn () => ContentSlug::fromString('My-Page'))
|
||||
->toThrow(InvalidArgumentException::class, 'Content slug must contain only lowercase letters, numbers, hyphens, and underscores');
|
||||
});
|
||||
|
||||
it('throws exception for spaces', function () {
|
||||
expect(fn () => ContentSlug::fromString('my page'))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('throws exception for special characters', function () {
|
||||
expect(fn () => ContentSlug::fromString('my@page'))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('can compare two ContentSlugs for equality', function () {
|
||||
$slug1 = ContentSlug::fromString('my-page');
|
||||
$slug2 = ContentSlug::fromString('my-page');
|
||||
$slug3 = ContentSlug::fromString('other-page');
|
||||
|
||||
expect($slug1->equals($slug2))->toBeTrue();
|
||||
expect($slug1->equals($slug3))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
53
tests/Unit/Domain/Cms/ValueObjects/ContentTypeIdTest.php
Normal file
53
tests/Unit/Domain/Cms/ValueObjects/ContentTypeIdTest.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\ValueObjects\ContentTypeId;
|
||||
|
||||
describe('ContentTypeId', function () {
|
||||
it('can be created from valid string', function () {
|
||||
$contentTypeId = ContentTypeId::fromString('landing_page');
|
||||
|
||||
expect($contentTypeId->toString())->toBe('landing_page');
|
||||
expect((string) $contentTypeId)->toBe('landing_page');
|
||||
});
|
||||
|
||||
it('accepts lowercase letters, numbers, and underscores', function () {
|
||||
$validIds = ['page', 'blog_post', 'product123', 'my_content_type'];
|
||||
|
||||
foreach ($validIds as $id) {
|
||||
$contentTypeId = ContentTypeId::fromString($id);
|
||||
expect($contentTypeId->toString())->toBe($id);
|
||||
}
|
||||
});
|
||||
|
||||
it('throws exception for empty string', function () {
|
||||
expect(fn () => ContentTypeId::fromString(''))
|
||||
->toThrow(InvalidArgumentException::class, 'ContentType ID cannot be empty');
|
||||
});
|
||||
|
||||
it('throws exception for uppercase letters', function () {
|
||||
expect(fn () => ContentTypeId::fromString('LandingPage'))
|
||||
->toThrow(InvalidArgumentException::class, 'ContentType ID must contain only lowercase letters, numbers, and underscores');
|
||||
});
|
||||
|
||||
it('throws exception for special characters', function () {
|
||||
expect(fn () => ContentTypeId::fromString('landing-page'))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('throws exception for spaces', function () {
|
||||
expect(fn () => ContentTypeId::fromString('landing page'))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('can compare two ContentTypeIds for equality', function () {
|
||||
$id1 = ContentTypeId::fromString('page');
|
||||
$id2 = ContentTypeId::fromString('page');
|
||||
$id3 = ContentTypeId::fromString('blog');
|
||||
|
||||
expect($id1->equals($id2))->toBeTrue();
|
||||
expect($id1->equals($id3))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
69
tests/Unit/Domain/Cms/ValueObjects/LocaleTest.php
Normal file
69
tests/Unit/Domain/Cms/ValueObjects/LocaleTest.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\ValueObjects\Locale;
|
||||
|
||||
describe('Locale', function () {
|
||||
it('can be created from valid locale string', function () {
|
||||
$locale = Locale::fromString('en');
|
||||
|
||||
expect($locale->toString())->toBe('en');
|
||||
expect((string) $locale)->toBe('en');
|
||||
});
|
||||
|
||||
it('accepts locale with region', function () {
|
||||
$locale = Locale::fromString('en-US');
|
||||
|
||||
expect($locale->toString())->toBe('en-US');
|
||||
expect($locale->getLanguage())->toBe('en');
|
||||
expect($locale->getRegion())->toBe('US');
|
||||
});
|
||||
|
||||
it('has factory methods for common locales', function () {
|
||||
expect(Locale::english()->toString())->toBe('en');
|
||||
expect(Locale::german()->toString())->toBe('de');
|
||||
expect(Locale::french()->toString())->toBe('fr');
|
||||
expect(Locale::spanish()->toString())->toBe('es');
|
||||
});
|
||||
|
||||
it('extracts language from locale', function () {
|
||||
expect(Locale::fromString('en')->getLanguage())->toBe('en');
|
||||
expect(Locale::fromString('en-US')->getLanguage())->toBe('en');
|
||||
expect(Locale::fromString('de-CH')->getLanguage())->toBe('de');
|
||||
});
|
||||
|
||||
it('extracts region from locale with region', function () {
|
||||
expect(Locale::fromString('en-US')->getRegion())->toBe('US');
|
||||
expect(Locale::fromString('de-CH')->getRegion())->toBe('CH');
|
||||
});
|
||||
|
||||
it('returns null for region when locale has no region', function () {
|
||||
expect(Locale::fromString('en')->getRegion())->toBeNull();
|
||||
expect(Locale::fromString('de')->getRegion())->toBeNull();
|
||||
});
|
||||
|
||||
it('throws exception for invalid locale format', function () {
|
||||
expect(fn () => Locale::fromString('invalid'))
|
||||
->toThrow(InvalidArgumentException::class, 'Invalid locale format');
|
||||
|
||||
expect(fn () => Locale::fromString('EN'))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
|
||||
expect(fn () => Locale::fromString('en-us'))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('can compare two Locales for equality', function () {
|
||||
$locale1 = Locale::fromString('en');
|
||||
$locale2 = Locale::fromString('en');
|
||||
$locale3 = Locale::fromString('de');
|
||||
|
||||
expect($locale1->equals($locale2))->toBeTrue();
|
||||
expect($locale1->equals($locale3))->toBeFalse();
|
||||
|
||||
$locale4 = Locale::fromString('en-US');
|
||||
expect($locale1->equals($locale4))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user