respect-validation/tests/unit/Transformers/PrefixTest.php
Henrique Moody 562d98d805
Refactor the NotEmpty rule
Since we have the ability to use `not` as a prefix, having rules that
validate negative behaviour makes them a bit inflexible, verbose, and
harder to understand.

This commit will refactor the `NotEmpty`, and rename it to `Falsy`. It
will no longer trim strings, because Blank does a much better job at it;
it only simulates the behaviour of PHP’s native `empty()` function.

Because `Falsy`, `Blank`, and `Undef` have similar behaviour, I created
a page to demonstrate the difference and show when the user should use
one or the other.

Assisted-by: Cursor (claude-4.5-opus-high)
2025-12-29 12:48:35 +01:00

102 lines
3.3 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Transformers;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Test\TestCase;
#[Group('core')]
#[CoversClass(Prefix::class)]
final class PrefixTest extends TestCase
{
#[Test]
#[DataProvider('providerForTransformedRuleSpec')]
public function itShouldTransformRuleSpec(RuleSpec $original, RuleSpec $expected): void
{
$transformer = new Prefix();
$transformed = $transformer->transform($original);
self::assertEquals($expected, $transformed);
}
#[Test]
#[DataProvider('providerForUntransformedRuleNames')]
public function itShouldPreventTransformingCanonicalRule(string $ruleName): void
{
$ruleSpec = new RuleSpec($ruleName);
$transformer = new Prefix();
self::assertSame($ruleSpec, $transformer->transform($ruleSpec));
}
/** @return array<array{RuleSpec, RuleSpec}> */
public static function providerForTransformedRuleSpec(): array
{
return [
'key' => [
new RuleSpec('keyNextRule', ['keyName', 123]),
new RuleSpec('NextRule', [123], new RuleSpec('key', ['keyName'])),
],
'length' => [
new RuleSpec('lengthNextRule', [5]),
new RuleSpec('NextRule', [5], new RuleSpec('length')),
],
'max' => [
new RuleSpec('maxNextRule', [1, 10]),
new RuleSpec('NextRule', [1, 10], new RuleSpec('max')),
],
'min' => [
new RuleSpec('minNextRule', [1, 10]),
new RuleSpec('NextRule', [1, 10], new RuleSpec('min')),
],
'not' => [
new RuleSpec('notNextRule', [1, 10]),
new RuleSpec('NextRule', [1, 10], new RuleSpec('not')),
],
'nullOr' => [
new RuleSpec('nullOrNextRule', [1, 10]),
new RuleSpec('NextRule', [1, 10], new RuleSpec('nullOr')),
],
'property' => [
new RuleSpec('propertyNextRule', ['propertyName', 567]),
new RuleSpec('NextRule', [567], new RuleSpec('property', ['propertyName'])),
],
'undefOr' => [
new RuleSpec('undefOrNextRule', [1, 10]),
new RuleSpec('NextRule', [1, 10], new RuleSpec('undefOr')),
],
];
}
/** @return array<array{string}> */
public static function providerForUntransformedRuleNames(): array
{
return [
'equals' => ['equals'],
'key' => ['key'],
'keyExists' => ['keyExists'],
'keyOptional' => ['keyOptional'],
'keySet' => ['keySet'],
'length' => ['length'],
'max' => ['max'],
'maxAge' => ['maxAge'],
'min' => ['min'],
'minAge' => ['minAge'],
'not' => ['not'],
'undef' => ['undef'],
'property' => ['property'],
'propertyExists' => ['propertyExists'],
'propertyOptional' => ['propertyOptional'],
];
}
}