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,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Rendering\BlockRendererInterface;
|
||||
use App\Domain\Cms\Rendering\BlockRendererRegistry;
|
||||
use App\Domain\Cms\Rendering\HeroBlockRenderer;
|
||||
use App\Domain\Cms\Rendering\TextBlockRenderer;
|
||||
|
||||
describe('BlockRendererRegistry', function () {
|
||||
it('can register renderer for specific type', function () {
|
||||
$registry = new BlockRendererRegistry();
|
||||
$renderer = new HeroBlockRenderer();
|
||||
|
||||
$registry->registerForType('hero', $renderer);
|
||||
|
||||
expect($registry->hasRenderer('hero'))->toBeTrue();
|
||||
expect($registry->getRenderer('hero'))->toBe($renderer);
|
||||
});
|
||||
|
||||
it('can register multiple renderers', function () {
|
||||
$registry = new BlockRendererRegistry();
|
||||
$heroRenderer = new HeroBlockRenderer();
|
||||
$textRenderer = new TextBlockRenderer();
|
||||
|
||||
$registry->registerForType('hero', $heroRenderer);
|
||||
$registry->registerForType('text', $textRenderer);
|
||||
|
||||
expect($registry->getRenderer('hero'))->toBe($heroRenderer);
|
||||
expect($registry->getRenderer('text'))->toBe($textRenderer);
|
||||
});
|
||||
|
||||
it('returns null for unregistered block type', function () {
|
||||
$registry = new BlockRendererRegistry();
|
||||
|
||||
expect($registry->getRenderer('unknown'))->toBeNull();
|
||||
expect($registry->hasRenderer('unknown'))->toBeFalse();
|
||||
});
|
||||
|
||||
it('can find renderer by supports method', function () {
|
||||
$registry = new BlockRendererRegistry();
|
||||
$heroRenderer = new HeroBlockRenderer();
|
||||
|
||||
$registry->register($heroRenderer);
|
||||
|
||||
$found = $registry->getRenderer('hero');
|
||||
expect($found)->toBe($heroRenderer);
|
||||
});
|
||||
|
||||
it('can get all registered renderers', function () {
|
||||
$registry = new BlockRendererRegistry();
|
||||
$heroRenderer = new HeroBlockRenderer();
|
||||
$textRenderer = new TextBlockRenderer();
|
||||
|
||||
$registry->registerForType('hero', $heroRenderer);
|
||||
$registry->registerForType('text', $textRenderer);
|
||||
|
||||
$all = $registry->getAllRenderers();
|
||||
|
||||
expect($all)->toHaveCount(2);
|
||||
expect($all['hero'])->toBe($heroRenderer);
|
||||
expect($all['text'])->toBe($textRenderer);
|
||||
});
|
||||
|
||||
it('overwrites renderer when registering same type twice', function () {
|
||||
$registry = new BlockRendererRegistry();
|
||||
$renderer1 = new HeroBlockRenderer();
|
||||
$renderer2 = new HeroBlockRenderer();
|
||||
|
||||
$registry->registerForType('hero', $renderer1);
|
||||
$registry->registerForType('hero', $renderer2);
|
||||
|
||||
expect($registry->getRenderer('hero'))->toBe($renderer2);
|
||||
});
|
||||
});
|
||||
|
||||
161
tests/Unit/Domain/Cms/Rendering/ContentRendererTest.php
Normal file
161
tests/Unit/Domain/Cms/Rendering/ContentRendererTest.php
Normal file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Entities\Content;
|
||||
use App\Domain\Cms\Enums\ContentStatus;
|
||||
use App\Domain\Cms\Rendering\BlockRendererRegistry;
|
||||
use App\Domain\Cms\Rendering\DefaultBlockRenderer;
|
||||
use App\Domain\Cms\Rendering\HeroBlockRenderer;
|
||||
use App\Domain\Cms\Rendering\TextBlockRenderer;
|
||||
use App\Domain\Cms\Services\ContentLocalizationService;
|
||||
use App\Domain\Cms\Services\ContentRenderer;
|
||||
use App\Domain\Cms\ValueObjects\BlockId;
|
||||
use App\Domain\Cms\ValueObjects\BlockType;
|
||||
use App\Domain\Cms\ValueObjects\ContentBlocks;
|
||||
use App\Domain\Cms\ValueObjects\ContentId;
|
||||
use App\Domain\Cms\ValueObjects\ContentSlug;
|
||||
use App\Domain\Cms\ValueObjects\ContentTypeId;
|
||||
use App\Domain\Cms\ValueObjects\Locale;
|
||||
use App\Framework\Core\ValueObjects\Timestamp;
|
||||
use App\Framework\DateTime\SystemClock;
|
||||
use App\Framework\DI\DefaultContainer;
|
||||
use App\Framework\Filesystem\FileStorage;
|
||||
use App\Framework\Cache\Cache;
|
||||
use App\Framework\Cache\Driver\NullCache;
|
||||
use App\Framework\Cache\GeneralCache;
|
||||
use App\Framework\Core\PathProvider;
|
||||
use App\Framework\Serializer\Serializer;
|
||||
use App\Framework\View\ComponentCache;
|
||||
use App\Framework\View\ComponentRenderer;
|
||||
use App\Framework\View\Loading\TemplateLoader;
|
||||
use App\Framework\View\TemplateProcessor;
|
||||
use Tests\Support\CmsTestHelpers;
|
||||
|
||||
describe('ContentRenderer', function () {
|
||||
beforeEach(function () {
|
||||
$this->clock = new SystemClock();
|
||||
$this->blockRegistry = new BlockRendererRegistry();
|
||||
|
||||
// Create minimal real TemplateLoader for testing
|
||||
$pathProvider = new PathProvider(__DIR__ . '/../../../../../');
|
||||
$nullCacheDriver = new NullCache();
|
||||
$serializer = Mockery::mock(Serializer::class);
|
||||
$serializer->shouldReceive('serialize')->andReturnUsing(fn($data) => serialize($data));
|
||||
$serializer->shouldReceive('unserialize')->andReturnUsing(fn($data) => unserialize($data));
|
||||
$cache = new GeneralCache($nullCacheDriver, $serializer);
|
||||
$templateLoader = new TemplateLoader(
|
||||
pathProvider: $pathProvider,
|
||||
cache: $cache,
|
||||
discoveryRegistry: null,
|
||||
templates: [],
|
||||
templatePath: '/src/Framework/View/templates',
|
||||
cacheEnabled: false
|
||||
);
|
||||
|
||||
$componentCache = new ComponentCache('/tmp/test-cache');
|
||||
|
||||
// Create minimal real TemplateProcessor for testing
|
||||
$container = new DefaultContainer();
|
||||
$templateProcessor = new TemplateProcessor(
|
||||
astTransformers: [],
|
||||
stringProcessors: [],
|
||||
container: $container,
|
||||
chainOptimizer: null,
|
||||
compiledTemplateCache: null,
|
||||
performanceTracker: null
|
||||
);
|
||||
|
||||
// Create real FileStorage for testing
|
||||
$fileStorage = new FileStorage(
|
||||
basePath: sys_get_temp_dir() . '/test-components'
|
||||
);
|
||||
|
||||
$this->componentRenderer = new ComponentRenderer(
|
||||
$templateProcessor,
|
||||
$componentCache,
|
||||
$templateLoader,
|
||||
$fileStorage
|
||||
);
|
||||
|
||||
// Create real ContentLocalizationService for testing
|
||||
$contentTranslationRepository = Mockery::mock(\App\Domain\Cms\Repositories\ContentTranslationRepository::class);
|
||||
$this->localizationService = new ContentLocalizationService(
|
||||
$contentTranslationRepository,
|
||||
$this->clock
|
||||
);
|
||||
$this->defaultRenderer = new DefaultBlockRenderer();
|
||||
$this->renderer = new ContentRenderer(
|
||||
$this->blockRegistry,
|
||||
$this->componentRenderer,
|
||||
$this->localizationService,
|
||||
$this->defaultRenderer
|
||||
);
|
||||
});
|
||||
|
||||
it('renders content with multiple blocks', function () {
|
||||
$content = CmsTestHelpers::createContent($this->clock);
|
||||
// ContentLocalizationService is real instance, no need to mock
|
||||
|
||||
$html = $this->renderer->render($content);
|
||||
|
||||
expect($html)->toBeString();
|
||||
expect($html)->not->toBeEmpty();
|
||||
});
|
||||
|
||||
it('uses default renderer for unregistered block types', function () {
|
||||
$blocks = ContentBlocks::fromArray([
|
||||
[
|
||||
'id' => 'custom-1',
|
||||
'type' => 'custom-block',
|
||||
'data' => ['key' => 'value'],
|
||||
],
|
||||
]);
|
||||
|
||||
$content = CmsTestHelpers::createContent($this->clock, blocks: $blocks);
|
||||
// ContentLocalizationService is real instance, no need to mock
|
||||
|
||||
$html = $this->renderer->render($content);
|
||||
|
||||
expect($html)->toBeString();
|
||||
expect($html)->not->toBeEmpty();
|
||||
});
|
||||
|
||||
it('uses registered renderer for block type', function () {
|
||||
$this->blockRegistry->registerForType('hero', new HeroBlockRenderer());
|
||||
$content = CmsTestHelpers::createContent($this->clock);
|
||||
// ContentLocalizationService is real instance, no need to mock
|
||||
|
||||
$html = $this->renderer->render($content);
|
||||
|
||||
expect($html)->toBeString();
|
||||
expect($html)->not->toBeEmpty();
|
||||
});
|
||||
|
||||
it('renders blocks to component data array', function () {
|
||||
$this->blockRegistry->registerForType('hero', new HeroBlockRenderer());
|
||||
$this->blockRegistry->registerForType('text', new TextBlockRenderer());
|
||||
$content = CmsTestHelpers::createContent($this->clock);
|
||||
// ContentLocalizationService is real instance, no need to mock
|
||||
|
||||
$componentData = $this->renderer->renderBlocksToComponentData($content);
|
||||
|
||||
expect($componentData)->toBeArray();
|
||||
expect($componentData)->toHaveCount(2);
|
||||
expect($componentData[0]['component'])->toBe('cms/hero');
|
||||
expect($componentData[1]['component'])->toBe('cms/text');
|
||||
});
|
||||
|
||||
it('uses provided locale for rendering', function () {
|
||||
$content = CmsTestHelpers::createContent($this->clock);
|
||||
$locale = Locale::german();
|
||||
// ContentLocalizationService is real instance, no need to mock
|
||||
|
||||
$html = $this->renderer->render($content, $locale);
|
||||
|
||||
expect($html)->toBeString();
|
||||
expect($html)->not->toBeEmpty();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
66
tests/Unit/Domain/Cms/Rendering/DefaultBlockRendererTest.php
Normal file
66
tests/Unit/Domain/Cms/Rendering/DefaultBlockRendererTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Rendering\DefaultBlockRenderer;
|
||||
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('DefaultBlockRenderer', function () {
|
||||
it('renders any block type as generic block', function () {
|
||||
$renderer = new DefaultBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::fromString('custom-block'),
|
||||
blockId: BlockId::fromString('custom-1'),
|
||||
data: BlockData::fromArray(['key' => 'value'])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result)->toHaveKeys(['component', 'data']);
|
||||
expect($result['component'])->toBe('cms/block');
|
||||
expect($result['data']['blockType'])->toBe('custom-block');
|
||||
expect($result['data']['blockId'])->toBe('custom-1');
|
||||
expect($result['data']['data'])->toBe(['key' => 'value']);
|
||||
});
|
||||
|
||||
it('renders block with settings', function () {
|
||||
$renderer = new DefaultBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::fromString('custom-block'),
|
||||
blockId: BlockId::fromString('custom-1'),
|
||||
data: BlockData::fromArray(['key' => 'value']),
|
||||
settings: BlockSettings::fromArray(['setting' => 'value'])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['settings'])->toBe(['setting' => 'value']);
|
||||
});
|
||||
|
||||
it('renders block without settings', function () {
|
||||
$renderer = new DefaultBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::fromString('custom-block'),
|
||||
blockId: BlockId::fromString('custom-1'),
|
||||
data: BlockData::fromArray(['key' => 'value'])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['settings'])->toBe([]);
|
||||
});
|
||||
|
||||
it('supports all block types', function () {
|
||||
$renderer = new DefaultBlockRenderer();
|
||||
|
||||
expect($renderer->supports('hero'))->toBeTrue();
|
||||
expect($renderer->supports('text'))->toBeTrue();
|
||||
expect($renderer->supports('custom-block'))->toBeTrue();
|
||||
expect($renderer->supports('any-type'))->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
103
tests/Unit/Domain/Cms/Rendering/HeroBlockRendererTest.php
Normal file
103
tests/Unit/Domain/Cms/Rendering/HeroBlockRendererTest.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Rendering\HeroBlockRenderer;
|
||||
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('HeroBlockRenderer', function () {
|
||||
it('renders hero block with title', function () {
|
||||
$renderer = new HeroBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray(['title' => 'Hero Title'])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result)->toHaveKeys(['component', 'data']);
|
||||
expect($result['component'])->toBe('cms/hero');
|
||||
expect($result['data']['title'])->toBe('Hero Title');
|
||||
});
|
||||
|
||||
it('renders hero block with all fields', function () {
|
||||
$renderer = new HeroBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray([
|
||||
'title' => 'Hero Title',
|
||||
'subtitle' => 'Hero Subtitle',
|
||||
'backgroundImage' => 'img-123',
|
||||
'ctaText' => 'Click Me',
|
||||
'ctaLink' => '/signup',
|
||||
]),
|
||||
settings: BlockSettings::fromArray([
|
||||
'fullWidth' => true,
|
||||
'padding' => 'large',
|
||||
])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['title'])->toBe('Hero Title');
|
||||
expect($result['data']['subtitle'])->toBe('Hero Subtitle');
|
||||
expect($result['data']['backgroundImage'])->toBe('img-123');
|
||||
expect($result['data']['ctaText'])->toBe('Click Me');
|
||||
expect($result['data']['ctaLink'])->toBe('/signup');
|
||||
expect($result['data']['fullWidth'])->toBeTrue();
|
||||
expect($result['data']['padding'])->toBe('large');
|
||||
});
|
||||
|
||||
it('handles snake_case field names', function () {
|
||||
$renderer = new HeroBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray([
|
||||
'title' => 'Hero Title',
|
||||
'background_image' => 'img-123',
|
||||
'cta_text' => 'Click Me',
|
||||
'cta_link' => '/signup',
|
||||
]),
|
||||
settings: BlockSettings::fromArray(['full_width' => true])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['backgroundImage'])->toBe('img-123');
|
||||
expect($result['data']['ctaText'])->toBe('Click Me');
|
||||
expect($result['data']['ctaLink'])->toBe('/signup');
|
||||
expect($result['data']['fullWidth'])->toBeTrue();
|
||||
});
|
||||
|
||||
it('provides default values for missing fields', function () {
|
||||
$renderer = new HeroBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray(['title' => 'Hero Title'])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['title'])->toBe('Hero Title');
|
||||
expect($result['data']['subtitle'])->toBeNull();
|
||||
expect($result['data']['fullWidth'])->toBeFalse();
|
||||
expect($result['data']['padding'])->toBe('medium');
|
||||
});
|
||||
|
||||
it('supports hero block type', function () {
|
||||
$renderer = new HeroBlockRenderer();
|
||||
|
||||
expect($renderer->supports('hero'))->toBeTrue();
|
||||
expect($renderer->supports('text'))->toBeFalse();
|
||||
expect($renderer->supports('image'))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
128
tests/Unit/Domain/Cms/Rendering/ImageBlockRendererTest.php
Normal file
128
tests/Unit/Domain/Cms/Rendering/ImageBlockRendererTest.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Rendering\ImageBlockRenderer;
|
||||
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('ImageBlockRenderer', function () {
|
||||
it('renders image block with imageId', function () {
|
||||
$renderer = new ImageBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::image(),
|
||||
blockId: BlockId::fromString('image-1'),
|
||||
data: BlockData::fromArray(['imageId' => 'img-123'])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result)->toHaveKeys(['component', 'data']);
|
||||
expect($result['component'])->toBe('cms/image');
|
||||
expect($result['data']['imageId'])->toBe('img-123');
|
||||
});
|
||||
|
||||
it('renders image block with imageUrl', function () {
|
||||
$renderer = new ImageBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::image(),
|
||||
blockId: BlockId::fromString('image-1'),
|
||||
data: BlockData::fromArray(['imageUrl' => 'https://example.com/image.jpg'])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['imageUrl'])->toBe('https://example.com/image.jpg');
|
||||
});
|
||||
|
||||
it('handles snake_case field names', function () {
|
||||
$renderer = new ImageBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::image(),
|
||||
blockId: BlockId::fromString('image-1'),
|
||||
data: BlockData::fromArray(['image_id' => 'img-123', 'image_url' => 'https://example.com/image.jpg'])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['imageId'])->toBe('img-123');
|
||||
expect($result['data']['imageUrl'])->toBe('https://example.com/image.jpg');
|
||||
});
|
||||
|
||||
it('renders image block with caption and alt', function () {
|
||||
$renderer = new ImageBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::image(),
|
||||
blockId: BlockId::fromString('image-1'),
|
||||
data: BlockData::fromArray([
|
||||
'imageId' => 'img-123',
|
||||
'caption' => 'Image caption',
|
||||
'alt' => 'Alt text',
|
||||
])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['caption'])->toBe('Image caption');
|
||||
expect($result['data']['alt'])->toBe('Alt text');
|
||||
});
|
||||
|
||||
it('uses caption as alt when alt is missing', function () {
|
||||
$renderer = new ImageBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::image(),
|
||||
blockId: BlockId::fromString('image-1'),
|
||||
data: BlockData::fromArray([
|
||||
'imageId' => 'img-123',
|
||||
'caption' => 'Image caption',
|
||||
])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['alt'])->toBe('Image caption');
|
||||
});
|
||||
|
||||
it('renders image block with width and height settings', function () {
|
||||
$renderer = new ImageBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::image(),
|
||||
blockId: BlockId::fromString('image-1'),
|
||||
data: BlockData::fromArray(['imageId' => 'img-123']),
|
||||
settings: BlockSettings::fromArray(['width' => 800, 'height' => 600])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['width'])->toBe(800);
|
||||
expect($result['data']['height'])->toBe(600);
|
||||
});
|
||||
|
||||
it('provides default values for missing fields', function () {
|
||||
$renderer = new ImageBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::image(),
|
||||
blockId: BlockId::fromString('image-1'),
|
||||
data: BlockData::fromArray(['imageId' => 'img-123'])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['imageId'])->toBe('img-123');
|
||||
expect($result['data']['imageUrl'])->toBeNull();
|
||||
expect($result['data']['caption'])->toBeNull();
|
||||
expect($result['data']['alt'])->toBe('');
|
||||
});
|
||||
|
||||
it('supports image block type', function () {
|
||||
$renderer = new ImageBlockRenderer();
|
||||
|
||||
expect($renderer->supports('image'))->toBeTrue();
|
||||
expect($renderer->supports('hero'))->toBeFalse();
|
||||
expect($renderer->supports('text'))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
96
tests/Unit/Domain/Cms/Rendering/TextBlockRendererTest.php
Normal file
96
tests/Unit/Domain/Cms/Rendering/TextBlockRendererTest.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Rendering\TextBlockRenderer;
|
||||
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('TextBlockRenderer', function () {
|
||||
it('renders text block with content', function () {
|
||||
$renderer = new TextBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::text(),
|
||||
blockId: BlockId::fromString('text-1'),
|
||||
data: BlockData::fromArray(['content' => 'Text content'])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result)->toHaveKeys(['component', 'data']);
|
||||
expect($result['component'])->toBe('cms/text');
|
||||
expect($result['data']['content'])->toBe('Text content');
|
||||
});
|
||||
|
||||
it('renders text block with alignment', function () {
|
||||
$renderer = new TextBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::text(),
|
||||
blockId: BlockId::fromString('text-1'),
|
||||
data: BlockData::fromArray([
|
||||
'content' => 'Text content',
|
||||
'alignment' => 'center',
|
||||
])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['content'])->toBe('Text content');
|
||||
expect($result['data']['alignment'])->toBe('center');
|
||||
});
|
||||
|
||||
it('renders text block with maxWidth setting', function () {
|
||||
$renderer = new TextBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::text(),
|
||||
blockId: BlockId::fromString('text-1'),
|
||||
data: BlockData::fromArray(['content' => 'Text content']),
|
||||
settings: BlockSettings::fromArray(['maxWidth' => 800])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['maxWidth'])->toBe(800);
|
||||
});
|
||||
|
||||
it('handles snake_case maxWidth setting', function () {
|
||||
$renderer = new TextBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::text(),
|
||||
blockId: BlockId::fromString('text-1'),
|
||||
data: BlockData::fromArray(['content' => 'Text content']),
|
||||
settings: BlockSettings::fromArray(['max_width' => 800])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['maxWidth'])->toBe(800);
|
||||
});
|
||||
|
||||
it('provides default values for missing fields', function () {
|
||||
$renderer = new TextBlockRenderer();
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::text(),
|
||||
blockId: BlockId::fromString('text-1'),
|
||||
data: BlockData::fromArray(['content' => 'Text content'])
|
||||
);
|
||||
|
||||
$result = $renderer->render($block);
|
||||
|
||||
expect($result['data']['content'])->toBe('Text content');
|
||||
expect($result['data']['alignment'])->toBe('left');
|
||||
expect($result['data']['maxWidth'])->toBeNull();
|
||||
});
|
||||
|
||||
it('supports text block type', function () {
|
||||
$renderer = new TextBlockRenderer();
|
||||
|
||||
expect($renderer->supports('text'))->toBeTrue();
|
||||
expect($renderer->supports('hero'))->toBeFalse();
|
||||
expect($renderer->supports('image'))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Entities\Content;
|
||||
use App\Domain\Cms\Enums\ContentStatus;
|
||||
use App\Domain\Cms\Repositories\DatabaseContentRepository;
|
||||
use App\Domain\Cms\ValueObjects\ContentId;
|
||||
use App\Domain\Cms\ValueObjects\ContentSlug;
|
||||
use App\Domain\Cms\ValueObjects\ContentTypeId;
|
||||
use App\Framework\Database\ConnectionInterface;
|
||||
use App\Framework\Database\ResultInterface;
|
||||
use Tests\Support\CmsTestHelpers;
|
||||
|
||||
describe('DatabaseContentRepository', function () {
|
||||
beforeEach(function () {
|
||||
$this->connection = Mockery::mock(ConnectionInterface::class);
|
||||
$this->repository = new DatabaseContentRepository($this->connection);
|
||||
});
|
||||
|
||||
it('saves content to database', function () {
|
||||
$content = CmsTestHelpers::createContent(new \App\Framework\DateTime\SystemClock());
|
||||
|
||||
$this->connection->shouldReceive('execute')
|
||||
->once()
|
||||
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
|
||||
->andReturn(1);
|
||||
|
||||
$this->repository->save($content);
|
||||
});
|
||||
|
||||
it('finds content by id', function () {
|
||||
$contentId = ContentId::generate(new \App\Framework\DateTime\SystemClock());
|
||||
$row = [
|
||||
'id' => $contentId->toString(),
|
||||
'content_type_id' => 'page',
|
||||
'slug' => 'test-page',
|
||||
'title' => 'Test Page',
|
||||
'blocks' => json_encode([['id' => 'hero-1', 'type' => 'hero', 'data' => ['title' => 'Hero']]]),
|
||||
'meta_data' => null,
|
||||
'status' => 'draft',
|
||||
'author_id' => null,
|
||||
'default_locale' => 'en',
|
||||
'published_at' => null,
|
||||
'created_at' => '2025-01-15 10:00:00',
|
||||
'updated_at' => '2025-01-15 10:00:00',
|
||||
];
|
||||
|
||||
$result = Mockery::mock(ResultInterface::class);
|
||||
$result->shouldReceive('fetch')
|
||||
->once()
|
||||
->andReturn($row);
|
||||
|
||||
$this->connection->shouldReceive('query')
|
||||
->once()
|
||||
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
|
||||
->andReturn($result);
|
||||
|
||||
$found = $this->repository->findById($contentId);
|
||||
|
||||
expect($found)->toBeInstanceOf(Content::class);
|
||||
expect($found->id->equals($contentId))->toBeTrue();
|
||||
});
|
||||
|
||||
it('returns null when content not found by id', function () {
|
||||
$contentId = ContentId::generate(new \App\Framework\DateTime\SystemClock());
|
||||
|
||||
$result = Mockery::mock(ResultInterface::class);
|
||||
$result->shouldReceive('fetch')
|
||||
->once()
|
||||
->andReturn(null);
|
||||
|
||||
$this->connection->shouldReceive('query')
|
||||
->once()
|
||||
->andReturn($result);
|
||||
|
||||
$found = $this->repository->findById($contentId);
|
||||
|
||||
expect($found)->toBeNull();
|
||||
});
|
||||
|
||||
it('finds content by slug', function () {
|
||||
$slug = ContentSlug::fromString('test-page');
|
||||
$row = [
|
||||
'id' => ContentId::generate(new \App\Framework\DateTime\SystemClock())->toString(),
|
||||
'content_type_id' => 'page',
|
||||
'slug' => 'test-page',
|
||||
'title' => 'Test Page',
|
||||
'blocks' => json_encode([['id' => 'hero-1', 'type' => 'hero', 'data' => ['title' => 'Hero']]]),
|
||||
'meta_data' => null,
|
||||
'status' => 'draft',
|
||||
'author_id' => null,
|
||||
'default_locale' => 'en',
|
||||
'published_at' => null,
|
||||
'created_at' => '2025-01-15 10:00:00',
|
||||
'updated_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->findBySlug($slug);
|
||||
|
||||
expect($found)->toBeInstanceOf(Content::class);
|
||||
expect($found->slug->equals($slug))->toBeTrue();
|
||||
});
|
||||
|
||||
it('checks if slug exists', function () {
|
||||
$slug = ContentSlug::fromString('test-page');
|
||||
|
||||
$result = Mockery::mock(ResultInterface::class);
|
||||
$result->shouldReceive('fetch')
|
||||
->once()
|
||||
->andReturn(['count' => 1]);
|
||||
|
||||
$this->connection->shouldReceive('query')
|
||||
->once()
|
||||
->andReturn($result);
|
||||
|
||||
expect($this->repository->existsSlug($slug))->toBeTrue();
|
||||
});
|
||||
|
||||
it('finds contents by type', function () {
|
||||
$typeId = ContentTypeId::fromString('page');
|
||||
$row = [
|
||||
'id' => ContentId::generate(new \App\Framework\DateTime\SystemClock())->toString(),
|
||||
'content_type_id' => 'page',
|
||||
'slug' => 'test-page',
|
||||
'title' => 'Test Page',
|
||||
'blocks' => json_encode([['id' => 'hero-1', 'type' => 'hero', 'data' => ['title' => 'Hero']]]),
|
||||
'meta_data' => null,
|
||||
'status' => 'draft',
|
||||
'author_id' => null,
|
||||
'default_locale' => 'en',
|
||||
'published_at' => null,
|
||||
'created_at' => '2025-01-15 10:00:00',
|
||||
'updated_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->findByType($typeId);
|
||||
|
||||
expect($found)->toBeArray();
|
||||
expect($found)->toHaveCount(1);
|
||||
});
|
||||
|
||||
it('deletes content', function () {
|
||||
$contentId = ContentId::generate(new \App\Framework\DateTime\SystemClock());
|
||||
|
||||
$this->connection->shouldReceive('execute')
|
||||
->once()
|
||||
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
|
||||
->andReturn(1);
|
||||
|
||||
$this->repository->delete($contentId);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Entities\ContentTranslation;
|
||||
use App\Domain\Cms\Repositories\DatabaseContentTranslationRepository;
|
||||
use App\Domain\Cms\ValueObjects\ContentId;
|
||||
use App\Domain\Cms\ValueObjects\Locale;
|
||||
use App\Framework\Database\ConnectionInterface;
|
||||
use App\Framework\Database\ResultInterface;
|
||||
use Tests\Support\CmsTestHelpers;
|
||||
|
||||
describe('DatabaseContentTranslationRepository', function () {
|
||||
beforeEach(function () {
|
||||
$this->clock = new \App\Framework\DateTime\SystemClock();
|
||||
$this->connection = Mockery::mock(ConnectionInterface::class);
|
||||
$this->repository = new DatabaseContentTranslationRepository($this->connection);
|
||||
});
|
||||
|
||||
it('saves translation to database', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$translation = CmsTestHelpers::createContentTranslation($contentId);
|
||||
|
||||
$this->connection->shouldReceive('execute')
|
||||
->once()
|
||||
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
|
||||
->andReturn(1);
|
||||
|
||||
$this->repository->save($translation);
|
||||
});
|
||||
|
||||
it('finds translation by content id and locale', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$locale = Locale::german();
|
||||
$row = [
|
||||
'content_id' => $contentId->toString(),
|
||||
'locale' => 'de',
|
||||
'title' => 'Deutscher Titel',
|
||||
'blocks' => json_encode([['id' => 'hero-1', 'type' => 'hero', 'data' => ['title' => 'Hero']]]),
|
||||
'created_at' => '2025-01-15 10:00:00',
|
||||
'updated_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->findByContentAndLocale($contentId, $locale);
|
||||
|
||||
expect($found)->toBeInstanceOf(ContentTranslation::class);
|
||||
expect($found->locale->equals($locale))->toBeTrue();
|
||||
});
|
||||
|
||||
it('finds all translations for content', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$row = [
|
||||
'content_id' => $contentId->toString(),
|
||||
'locale' => 'de',
|
||||
'title' => 'Deutscher Titel',
|
||||
'blocks' => json_encode([['id' => 'hero-1', 'type' => 'hero', 'data' => ['title' => 'Hero']]]),
|
||||
'created_at' => '2025-01-15 10:00:00',
|
||||
'updated_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->findByContent($contentId);
|
||||
|
||||
expect($found)->toBeArray();
|
||||
expect($found)->toHaveCount(1);
|
||||
});
|
||||
|
||||
it('deletes translation', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$locale = Locale::german();
|
||||
|
||||
$this->connection->shouldReceive('execute')
|
||||
->once()
|
||||
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
|
||||
->andReturn(1);
|
||||
|
||||
$this->repository->delete($contentId, $locale);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Entities\ContentType;
|
||||
use App\Domain\Cms\Repositories\DatabaseContentTypeRepository;
|
||||
use App\Domain\Cms\ValueObjects\ContentTypeId;
|
||||
use App\Framework\Database\ConnectionInterface;
|
||||
use App\Framework\Database\ResultInterface;
|
||||
use Tests\Support\CmsTestHelpers;
|
||||
|
||||
describe('DatabaseContentTypeRepository', function () {
|
||||
beforeEach(function () {
|
||||
$this->connection = Mockery::mock(ConnectionInterface::class);
|
||||
$this->repository = new DatabaseContentTypeRepository($this->connection);
|
||||
});
|
||||
|
||||
it('saves content type to database', function () {
|
||||
$contentType = CmsTestHelpers::createContentType();
|
||||
|
||||
$this->connection->shouldReceive('execute')
|
||||
->once()
|
||||
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
|
||||
->andReturn(1);
|
||||
|
||||
$this->repository->save($contentType);
|
||||
});
|
||||
|
||||
it('finds content type by id', function () {
|
||||
$id = ContentTypeId::fromString('page');
|
||||
$row = [
|
||||
'id' => 'page',
|
||||
'name' => 'Page',
|
||||
'slug' => 'page',
|
||||
'description' => 'A page content type',
|
||||
'is_system' => 0,
|
||||
'created_at' => '2025-01-15 10:00:00',
|
||||
'updated_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($id);
|
||||
|
||||
expect($found)->toBeInstanceOf(ContentType::class);
|
||||
expect($found->id->equals($id))->toBeTrue();
|
||||
});
|
||||
|
||||
it('finds content type by slug', function () {
|
||||
$slug = 'page';
|
||||
$row = [
|
||||
'id' => 'page',
|
||||
'name' => 'Page',
|
||||
'slug' => 'page',
|
||||
'description' => 'A page content type',
|
||||
'is_system' => 0,
|
||||
'created_at' => '2025-01-15 10:00:00',
|
||||
'updated_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->findBySlug($slug);
|
||||
|
||||
expect($found)->toBeInstanceOf(ContentType::class);
|
||||
expect($found->slug)->toBe('page');
|
||||
});
|
||||
|
||||
it('finds all content types', function () {
|
||||
$row = [
|
||||
'id' => 'page',
|
||||
'name' => 'Page',
|
||||
'slug' => 'page',
|
||||
'description' => 'A page content type',
|
||||
'is_system' => 0,
|
||||
'created_at' => '2025-01-15 10:00:00',
|
||||
'updated_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->findAll();
|
||||
|
||||
expect($found)->toBeArray();
|
||||
expect($found)->toHaveCount(1);
|
||||
});
|
||||
|
||||
it('checks if content type exists', function () {
|
||||
$id = ContentTypeId::fromString('page');
|
||||
|
||||
$result = Mockery::mock(ResultInterface::class);
|
||||
$result->shouldReceive('fetch')
|
||||
->once()
|
||||
->andReturn(['count' => 1]);
|
||||
|
||||
$this->connection->shouldReceive('query')
|
||||
->once()
|
||||
->andReturn($result);
|
||||
|
||||
expect($this->repository->exists($id))->toBeTrue();
|
||||
});
|
||||
|
||||
it('deletes content type', function () {
|
||||
$id = ContentTypeId::fromString('page');
|
||||
|
||||
$this->connection->shouldReceive('execute')
|
||||
->once()
|
||||
->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class))
|
||||
->andReturn(1);
|
||||
|
||||
$this->repository->delete($id);
|
||||
});
|
||||
});
|
||||
|
||||
169
tests/Unit/Domain/Cms/Services/BlockValidatorTest.php
Normal file
169
tests/Unit/Domain/Cms/Services/BlockValidatorTest.php
Normal file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Exceptions\InvalidBlockException;
|
||||
use App\Domain\Cms\Services\BlockTypeRegistry;
|
||||
use App\Domain\Cms\Services\BlockValidator;
|
||||
use App\Domain\Cms\ValueObjects\BlockData;
|
||||
use App\Domain\Cms\ValueObjects\BlockId;
|
||||
use App\Domain\Cms\ValueObjects\BlockType;
|
||||
use App\Domain\Cms\ValueObjects\ContentBlock;
|
||||
|
||||
describe('BlockValidator', function () {
|
||||
beforeEach(function () {
|
||||
$this->registry = new BlockTypeRegistry();
|
||||
$this->validator = new BlockValidator($this->registry);
|
||||
});
|
||||
|
||||
it('validates hero block with required title', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray(['title' => 'Hero Title'])
|
||||
);
|
||||
|
||||
$this->validator->validate($block);
|
||||
expect(true)->toBeTrue(); // Test passes if no exception is thrown
|
||||
});
|
||||
|
||||
it('throws exception for hero block without title', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::hero(),
|
||||
blockId: BlockId::fromString('hero-1'),
|
||||
data: BlockData::fromArray([])
|
||||
);
|
||||
|
||||
expect(fn () => $this->validator->validate($block))
|
||||
->toThrow(InvalidBlockException::class);
|
||||
});
|
||||
|
||||
it('validates text block with required content', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::text(),
|
||||
blockId: BlockId::fromString('text-1'),
|
||||
data: BlockData::fromArray(['content' => 'Text content'])
|
||||
);
|
||||
|
||||
$this->validator->validate($block);
|
||||
expect(true)->toBeTrue(); // Test passes if no exception is thrown
|
||||
});
|
||||
|
||||
it('throws exception for text block without content', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::text(),
|
||||
blockId: BlockId::fromString('text-1'),
|
||||
data: BlockData::fromArray([])
|
||||
);
|
||||
|
||||
expect(fn () => $this->validator->validate($block))
|
||||
->toThrow(InvalidBlockException::class);
|
||||
});
|
||||
|
||||
it('validates image block with imageId', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::image(),
|
||||
blockId: BlockId::fromString('image-1'),
|
||||
data: BlockData::fromArray(['imageId' => 'img-123'])
|
||||
);
|
||||
|
||||
$this->validator->validate($block);
|
||||
expect(true)->toBeTrue(); // Test passes if no exception is thrown
|
||||
});
|
||||
|
||||
it('validates image block with image_url', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::image(),
|
||||
blockId: BlockId::fromString('image-1'),
|
||||
data: BlockData::fromArray(['image_url' => 'https://example.com/image.jpg'])
|
||||
);
|
||||
|
||||
$this->validator->validate($block);
|
||||
expect(true)->toBeTrue(); // Test passes if no exception is thrown
|
||||
});
|
||||
|
||||
it('throws exception for image block without imageId or image_url', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::image(),
|
||||
blockId: BlockId::fromString('image-1'),
|
||||
data: BlockData::fromArray([])
|
||||
);
|
||||
|
||||
expect(fn () => $this->validator->validate($block))
|
||||
->toThrow(InvalidBlockException::class);
|
||||
});
|
||||
|
||||
it('validates gallery block with images array', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::gallery(),
|
||||
blockId: BlockId::fromString('gallery-1'),
|
||||
data: BlockData::fromArray(['images' => ['img1', 'img2']])
|
||||
);
|
||||
|
||||
$this->validator->validate($block);
|
||||
expect(true)->toBeTrue(); // Test passes if no exception is thrown
|
||||
});
|
||||
|
||||
it('throws exception for gallery block without images', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::gallery(),
|
||||
blockId: BlockId::fromString('gallery-1'),
|
||||
data: BlockData::fromArray([])
|
||||
);
|
||||
|
||||
expect(fn () => $this->validator->validate($block))
|
||||
->toThrow(InvalidBlockException::class);
|
||||
});
|
||||
|
||||
it('validates cta block with buttonText and buttonLink', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::cta(),
|
||||
blockId: BlockId::fromString('cta-1'),
|
||||
data: BlockData::fromArray([
|
||||
'buttonText' => 'Click Me',
|
||||
'buttonLink' => '/signup',
|
||||
])
|
||||
);
|
||||
|
||||
$this->validator->validate($block);
|
||||
expect(true)->toBeTrue(); // Test passes if no exception is thrown
|
||||
});
|
||||
|
||||
it('validates cta block with button_text and button_link', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::cta(),
|
||||
blockId: BlockId::fromString('cta-1'),
|
||||
data: BlockData::fromArray([
|
||||
'button_text' => 'Click Me',
|
||||
'button_link' => '/signup',
|
||||
])
|
||||
);
|
||||
|
||||
$this->validator->validate($block);
|
||||
expect(true)->toBeTrue(); // Test passes if no exception is thrown
|
||||
});
|
||||
|
||||
it('validates separator block without required data', function () {
|
||||
$block = ContentBlock::create(
|
||||
type: BlockType::separator(),
|
||||
blockId: BlockId::fromString('sep-1'),
|
||||
data: BlockData::empty()
|
||||
);
|
||||
|
||||
$this->validator->validate($block);
|
||||
expect(true)->toBeTrue(); // Test passes if no exception is thrown
|
||||
});
|
||||
|
||||
it('throws exception for unregistered block type', function () {
|
||||
$customType = BlockType::fromString('custom-block', false);
|
||||
$block = ContentBlock::create(
|
||||
type: $customType,
|
||||
blockId: BlockId::fromString('custom-1'),
|
||||
data: BlockData::empty()
|
||||
);
|
||||
|
||||
expect(fn () => $this->validator->validate($block))
|
||||
->toThrow(InvalidArgumentException::class, 'not registered');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Entities\Content;
|
||||
use App\Domain\Cms\Entities\ContentTranslation;
|
||||
use App\Domain\Cms\Enums\ContentStatus;
|
||||
use App\Domain\Cms\Repositories\ContentTranslationRepository;
|
||||
use App\Domain\Cms\Services\ContentLocalizationService;
|
||||
use App\Domain\Cms\ValueObjects\ContentBlocks;
|
||||
use App\Domain\Cms\ValueObjects\ContentId;
|
||||
use App\Domain\Cms\ValueObjects\ContentSlug;
|
||||
use App\Domain\Cms\ValueObjects\ContentTypeId;
|
||||
use App\Domain\Cms\ValueObjects\Locale;
|
||||
use App\Framework\Core\ValueObjects\Timestamp;
|
||||
use App\Framework\DateTime\SystemClock;
|
||||
use Tests\Support\CmsTestHelpers;
|
||||
|
||||
describe('ContentLocalizationService', function () {
|
||||
beforeEach(function () {
|
||||
$this->clock = new SystemClock();
|
||||
$this->repository = Mockery::mock(ContentTranslationRepository::class);
|
||||
$this->service = new ContentLocalizationService($this->repository, $this->clock);
|
||||
});
|
||||
|
||||
it('creates translation', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$locale = Locale::german();
|
||||
$blocks = ContentBlocks::fromArray([
|
||||
[
|
||||
'id' => 'text-1',
|
||||
'type' => 'text',
|
||||
'data' => ['content' => 'Deutscher Text'],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->repository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$translation = $this->service->createTranslation(
|
||||
contentId: $contentId,
|
||||
locale: $locale,
|
||||
title: 'Deutscher Titel',
|
||||
blocks: $blocks
|
||||
);
|
||||
|
||||
expect($translation)->toBeInstanceOf(ContentTranslation::class);
|
||||
expect($translation->locale->equals($locale))->toBeTrue();
|
||||
expect($translation->title)->toBe('Deutscher Titel');
|
||||
});
|
||||
|
||||
it('updates translation', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$locale = Locale::german();
|
||||
$translation = CmsTestHelpers::createContentTranslation($contentId, $locale);
|
||||
|
||||
$this->repository->shouldReceive('findByContentAndLocale')
|
||||
->once()
|
||||
->with($contentId, $locale)
|
||||
->andReturn($translation);
|
||||
|
||||
$this->repository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$updated = $this->service->updateTranslation(
|
||||
contentId: $contentId,
|
||||
locale: $locale,
|
||||
title: 'Aktualisierter Titel'
|
||||
);
|
||||
|
||||
expect($updated->title)->toBe('Aktualisierter Titel');
|
||||
});
|
||||
|
||||
it('throws exception when updating non-existent translation', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$locale = Locale::german();
|
||||
|
||||
$this->repository->shouldReceive('findByContentAndLocale')
|
||||
->once()
|
||||
->with($contentId, $locale)
|
||||
->andReturn(null);
|
||||
|
||||
expect(fn () => $this->service->updateTranslation(
|
||||
contentId: $contentId,
|
||||
locale: $locale,
|
||||
title: 'New Title'
|
||||
))->toThrow(DomainException::class);
|
||||
});
|
||||
|
||||
it('gets translation', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$locale = Locale::german();
|
||||
$translation = CmsTestHelpers::createContentTranslation($contentId, $locale);
|
||||
|
||||
$this->repository->shouldReceive('findByContentAndLocale')
|
||||
->once()
|
||||
->with($contentId, $locale)
|
||||
->andReturn($translation);
|
||||
|
||||
$found = $this->service->getTranslation($contentId, $locale);
|
||||
|
||||
expect($found)->toBe($translation);
|
||||
});
|
||||
|
||||
it('returns content for default locale', function () {
|
||||
$content = CmsTestHelpers::createContent($this->clock);
|
||||
$locale = $content->defaultLocale;
|
||||
|
||||
$localized = $this->service->getContentForLocale($content, $locale);
|
||||
|
||||
expect($localized)->toBe($content);
|
||||
});
|
||||
|
||||
it('returns translated content for non-default locale', function () {
|
||||
$content = CmsTestHelpers::createContent($this->clock);
|
||||
$locale = Locale::german();
|
||||
$translation = CmsTestHelpers::createContentTranslation($content->id, $locale);
|
||||
|
||||
$this->repository->shouldReceive('findByContentAndLocale')
|
||||
->once()
|
||||
->with($content->id, $locale)
|
||||
->andReturn($translation);
|
||||
|
||||
$localized = $this->service->getContentForLocale($content, $locale);
|
||||
|
||||
expect($localized->title)->toBe($translation->title);
|
||||
});
|
||||
|
||||
it('falls back to default locale when translation not found', function () {
|
||||
$content = CmsTestHelpers::createContent($this->clock);
|
||||
$locale = Locale::german();
|
||||
|
||||
$this->repository->shouldReceive('findByContentAndLocale')
|
||||
->once()
|
||||
->with($content->id, $locale)
|
||||
->andReturn(null);
|
||||
|
||||
$localized = $this->service->getContentForLocale($content, $locale);
|
||||
|
||||
expect($localized->title)->toBe($content->title);
|
||||
});
|
||||
|
||||
it('gets all translations for content', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$translations = [
|
||||
CmsTestHelpers::createContentTranslation($contentId, Locale::german()),
|
||||
];
|
||||
|
||||
$this->repository->shouldReceive('findByContent')
|
||||
->once()
|
||||
->with($contentId)
|
||||
->andReturn($translations);
|
||||
|
||||
$found = $this->service->getAllTranslations($contentId);
|
||||
|
||||
expect($found)->toBe($translations);
|
||||
});
|
||||
|
||||
it('deletes translation', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$locale = Locale::german();
|
||||
|
||||
$this->repository->shouldReceive('delete')
|
||||
->once()
|
||||
->with($contentId, $locale)
|
||||
->andReturnNull();
|
||||
|
||||
$this->service->deleteTranslation($contentId, $locale);
|
||||
});
|
||||
});
|
||||
|
||||
319
tests/Unit/Domain/Cms/Services/ContentServiceTest.php
Normal file
319
tests/Unit/Domain/Cms/Services/ContentServiceTest.php
Normal file
@@ -0,0 +1,319 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Entities\Content;
|
||||
use App\Domain\Cms\Enums\ContentStatus;
|
||||
use App\Domain\Cms\Exceptions\ContentNotFoundException;
|
||||
use App\Domain\Cms\Exceptions\DuplicateSlugException;
|
||||
use App\Domain\Cms\Exceptions\InvalidContentStatusException;
|
||||
use App\Domain\Cms\Repositories\ContentRepository;
|
||||
use App\Domain\Cms\Services\BlockTypeRegistry;
|
||||
use App\Domain\Cms\Services\BlockValidator;
|
||||
use App\Domain\Cms\Services\ContentService;
|
||||
use App\Domain\Cms\Services\SlugGenerator;
|
||||
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;
|
||||
use App\Domain\Cms\ValueObjects\ContentId;
|
||||
use App\Domain\Cms\ValueObjects\ContentSlug;
|
||||
use App\Domain\Cms\ValueObjects\ContentTypeId;
|
||||
use App\Domain\Cms\ValueObjects\Locale;
|
||||
use App\Framework\Core\ValueObjects\Timestamp;
|
||||
use App\Framework\DateTime\SystemClock;
|
||||
use Tests\Support\CmsTestHelpers;
|
||||
|
||||
describe('ContentService', function () {
|
||||
beforeEach(function () {
|
||||
$this->clock = new SystemClock();
|
||||
$this->repository = Mockery::mock(ContentRepository::class);
|
||||
$this->blockTypeRegistry = new BlockTypeRegistry();
|
||||
$this->blockValidator = new BlockValidator($this->blockTypeRegistry);
|
||||
// Use real SlugGenerator instance with mocked repository
|
||||
$this->slugGenerator = new SlugGenerator($this->repository);
|
||||
$this->service = new ContentService(
|
||||
$this->repository,
|
||||
$this->blockValidator,
|
||||
$this->slugGenerator,
|
||||
$this->clock
|
||||
);
|
||||
});
|
||||
|
||||
it('creates content with provided slug', function () {
|
||||
$contentTypeId = ContentTypeId::fromString('page');
|
||||
$slug = ContentSlug::fromString('my-page');
|
||||
$blocks = ContentBlocks::fromArray([
|
||||
[
|
||||
'id' => 'hero-1',
|
||||
'type' => 'hero',
|
||||
'data' => ['title' => 'Hero Title'],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->repository->shouldReceive('existsSlug')
|
||||
->once()
|
||||
->with($slug)
|
||||
->andReturn(false);
|
||||
|
||||
$this->repository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$content = $this->service->create(
|
||||
contentTypeId: $contentTypeId,
|
||||
title: 'My Page',
|
||||
blocks: $blocks,
|
||||
defaultLocale: Locale::english(),
|
||||
slug: $slug
|
||||
);
|
||||
|
||||
expect($content)->toBeInstanceOf(Content::class);
|
||||
expect($content->slug->equals($slug))->toBeTrue();
|
||||
expect($content->title)->toBe('My Page');
|
||||
});
|
||||
|
||||
it('generates slug when not provided', function () {
|
||||
$contentTypeId = ContentTypeId::fromString('page');
|
||||
$blocks = ContentBlocks::fromArray([
|
||||
[
|
||||
'id' => 'hero-1',
|
||||
'type' => 'hero',
|
||||
'data' => ['title' => 'Hero Title'],
|
||||
],
|
||||
]);
|
||||
|
||||
// SlugGenerator will generate 'my-page' from 'My Page'
|
||||
// Use Mockery::on() to match ContentSlug objects by their string value
|
||||
$this->repository->shouldReceive('existsSlug')
|
||||
->atLeast()->once()
|
||||
->with(Mockery::on(function ($slug) {
|
||||
return $slug instanceof ContentSlug && $slug->toString() === 'my-page';
|
||||
}))
|
||||
->andReturn(false);
|
||||
|
||||
$this->repository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$content = $this->service->create(
|
||||
contentTypeId: $contentTypeId,
|
||||
title: 'My Page',
|
||||
blocks: $blocks,
|
||||
defaultLocale: Locale::english()
|
||||
);
|
||||
|
||||
expect($content->slug->toString())->toBe('my-page');
|
||||
});
|
||||
|
||||
it('throws exception when slug already exists', function () {
|
||||
$contentTypeId = ContentTypeId::fromString('page');
|
||||
$slug = ContentSlug::fromString('existing-slug');
|
||||
$blocks = ContentBlocks::fromArray([
|
||||
[
|
||||
'id' => 'hero-1',
|
||||
'type' => 'hero',
|
||||
'data' => ['title' => 'Hero Title'],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->repository->shouldReceive('existsSlug')
|
||||
->once()
|
||||
->with($slug)
|
||||
->andReturn(true);
|
||||
|
||||
expect(fn () => $this->service->create(
|
||||
contentTypeId: $contentTypeId,
|
||||
title: 'My Page',
|
||||
blocks: $blocks,
|
||||
defaultLocale: Locale::english(),
|
||||
slug: $slug
|
||||
))->toThrow(DuplicateSlugException::class);
|
||||
});
|
||||
|
||||
it('finds content by id', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$content = CmsTestHelpers::createContent($this->clock, id: $contentId);
|
||||
|
||||
$this->repository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($contentId)
|
||||
->andReturn($content);
|
||||
|
||||
$found = $this->service->findById($contentId);
|
||||
|
||||
expect($found)->toBe($content);
|
||||
});
|
||||
|
||||
it('throws exception when content not found by id', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
|
||||
$this->repository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($contentId)
|
||||
->andReturn(null);
|
||||
|
||||
expect(fn () => $this->service->findById($contentId))
|
||||
->toThrow(ContentNotFoundException::class);
|
||||
});
|
||||
|
||||
it('finds content by slug', function () {
|
||||
$slug = ContentSlug::fromString('my-page');
|
||||
$content = CmsTestHelpers::createContent($this->clock);
|
||||
|
||||
$this->repository->shouldReceive('findBySlug')
|
||||
->once()
|
||||
->with($slug)
|
||||
->andReturn($content);
|
||||
|
||||
$found = $this->service->findBySlug($slug);
|
||||
|
||||
expect($found)->toBe($content);
|
||||
});
|
||||
|
||||
it('updates content title', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$content = CmsTestHelpers::createContent($this->clock, id: $contentId);
|
||||
|
||||
$this->repository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($contentId)
|
||||
->andReturn($content);
|
||||
|
||||
$this->repository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$updated = $this->service->updateTitle($contentId, 'New Title');
|
||||
|
||||
expect($updated->title)->toBe('New Title');
|
||||
});
|
||||
|
||||
it('updates content slug', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$content = CmsTestHelpers::createContent($this->clock, id: $contentId);
|
||||
$newSlug = ContentSlug::fromString('new-slug');
|
||||
|
||||
// updateSlug calls findById once (line 107) and findBySlug once (line 106)
|
||||
$this->repository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($contentId)
|
||||
->andReturn($content);
|
||||
|
||||
$this->repository->shouldReceive('findBySlug')
|
||||
->once()
|
||||
->with($newSlug)
|
||||
->andReturn(null);
|
||||
|
||||
$this->repository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$updated = $this->service->updateSlug($contentId, $newSlug);
|
||||
|
||||
expect($updated->slug->equals($newSlug))->toBeTrue();
|
||||
});
|
||||
|
||||
it('publishes content', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$content = CmsTestHelpers::createContent($this->clock, id: $contentId);
|
||||
|
||||
$this->repository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($contentId)
|
||||
->andReturn($content);
|
||||
|
||||
$this->repository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$published = $this->service->publish($contentId);
|
||||
|
||||
expect($published->status)->toBe(ContentStatus::PUBLISHED);
|
||||
});
|
||||
|
||||
it('unpublishes content', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$content = CmsTestHelpers::createContent($this->clock, id: $contentId);
|
||||
$publishedContent = $content->withStatus(ContentStatus::PUBLISHED);
|
||||
|
||||
$this->repository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($contentId)
|
||||
->andReturn($publishedContent);
|
||||
|
||||
$this->repository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$unpublished = $this->service->unpublish($contentId);
|
||||
|
||||
expect($unpublished->status)->toBe(ContentStatus::DRAFT);
|
||||
});
|
||||
|
||||
it('deletes content', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$content = CmsTestHelpers::createContent($this->clock, id: $contentId);
|
||||
|
||||
$this->repository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($contentId)
|
||||
->andReturn($content);
|
||||
|
||||
$this->repository->shouldReceive('delete')
|
||||
->once()
|
||||
->with($contentId)
|
||||
->andReturnNull();
|
||||
|
||||
$this->service->delete($contentId);
|
||||
});
|
||||
|
||||
it('updates content blocks', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$content = CmsTestHelpers::createContent($this->clock, id: $contentId);
|
||||
$newBlocks = ContentBlocks::fromArray([
|
||||
[
|
||||
'id' => 'text-1',
|
||||
'type' => 'text',
|
||||
'data' => ['content' => 'New content'],
|
||||
],
|
||||
]);
|
||||
|
||||
// BlockValidator is real instance, no need to mock
|
||||
|
||||
$this->repository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($contentId)
|
||||
->andReturn($content);
|
||||
|
||||
$this->repository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$updated = $this->service->updateBlocks($contentId, $newBlocks);
|
||||
|
||||
expect($updated->blocks->count())->toBe(1);
|
||||
});
|
||||
|
||||
it('updates content metadata', function () {
|
||||
$contentId = ContentId::generate($this->clock);
|
||||
$content = CmsTestHelpers::createContent($this->clock, id: $contentId);
|
||||
$metaData = BlockData::fromArray(['seo_title' => 'SEO Title']);
|
||||
|
||||
$this->repository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($contentId)
|
||||
->andReturn($content);
|
||||
|
||||
$this->repository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$updated = $this->service->updateMetaData($contentId, $metaData);
|
||||
|
||||
expect($updated->metaData)->not->toBeNull();
|
||||
expect($updated->metaData->get('seo_title'))->toBe('SEO Title');
|
||||
});
|
||||
});
|
||||
|
||||
223
tests/Unit/Domain/Cms/Services/ContentTypeServiceTest.php
Normal file
223
tests/Unit/Domain/Cms/Services/ContentTypeServiceTest.php
Normal file
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Entities\ContentType;
|
||||
use App\Domain\Cms\Exceptions\CannotDeleteSystemContentTypeException;
|
||||
use App\Domain\Cms\Exceptions\ContentTypeNotFoundException;
|
||||
use App\Domain\Cms\Exceptions\DuplicateContentTypeSlugException;
|
||||
use App\Domain\Cms\Repositories\ContentRepository;
|
||||
use App\Domain\Cms\Repositories\ContentTypeRepository;
|
||||
use App\Domain\Cms\Services\ContentTypeService;
|
||||
use App\Domain\Cms\ValueObjects\ContentTypeId;
|
||||
use App\Framework\Core\ValueObjects\Timestamp;
|
||||
use App\Framework\DateTime\SystemClock;
|
||||
use Tests\Support\CmsTestHelpers;
|
||||
|
||||
describe('ContentTypeService', function () {
|
||||
beforeEach(function () {
|
||||
$this->clock = new SystemClock();
|
||||
$this->contentTypeRepository = Mockery::mock(ContentTypeRepository::class);
|
||||
$this->contentRepository = Mockery::mock(ContentRepository::class);
|
||||
$this->service = new ContentTypeService($this->contentTypeRepository, $this->contentRepository, $this->clock);
|
||||
});
|
||||
|
||||
it('creates content type', function () {
|
||||
$this->contentTypeRepository->shouldReceive('findBySlug')
|
||||
->once()
|
||||
->with('page')
|
||||
->andReturn(null);
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$contentType = $this->service->create(
|
||||
name: 'Page',
|
||||
slug: 'page',
|
||||
description: 'A page content type'
|
||||
);
|
||||
|
||||
expect($contentType)->toBeInstanceOf(ContentType::class);
|
||||
expect($contentType->name)->toBe('Page');
|
||||
expect($contentType->slug)->toBe('page');
|
||||
});
|
||||
|
||||
it('throws exception when slug already exists', function () {
|
||||
$existing = CmsTestHelpers::createContentType();
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('findBySlug')
|
||||
->once()
|
||||
->with('page')
|
||||
->andReturn($existing);
|
||||
|
||||
expect(fn () => $this->service->create(
|
||||
name: 'Page',
|
||||
slug: 'page'
|
||||
))->toThrow(DuplicateContentTypeSlugException::class);
|
||||
});
|
||||
|
||||
it('finds content type by id', function () {
|
||||
$id = ContentTypeId::fromString('page');
|
||||
$contentType = CmsTestHelpers::createContentType();
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($id)
|
||||
->andReturn($contentType);
|
||||
|
||||
$found = $this->service->findById($id);
|
||||
|
||||
expect($found)->toBe($contentType);
|
||||
});
|
||||
|
||||
it('throws exception when content type not found by id', function () {
|
||||
$id = ContentTypeId::fromString('missing');
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($id)
|
||||
->andReturn(null);
|
||||
|
||||
expect(fn () => $this->service->findById($id))
|
||||
->toThrow(ContentTypeNotFoundException::class);
|
||||
});
|
||||
|
||||
it('finds content type by slug', function () {
|
||||
$contentType = CmsTestHelpers::createContentType();
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('findBySlug')
|
||||
->once()
|
||||
->with('page')
|
||||
->andReturn($contentType);
|
||||
|
||||
$found = $this->service->findBySlug('page');
|
||||
|
||||
expect($found)->toBe($contentType);
|
||||
});
|
||||
|
||||
it('updates content type name', function () {
|
||||
$id = ContentTypeId::fromString('page');
|
||||
$contentType = CmsTestHelpers::createContentType();
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($id)
|
||||
->andReturn($contentType);
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$updated = $this->service->update($id, name: 'Updated Page');
|
||||
|
||||
expect($updated->name)->toBe('Updated Page');
|
||||
});
|
||||
|
||||
it('updates content type description', function () {
|
||||
$id = ContentTypeId::fromString('page');
|
||||
$contentType = CmsTestHelpers::createContentType();
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($id)
|
||||
->andReturn($contentType);
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturnNull();
|
||||
|
||||
$updated = $this->service->update($id, description: 'Updated description');
|
||||
|
||||
expect($updated->description)->toBe('Updated description');
|
||||
});
|
||||
|
||||
it('throws exception when trying to change slug', function () {
|
||||
$id = ContentTypeId::fromString('page');
|
||||
$contentType = CmsTestHelpers::createContentType();
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($id)
|
||||
->andReturn($contentType);
|
||||
|
||||
// update() checks findBySlug if slug is different
|
||||
$this->contentTypeRepository->shouldReceive('findBySlug')
|
||||
->once()
|
||||
->with('new-slug')
|
||||
->andReturn(null);
|
||||
|
||||
expect(fn () => $this->service->update($id, slug: 'new-slug'))
|
||||
->toThrow(\InvalidArgumentException::class, 'slug cannot be changed');
|
||||
});
|
||||
|
||||
it('deletes non-system content type', function () {
|
||||
$id = ContentTypeId::fromString('page');
|
||||
$contentType = CmsTestHelpers::createContentType(isSystem: false);
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($id)
|
||||
->andReturn($contentType);
|
||||
|
||||
$this->contentRepository->shouldReceive('findByType')
|
||||
->once()
|
||||
->with($id)
|
||||
->andReturn([]);
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('delete')
|
||||
->once()
|
||||
->with($id)
|
||||
->andReturnNull();
|
||||
|
||||
$this->service->delete($id);
|
||||
});
|
||||
|
||||
it('throws exception when trying to delete system content type', function () {
|
||||
$id = ContentTypeId::fromString('page');
|
||||
$contentType = CmsTestHelpers::createContentType(isSystem: true);
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($id)
|
||||
->andReturn($contentType);
|
||||
|
||||
expect(fn () => $this->service->delete($id))
|
||||
->toThrow(CannotDeleteSystemContentTypeException::class);
|
||||
});
|
||||
|
||||
it('throws exception when trying to delete content type in use', function () {
|
||||
$id = ContentTypeId::fromString('page');
|
||||
$contentType = CmsTestHelpers::createContentType(isSystem: false);
|
||||
$contents = [CmsTestHelpers::createContent($this->clock)];
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('findById')
|
||||
->once()
|
||||
->with($id)
|
||||
->andReturn($contentType);
|
||||
|
||||
$this->contentRepository->shouldReceive('findByType')
|
||||
->once()
|
||||
->with($id)
|
||||
->andReturn($contents);
|
||||
|
||||
expect(fn () => $this->service->delete($id))
|
||||
->toThrow(\App\Domain\Cms\Exceptions\CannotDeleteContentTypeInUseException::class);
|
||||
});
|
||||
|
||||
it('finds all content types', function () {
|
||||
$contentTypes = [
|
||||
CmsTestHelpers::createContentType(),
|
||||
];
|
||||
|
||||
$this->contentTypeRepository->shouldReceive('findAll')
|
||||
->once()
|
||||
->andReturn($contentTypes);
|
||||
|
||||
$found = $this->service->findAll();
|
||||
|
||||
expect($found)->toBe($contentTypes);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
89
tests/Unit/Domain/Cms/Services/SlugGeneratorTest.php
Normal file
89
tests/Unit/Domain/Cms/Services/SlugGeneratorTest.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\Cms\Repositories\ContentRepository;
|
||||
use App\Domain\Cms\Services\SlugGenerator;
|
||||
use App\Domain\Cms\ValueObjects\ContentSlug;
|
||||
|
||||
describe('SlugGenerator', function () {
|
||||
beforeEach(function () {
|
||||
$this->repository = Mockery::mock(ContentRepository::class);
|
||||
$this->slugGenerator = new SlugGenerator($this->repository);
|
||||
});
|
||||
|
||||
it('generates slug from title', function () {
|
||||
$slug = $this->slugGenerator->generateFromTitle('My Awesome Page');
|
||||
|
||||
expect($slug)->toBeInstanceOf(ContentSlug::class);
|
||||
expect($slug->toString())->toBe('my-awesome-page');
|
||||
});
|
||||
|
||||
it('handles special characters in title', function () {
|
||||
$slug = $this->slugGenerator->generateFromTitle('Hello & World!');
|
||||
|
||||
expect($slug->toString())->toBe('hello-world');
|
||||
});
|
||||
|
||||
it('handles empty title', function () {
|
||||
$slug = $this->slugGenerator->generateFromTitle('');
|
||||
|
||||
expect($slug->toString())->toStartWith('content-');
|
||||
});
|
||||
|
||||
it('truncates long titles', function () {
|
||||
$longTitle = str_repeat('a', 300);
|
||||
$slug = $this->slugGenerator->generateFromTitle($longTitle);
|
||||
|
||||
expect(strlen($slug->toString()))->toBeLessThanOrEqual(255);
|
||||
});
|
||||
|
||||
it('generates unique slug when base slug exists', function () {
|
||||
$this->repository->shouldReceive('existsSlug')
|
||||
->once()
|
||||
->with(Mockery::on(fn ($slug) => $slug->toString() === 'my-page'))
|
||||
->andReturn(true);
|
||||
|
||||
$this->repository->shouldReceive('existsSlug')
|
||||
->once()
|
||||
->with(Mockery::on(fn ($slug) => $slug->toString() === 'my-page-1'))
|
||||
->andReturn(false);
|
||||
|
||||
$slug = $this->slugGenerator->generateUniqueFromTitle('My Page');
|
||||
|
||||
expect($slug->toString())->toBe('my-page-1');
|
||||
});
|
||||
|
||||
it('generates unique slug with multiple collisions', function () {
|
||||
$this->repository->shouldReceive('existsSlug')
|
||||
->once()
|
||||
->with(Mockery::on(fn ($slug) => $slug->toString() === 'my-page'))
|
||||
->andReturn(true);
|
||||
|
||||
$this->repository->shouldReceive('existsSlug')
|
||||
->once()
|
||||
->with(Mockery::on(fn ($slug) => $slug->toString() === 'my-page-1'))
|
||||
->andReturn(true);
|
||||
|
||||
$this->repository->shouldReceive('existsSlug')
|
||||
->once()
|
||||
->with(Mockery::on(fn ($slug) => $slug->toString() === 'my-page-2'))
|
||||
->andReturn(false);
|
||||
|
||||
$slug = $this->slugGenerator->generateUniqueFromTitle('My Page');
|
||||
|
||||
expect($slug->toString())->toBe('my-page-2');
|
||||
});
|
||||
|
||||
it('returns base slug when it does not exist', function () {
|
||||
$this->repository->shouldReceive('existsSlug')
|
||||
->once()
|
||||
->with(Mockery::on(fn ($slug) => $slug->toString() === 'my-page'))
|
||||
->andReturn(false);
|
||||
|
||||
$slug = $this->slugGenerator->generateUniqueFromTitle('My Page');
|
||||
|
||||
expect($slug->toString())->toBe('my-page');
|
||||
});
|
||||
});
|
||||
|
||||
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