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

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

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

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

View File

@@ -0,0 +1,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');
});
});

View File

@@ -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);
});
});

View 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');
});
});

View 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);
});
});

View 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');
});
});