130 lines
3.9 KiB
PHP
130 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Shared\Models\ValueObjects;
|
|
|
|
use Shared\Models\ValueObjects\PicturePath;
|
|
use Shared\Models\ValueObjects\RawPicture;
|
|
|
|
covers(RawPicture::class);
|
|
|
|
describe('raw picture', function (): void {
|
|
it('creates a valid RawPicture', function (): void {
|
|
// GIVEN
|
|
$content = 'binary-content';
|
|
$originalName = 'logo.png';
|
|
|
|
// WHEN
|
|
$picture = new RawPicture($content, $originalName);
|
|
|
|
// THEN
|
|
expect($picture)->toBeInstanceOf(RawPicture::class)
|
|
->and($picture->content)->toBe($content)
|
|
->and($picture->originalName)->toBe($originalName);
|
|
});
|
|
|
|
it('rejects an empty content', function (): void {
|
|
// GIVEN
|
|
$content = '';
|
|
|
|
// WHEN
|
|
$action = fn (): RawPicture => new RawPicture($content, 'logo.png');
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|
|
|
|
it('rejects a content that exceeds the maximum allowed size', function (): void {
|
|
// GIVEN
|
|
$content = str_repeat('a', RawPicture::MAX_SIZE_IN_BYTES + 1);
|
|
|
|
// WHEN
|
|
$action = fn (): RawPicture => new RawPicture($content, 'logo.png');
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|
|
|
|
it('accepts a content of exactly the maximum allowed size', function (): void {
|
|
// GIVEN
|
|
$content = str_repeat('a', RawPicture::MAX_SIZE_IN_BYTES);
|
|
|
|
// WHEN
|
|
$picture = new RawPicture($content, 'logo.png');
|
|
|
|
// THEN
|
|
expect($picture)->toBeInstanceOf(RawPicture::class)
|
|
->and(strlen($picture->content))->toBe(RawPicture::MAX_SIZE_IN_BYTES);
|
|
});
|
|
|
|
it('measures the size in bytes, not in characters', function (): void {
|
|
// GIVEN
|
|
$content = str_repeat('é', RawPicture::MAX_SIZE_IN_BYTES - 1);
|
|
|
|
// WHEN
|
|
$action = fn (): RawPicture => new RawPicture($content, 'logo.png');
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|
|
|
|
it('rejects an empty original name', function (string $originalName): void {
|
|
// WHEN
|
|
$action = fn (): RawPicture => new RawPicture('binary-content', $originalName);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
|
|
// GIVEN
|
|
})->with([
|
|
'empty string' => '',
|
|
'spaces only' => ' ',
|
|
'whitespace only' => "\t\n ",
|
|
]);
|
|
|
|
it('rejects an original name without an allowed extension', function (string $originalName): void {
|
|
// WHEN
|
|
$action = fn (): RawPicture => new RawPicture('binary-content', $originalName);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
|
|
// GIVEN
|
|
})->with([
|
|
'no extension' => 'logo',
|
|
'forbidden extension' => 'logo.gif',
|
|
'executable extension' => 'logo.php',
|
|
'trailing dot' => 'logo.',
|
|
'double extension' => 'logo.png.exe',
|
|
]);
|
|
|
|
it('accepts every allowed extension', function (string $extension): void {
|
|
// GIVEN
|
|
$originalName = 'logo.' . $extension;
|
|
|
|
// WHEN
|
|
$picture = new RawPicture('binary-content', $originalName);
|
|
|
|
// THEN
|
|
expect($picture)->toBeInstanceOf(RawPicture::class)
|
|
->and($picture->originalName)->toBe($originalName);
|
|
})->with(RawPicture::ALLOWED_EXTENSIONS);
|
|
|
|
it('accepts an original name surrounded by whitespace and keeps it as provided', function (): void {
|
|
// GIVEN
|
|
$originalName = ' logo.png ';
|
|
|
|
// WHEN
|
|
$picture = new RawPicture('binary-content', $originalName);
|
|
|
|
// THEN
|
|
expect($picture->originalName)->toBe($originalName);
|
|
});
|
|
|
|
it('shares its allowed extensions with PicturePath', function (): void {
|
|
// GIVEN / WHEN / THEN
|
|
expect(RawPicture::ALLOWED_EXTENSIONS)
|
|
->toBe(PicturePath::ALLOWED_EXTENSIONS);
|
|
});
|
|
});
|