mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 15:50:03 +01:00
That enables more flexibility when creating data providers, allowing the use of generators, for example. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
95 lines
2.7 KiB
PHP
95 lines
2.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Test;
|
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Respect\Validation\Validatable;
|
|
|
|
use function implode;
|
|
use function ltrim;
|
|
use function realpath;
|
|
use function Respect\Stringifier\stringify;
|
|
use function sprintf;
|
|
use function strrchr;
|
|
use function substr;
|
|
|
|
abstract class RuleTestCase extends TestCase
|
|
{
|
|
/**
|
|
* Data providers for valid results.
|
|
*
|
|
* It returns an array of arrays. Each array contains an instance of Validatable
|
|
* as the first element and an input in which the validation SHOULD pass.
|
|
*
|
|
* @api
|
|
* @return iterable<string|int, array{Validatable, mixed}>
|
|
*/
|
|
abstract public static function providerForValidInput(): iterable;
|
|
|
|
/**
|
|
* Data providers for invalid results.
|
|
*
|
|
* It returns an array of arrays. Each array contains an instance of Validatable
|
|
* as the first element and an input in which the validation SHOULD NOT pass.
|
|
*
|
|
* @api
|
|
* @return iterable<string|int, array{Validatable, mixed}>
|
|
*/
|
|
abstract public static function providerForInvalidInput(): iterable;
|
|
|
|
#[Test]
|
|
#[DataProvider('providerForValidInput')]
|
|
public function shouldValidateValidInput(Validatable $validator, mixed $input): void
|
|
{
|
|
self::assertValidInput($validator, $input);
|
|
}
|
|
|
|
#[Test]
|
|
#[DataProvider('providerForInvalidInput')]
|
|
public function shouldValidateInvalidInput(Validatable $validator, mixed $input): void
|
|
{
|
|
self::assertInvalidInput($validator, $input);
|
|
}
|
|
|
|
public static function fixture(?string $filename = null): string
|
|
{
|
|
$parts = [(string) realpath(__DIR__ . '/../fixtures')];
|
|
if ($filename !== null) {
|
|
$parts[] = ltrim($filename, '/');
|
|
}
|
|
|
|
return implode('/', $parts);
|
|
}
|
|
|
|
public static function assertValidInput(Validatable $rule, mixed $input): void
|
|
{
|
|
self::assertTrue(
|
|
$rule->validate($input),
|
|
sprintf(
|
|
'%s should pass with %s',
|
|
substr((string) strrchr($rule::class, '\\'), 1),
|
|
stringify($rule->reportError($input)->getParams())
|
|
)
|
|
);
|
|
}
|
|
|
|
public static function assertInvalidInput(Validatable $rule, mixed $input): void
|
|
{
|
|
self::assertFalse(
|
|
$rule->validate($input),
|
|
sprintf(
|
|
'%s should not pass with %s',
|
|
substr((string) strrchr($rule::class, '\\'), 1),
|
|
stringify($rule->reportError($input)->getParams())
|
|
)
|
|
);
|
|
}
|
|
}
|