ldc-mesbr-app/tests/Unit/Shared/Models/ValueObjects/PicturePathTest.php
2026-09-08 16:33:54 +02:00

98 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Shared\Models\ValueObjects;
use DomainException;
use Shared\Models\ValueObjects\PicturePath;
use stdClass;
covers(PicturePath::class);
describe('picture path', function (): void {
it('creates a valid PicturePath', function (): void {
// GIVEN
$pathValue = 'coupons/logo.png';
// WHEN
$path = new PicturePath($pathValue);
// THEN
expect($path)->toBeInstanceOf(PicturePath::class)
->and($path->value())->toBe($pathValue);
});
it('rejects an empty path', function (): void {
// GIVEN
$pathValue = '';
// WHEN
$action = fn (): PicturePath => new PicturePath($pathValue);
// THEN
expect($action)->toThrow(DomainException::class, 'Picture Path cannot be empty.');
});
it('rejects a path that exceeds the maximum allowed length', function (): void {
// GIVEN
$pathValue = str_repeat('a', 2049) . '.jpg';
// WHEN
$action = fn (): PicturePath => new PicturePath($pathValue);
// THEN
expect($action)->toThrow(DomainException::class, 'Picture Path exceeds the maximum allowed length.');
});
it('accepts a path of exactly the maximum length', function (): void {
// GIVEN
$pathValue = str_repeat('a', 2044) . '.jpg';
// WHEN
$path = new PicturePath($pathValue);
// THEN
expect($path)->toBeInstanceOf(PicturePath::class)
->and($path->value())->toHaveLength(2048);
});
it('rejects a path without an image extension', function (): void {
// GIVEN
$pathValue = 'image';
// WHEN
$action = fn (): PicturePath => new PicturePath($pathValue);
// THEN
expect($action)->toThrow(DomainException::class, 'Picture Path must end with one of: ' . implode(', ', PicturePath::ALLOWED_EXTENSIONS));
});
it('considers two paths to be equal if they have the same value', function (): void {
// GIVEN
$first = new PicturePath('coupons/logo.png');
$second = new PicturePath('coupons/logo.png');
// WHEN
$result = $first->equals($second);
// THEN
expect($result)->toBeTrue();
});
it('considers a logo as not equal to', function (mixed $invalid): void {
// GIVEN
$logo = new PicturePath('coupons/logo-1.png');
// WHEN
$result = $logo->equals($invalid);
// THEN
expect($result)->toBeFalse();
// GIVEN
})->with([
'different picture path' => new PicturePath('coupons/logo-2.png'),
'non-picture-path value' => new stdClass(),
]);
});