mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 10:40:24 +02:00
Creating a specific exception for each rule adds a painful overhead. If you want to make a custom message for your rule, you will need to create an exception and then register that exception namespace to be able to use it—all that is just for customizing the message of your rule. Having different namespaces also implies that you need to fetch the exception of the rule from another directory to change it. As Uncle Bob said, "Classes that change together belong together. Classes that are not reused together should not be grouped." This commit will drastically change this library, moving all the templates from the exceptions to the rules. Consequently, the Factory becomes much simpler, and the library gets a bit smaller, too. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
101 lines
2.9 KiB
PHP
101 lines
2.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Respect\Validation\Exceptions\ValidationException;
|
|
use Respect\Validation\Test\TestCase;
|
|
use Respect\Validation\Validatable;
|
|
use Respect\Validation\Validator;
|
|
|
|
#[Group('rule')]
|
|
#[CoversClass(Not::class)]
|
|
final class NotTest extends TestCase
|
|
{
|
|
#[Test]
|
|
#[DataProvider('providerForValidNot')]
|
|
public function not(Validatable $rule, mixed $input): void
|
|
{
|
|
$this->expectNotToPerformAssertions();
|
|
|
|
$not = new Not($rule);
|
|
$not->assert($input);
|
|
}
|
|
|
|
#[Test]
|
|
#[DataProvider('providerForInvalidNot')]
|
|
public function notNotHaha(Validatable $rule, mixed $input): void
|
|
{
|
|
$not = new Not($rule);
|
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$not->assert($input);
|
|
}
|
|
|
|
#[Test]
|
|
#[DataProvider('providerForSetName')]
|
|
public function notSetName(Validatable $rule): void
|
|
{
|
|
$not = new Not($rule);
|
|
$not->setName('Foo');
|
|
|
|
self::assertEquals('Foo', $not->getName());
|
|
self::assertEquals('Foo', $not->getNegatedRule()->getName());
|
|
}
|
|
|
|
/**
|
|
* @return array<array{Validatable, mixed}>
|
|
*/
|
|
public static function providerForValidNot(): array
|
|
{
|
|
return [
|
|
[new IntVal(), ''],
|
|
[new IntVal(), 'aaa'],
|
|
[new AllOf(new NoWhitespace(), new Digit()), 'as df'],
|
|
[new AllOf(new NoWhitespace(), new Digit()), '12 34'],
|
|
[new AllOf(new AllOf(new NoWhitespace(), new Digit())), '12 34'],
|
|
[new AllOf(new NoneOf(new NumericVal(), new IntVal())), 13.37],
|
|
[new NoneOf(new NumericVal(), new IntVal()), 13.37],
|
|
[Validator::noneOf(Validator::numericVal(), Validator::intVal()), 13.37],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<array{Validatable, mixed}>
|
|
*/
|
|
public static function providerForInvalidNot(): array
|
|
{
|
|
return [
|
|
[new IntVal(), 123],
|
|
[new AllOf(new AnyOf(new NumericVal(), new IntVal())), 13.37],
|
|
[new AnyOf(new NumericVal(), new IntVal()), 13.37],
|
|
[Validator::anyOf(Validator::numericVal(), Validator::intVal()), 13.37],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<array{Validatable}>
|
|
*/
|
|
public static function providerForSetName(): array
|
|
{
|
|
return [
|
|
'non-allOf' => [new IntVal()],
|
|
'allOf' => [new AllOf(new NumericVal(), new IntVal())],
|
|
'not' => [new Not(new Not(new IntVal()))],
|
|
'allOf with name' => [Validator::intVal()->setName('Bar')],
|
|
'noneOf' => [Validator::noneOf(Validator::numericVal(), Validator::intVal())],
|
|
];
|
|
}
|
|
}
|