Files
portal/app/tests/Unit/Advertising/CreativeValidatorTest.php
T

82 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
use App\Services\Advertising\CreativeValidator;
use Tests\TestCase;
uses(TestCase::class);
it('rejects too-long title and text, accepts valid', function () {
$v = new CreativeValidator;
expect($v->validateText(str_repeat('а', 57), 'ok text'))->toHaveKey('title'); // >56 → ошибка
expect($v->validateText('Норм заголовок', str_repeat('б', 82)))->toHaveKey('text'); // >81
expect($v->validateText('Норм заголовок', 'Короткий текст'))->toBe([]); // ок
});
it('rejects title with a single word longer than 22 characters', function () {
$v = new CreativeValidator;
$longWord = str_repeat('ё', 23);
expect($v->validateText($longWord, 'Короткий текст'))->toHaveKey('title');
$okWord = str_repeat('ё', 22);
expect($v->validateText($okWord, 'Короткий текст'))->toBe([]);
});
it('rejects text with a single word longer than 23 characters', function () {
$v = new CreativeValidator;
$longWord = str_repeat('ж', 24);
expect($v->validateText('Норм заголовок', $longWord))->toHaveKey('text');
$okWord = str_repeat('ж', 23);
expect($v->validateText('Норм заголовок', $okWord))->toBe([]);
});
it('accepts title2 within limit (30 normal + up to 15 narrow) and rejects too-long', function () {
$v = new CreativeValidator;
// 30 обычных символов + 15 узких (кавычки) — на грани, должно быть ок
$okTitle2 = str_repeat('а', 30).str_repeat('"', 15);
expect($v->validateText('Норм заголовок', 'Короткий текст', $okTitle2))->toBe([]);
// 31 обычный символ сверх лимита → ошибка
$tooLongTitle2 = str_repeat('а', 31);
expect($v->validateText('Норм заголовок', 'Короткий текст', $tooLongTitle2))->toHaveKey('title2');
// узких больше 15 → ошибка
$tooManyNarrow = str_repeat('а', 20).str_repeat('"', 16);
expect($v->validateText('Норм заголовок', 'Короткий текст', $tooManyNarrow))->toHaveKey('title2');
});
it('accepts text within limit (81 normal + up to 15 narrow) and rejects too-long', function () {
$v = new CreativeValidator;
// 4 слова (23+23+23+8=77 + 3 пробела между ними = 80) + 1 разделительный пробел (81 обычный)
// + отдельное «слово» из 15 узких символов (не считается в лимит обычных).
$okText = implode(' ', [str_repeat('б', 23), str_repeat('б', 23), str_repeat('б', 23), str_repeat('б', 8)])
.' '.str_repeat('!', 15);
expect($v->validateText('Норм заголовок', $okText))->toBe([]);
$tooLongText = str_repeat('б', 82).str_repeat('!', 15);
expect($v->validateText('Норм заголовок', $tooLongText))->toHaveKey('text');
});
it('validates image dimensions, weight and mime', function () {
$v = new CreativeValidator;
// корректная обычная картинка
expect($v->validateImage(600, 600, 'image/jpeg', 500_000))->toBe([]);
// слишком маленькая
expect($v->validateImage(200, 200, 'image/jpeg', 500_000))->toHaveKey('image');
// слишком тяжёлая (>10 МБ)
expect($v->validateImage(600, 600, 'image/jpeg', 11 * 1024 * 1024))->toHaveKey('image');
// неверный mime
expect($v->validateImage(600, 600, 'image/webp', 500_000))->toHaveKey('image');
});