Files
michaelschiemer/tests/Unit/Domain/Cms/Repositories/DatabaseContentRepositoryTest.php
Michael Schiemer 2d53270056 feat(cms,asset): add comprehensive test suite and finalize modules
- Add comprehensive test suite for CMS and Asset modules using Pest Framework
- Implement ContentTypeService::delete() protection against deletion of in-use content types
- Add CannotDeleteContentTypeInUseException for better error handling
- Fix DerivatPipelineRegistry::getAllPipelines() to handle object uniqueness correctly
- Fix VariantName::getScale() to correctly parse scales with file extensions
- Update CMS module documentation with new features, exceptions, and test coverage
- Add CmsTestHelpers and AssetTestHelpers for test data factories
- Fix BlockTypeRegistry to be immutable after construction
- Update ContentTypeService to check for associated content before deletion
- Improve BlockRendererRegistry initialization

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

254 tests passing, 38 remaining (mostly image processing pipeline tests)
2025-11-10 02:12:28 +01:00

173 lines
5.8 KiB
PHP

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