respect-validation/tests/unit/Exceptions/CheckExceptionsTest.php
Henrique Moody 9a13c9fb03
Update coding standards
This change will bring many breaking changes. The good thing is that we
can finally use more modern resources available in PHP.

I can imagine that's not a popular change since it will bring many
breaking changes to users, but we shouldn't be stuck in time because of
that. Using some of those features will make it easier to contribute to
the project. At least, I hope so.

There are still some useless doc-blocks, and we're not using "readonly"
properties when we could. I aim to send those changes soon.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 00:22:41 +01:00

77 lines
2 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Exceptions;
use DirectoryIterator;
use ReflectionClass;
use Respect\Validation\Test\TestCase;
use function class_exists;
use function mb_substr;
use function sprintf;
/**
* @coversNothing
*/
final class CheckExceptionsTest extends TestCase
{
/**
* @dataProvider provideListOfRuleNames
* @test
*/
public function ruleHasAnExceptionWhichHasValidApi(string $ruleName): void
{
$exceptionClass = 'Respect\\Validation\\Exceptions\\' . $ruleName . 'Exception';
self::assertTrue(
class_exists($exceptionClass),
sprintf('Expected exception class to exist: %s.', $ruleName)
);
$reflectionClass = new ReflectionClass($exceptionClass);
self::assertTrue(
$reflectionClass->isSubclassOf(ValidationException::class),
'Every Respect/Validation exception must extend ValidationException.'
);
}
/**
* @return string[][]
*/
public static function provideListOfRuleNames(): array
{
$rulesDirectory = 'library/Rules';
$rulesDirectoryIterator = new DirectoryIterator($rulesDirectory);
$ruleNames = [];
foreach ($rulesDirectoryIterator as $fileInfo) {
if ($fileInfo->isDir()) {
continue;
}
$ruleName = mb_substr($fileInfo->getBasename(), 0, -4);
if (($fileInfo->getExtension() !== 'php')) {
continue;
}
$className = 'Respect\\Validation\\Rules\\' . $ruleName;
if (!class_exists($className)) {
continue;
}
$reflectionClass = new ReflectionClass($className);
if ($reflectionClass->isAbstract() || $reflectionClass->isInterface()) {
continue;
}
$ruleNames[] = [$ruleName];
}
return $ruleNames;
}
}