respect-validation/tests/feature/Validators/SizeTest.php
Alexandre Gomes Gaigalas 16148e9593 Standardize and improve validation message templates
- Remove redundant "valid" prefix:
   Date, DateTime, DateTimeDiff, Domain, Email, Iban, Imei, Ip, Isbn, Json, LanguageCode, LeapDate, LeapYear, Luhn, MacAddress, NfeAccessKey, Nif, Nip, Pesel, Phone, Pis, PolishIdCard, PostalCode, Roman, Slug, Tld, Url, Uuid, Version.

 - Remove redundant "value" suffix
   ArrayVal, BoolVal, Countable, FloatVal, IntVal, IterableVal, NumericVal, ScalarVal, StringVal.

 - Standardize "consist only of" phrasing
   Alnum, Alpha, Cntrl, Consonant, Digit, Graph, Lowercase, Printable, Punct, Space, Spaced, Uppercase, Vowel, Xdigit.

 - Improve file accessibility messages
   Directory, Executable, File, Image, Readable, SymbolicLink, Writable.

 - Improve grammar and article usage
   CreditCard, Extension, Mimetype, Regex, Size.
2026-02-03 19:58:55 +00:00

81 lines
4.1 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
use org\bovigo\vfs\content\LargeFileContent;
use org\bovigo\vfs\vfsStream;
beforeEach(function (): void {
$this->root = vfsStream::setup();
$this->file2Kb = vfsStream::newFile('2kb.txt')
->withContent(LargeFileContent::withKilobytes(2))
->at($this->root);
$this->file2Mb = vfsStream::newFile('3mb.txt')
->withContent(LargeFileContent::withMegabytes(3))
->at($this->root);
});
test('Default', catchAll(
fn() => v::size('KB', v::lessThan(2))->assert($this->file2Kb->url()),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('The size in kilobytes of "vfs://root/2kb.txt" must be less than 2')
->and($fullMessage)->toBe('- The size in kilobytes of "vfs://root/2kb.txt" must be less than 2')
->and($messages)->toBe(['sizeLessThan' => 'The size in kilobytes of "vfs://root/2kb.txt" must be less than 2']),
));
test('Wrong type', catchAll(
fn() => v::size('KB', v::lessThan(2))->assert(new stdClass()),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`stdClass {}` must be a filename, an instance of SplFileInfo or a PSR-7 interface')
->and($fullMessage)->toBe('- `stdClass {}` must be a filename, an instance of SplFileInfo or a PSR-7 interface')
->and($messages)->toBe(['sizeLessThan' => '`stdClass {}` must be a filename, an instance of SplFileInfo or a PSR-7 interface']),
));
test('Inverted', catchAll(
fn() => v::size('MB', v::not(v::equals(3)))->assert($this->file2Mb->url()),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('The size in megabytes of "vfs://root/3mb.txt" must not be equal to 3')
->and($fullMessage)->toBe('- The size in megabytes of "vfs://root/3mb.txt" must not be equal to 3')
->and($messages)->toBe(['sizeNotEquals' => 'The size in megabytes of "vfs://root/3mb.txt" must not be equal to 3']),
));
test('Wrapped with name', catchAll(
fn() => v::size('KB', v::named('Wrapped', v::lessThan(2)))->assert($this->file2Kb->url()),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('The size in kilobytes of Wrapped must be less than 2')
->and($fullMessage)->toBe('- The size in kilobytes of Wrapped must be less than 2')
->and($messages)->toBe(['sizeLessThan' => 'The size in kilobytes of Wrapped must be less than 2']),
));
test('Wrapper with name', catchAll(
fn() => v::named('Wrapper', v::size('KB', v::lessThan(2)))->assert($this->file2Kb->url()),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('The size in kilobytes of Wrapper must be less than 2')
->and($fullMessage)->toBe('- The size in kilobytes of Wrapper must be less than 2')
->and($messages)->toBe(['sizeLessThan' => 'The size in kilobytes of Wrapper must be less than 2']),
));
test('Chained wrapped rule', catchAll(
fn() => v::size('KB', v::between(5, 7)->odd())->assert($this->file2Kb->url()),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('The size in kilobytes of "vfs://root/2kb.txt" must be between 5 and 7')
->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
- "vfs://root/2kb.txt" must pass all the rules
- The size in kilobytes of "vfs://root/2kb.txt" must be between 5 and 7
- The size in kilobytes of "vfs://root/2kb.txt" must be an odd number
FULL_MESSAGE)
->and($messages)->toBe([
'__root__' => '"vfs://root/2kb.txt" must pass all the rules',
'sizeBetween' => 'The size in kilobytes of "vfs://root/2kb.txt" must be between 5 and 7',
'sizeOdd' => 'The size in kilobytes of "vfs://root/2kb.txt" must be an odd number',
]),
));