respect-validation/tests/unit/Transformers/PrefixTest.php
Henrique Moody 97b243daa1
Allow building rules using prefixes
Although helpful, the changes in the Min, Max, and Length rules made
using those rules more verbose. This commit will simplify their use by
allowing users to use them as prefixes.

Because I was creating prefixes for those rules, I made other cool
prefixes. Doing that is scary because it will generate more code to
support, and I would have liked to avoid that. However, that's a
valuable addition, and it's worth the risk.

I might reconsider that in the future, but for now, that looks like a
good idea.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-24 16:58:24 +01:00

106 lines
3.5 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('nullable')),
],
'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('optional')),
],
];
}
/** @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'],
'notBlank' => ['notBlank'],
'notEmoji' => ['notEmoji'],
'notEmpty' => ['notEmpty'],
'notOptional' => ['notOptional'],
'property' => ['property'],
'propertyExists' => ['propertyExists'],
'propertyOptional' => ['propertyOptional'],
];
}
}