respect-validation/tests/feature/Rules/KeyOptionalTest.php
Henrique Moody d3239e878d
Change how we're writing Pest tests
The problem with the current approach is that the "expect()" calls
happen inside "tests/Pest.php". That means that when something fails, we
can't easily know which exact expectation has failed.

This commit will change the helper functions, and will make the tests
more verbose, but event with that, the developer experience is better.
2025-12-18 14:02:33 +01:00

88 lines
4.2 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
test('Default', catchAll(
fn() => v::keyOptional('foo', v::intType())->assert(['foo' => 'string']),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` must be an integer')
->and($fullMessage)->toBe('- `.foo` must be an integer')
->and($messages)->toBe(['foo' => '`.foo` must be an integer'])
));
test('Inverted', catchAll(
fn() => v::not(v::keyOptional('foo', v::intType()))->assert(['foo' => 12]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` must not be an integer')
->and($fullMessage)->toBe('- `.foo` must not be an integer')
->and($messages)->toBe(['foo' => '`.foo` must not be an integer'])
));
test('Inverted with missing key', catchAll(
fn() => v::not(v::keyOptional('foo', v::intType()))->assert([]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` must be present')
->and($fullMessage)->toBe('- `.foo` must be present')
->and($messages)->toBe(['foo' => '`.foo` must be present'])
));
test('With wrapped name, default', catchAll(
fn() => v::keyOptional('foo', v::intType()->setName('Wrapped'))->setName('Wrapper')->assert(['foo' => 'string']),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Wrapped must be an integer')
->and($fullMessage)->toBe('- Wrapped must be an integer')
->and($messages)->toBe(['foo' => 'Wrapped must be an integer'])
));
test('With wrapped name, inverted', catchAll(
fn() => v::not(v::keyOptional('foo', v::intType()->setName('Wrapped'))->setName('Wrapper'))->setName('Not')->assert(['foo' => 12]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Wrapped must not be an integer')
->and($fullMessage)->toBe('- Wrapped must not be an integer')
->and($messages)->toBe(['foo' => 'Wrapped must not be an integer'])
));
test('With wrapper name, default', catchAll(
fn() => v::keyOptional('foo', v::intType())->setName('Wrapper')->assert(['foo' => 'string']),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Wrapper must be an integer')
->and($fullMessage)->toBe('- Wrapper must be an integer')
->and($messages)->toBe(['foo' => 'Wrapper must be an integer'])
));
test('With wrapper name, inverted', catchAll(
fn() => v::not(v::keyOptional('foo', v::intType())->setName('Wrapper'))->setName('Not')->assert(['foo' => 12]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Wrapper must not be an integer')
->and($fullMessage)->toBe('- Wrapper must not be an integer')
->and($messages)->toBe(['foo' => 'Wrapper must not be an integer'])
));
test('With "Not" name, inverted', catchAll(
fn() => v::not(v::keyOptional('foo', v::intType()))->setName('Not')->assert(['foo' => 12]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Not must not be an integer')
->and($fullMessage)->toBe('- Not must not be an integer')
->and($messages)->toBe(['foo' => 'Not must not be an integer'])
));
test('With template, default', catchAll(
fn() => v::keyOptional('foo', v::intType())->assert(['foo' => 'string'], 'That key is off-key'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('That key is off-key')
->and($fullMessage)->toBe('- That key is off-key')
->and($messages)->toBe(['foo' => 'That key is off-key'])
));
test('With template, inverted', catchAll(
fn() => v::not(v::keyOptional('foo', v::intType()))->assert(['foo' => 12], 'No off-key key'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('No off-key key')
->and($fullMessage)->toBe('- No off-key key')
->and($messages)->toBe(['foo' => 'No off-key key'])
));