Files
michaelschiemer/tests/Unit/Domain/Asset/Services/DeduplicationServiceTest.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

78 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Domain\Asset\Entities\Asset;
use App\Domain\Asset\Repositories\AssetRepository;
use App\Domain\Asset\Services\DeduplicationService;
use App\Domain\Asset\ValueObjects\AssetId;
use App\Domain\Asset\ValueObjects\AssetMetadata;
use App\Framework\Core\ValueObjects\FileSize;
use App\Framework\Core\ValueObjects\Hash;
use App\Framework\Core\ValueObjects\HashAlgorithm;
use App\Framework\Core\ValueObjects\Timestamp;
use App\Framework\DateTime\SystemClock;
use App\Framework\Http\MimeType;
use App\Framework\Storage\ValueObjects\BucketName;
use App\Framework\Storage\ValueObjects\ObjectKey;
describe('DeduplicationService', function () {
beforeEach(function () {
$this->repository = Mockery::mock(AssetRepository::class);
$this->service = new DeduplicationService($this->repository);
});
it('returns null when no duplicate found', function () {
$hash = Hash::create('test-content', HashAlgorithm::SHA256);
$this->repository->shouldReceive('findBySha256')
->once()
->with($hash)
->andReturn(null);
$result = $this->service->checkDuplicate($hash);
expect($result)->toBeNull();
});
it('returns existing asset when duplicate found', function () {
$clock = new SystemClock();
$hash = Hash::create('test-content', HashAlgorithm::SHA256);
$asset = AssetTestHelpers::createAsset($clock);
$this->repository->shouldReceive('findBySha256')
->once()
->with($hash)
->andReturn($asset);
$result = $this->service->checkDuplicate($hash);
expect($result)->toBe($asset);
});
it('returns false when no duplicate exists', function () {
$hash = Hash::create('test-content', HashAlgorithm::SHA256);
$this->repository->shouldReceive('findBySha256')
->once()
->with($hash)
->andReturn(null);
expect($this->service->isDuplicate($hash))->toBeFalse();
});
it('returns true when duplicate exists', function () {
$clock = new SystemClock();
$hash = Hash::create('test-content', HashAlgorithm::SHA256);
$asset = AssetTestHelpers::createAsset($clock);
$this->repository->shouldReceive('findBySha256')
->once()
->with($hash)
->andReturn($asset);
expect($this->service->isDuplicate($hash))->toBeTrue();
});
});