respect-validation/tests/unit/Exceptions/InvalidRuleConstructorExceptionTest.php
Henrique Moody d1f108dc87
Make proper use of exceptions in rules
This commit will ensure that all rules that cannot be created because of
invalid arguments in the constructor will throw the
InvalidRuleConstructorException. It will also make ComponentException
extend LogicException, which makes it easier to determine that the
client has improperly used the library.

I also introduced some tests for two exceptions with logic in their
constructor.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-25 22:09:02 +01:00

42 lines
1.4 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Exceptions;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Test\TestCase;
#[CoversClass(InvalidRuleConstructorException::class)]
final class InvalidRuleConstructorExceptionTest extends TestCase
{
/** @param array<string|array<string>> $arguments */
#[Test]
#[DataProvider('providerForMessages')]
public function itShouldCreateMessageForWithString(string $expect, string $format, array $arguments): void
{
$exception = new InvalidRuleConstructorException($format, ...$arguments);
self::assertEquals($expect, $exception->getMessage());
}
/** @return array<string, array{0: string, 1: string, 2: array<string|array<string>>}> */
public static function providerForMessages(): array
{
return [
'with 1 argument' => ['-arg-', '-%s-', ['arg']],
'with 2 arguments' => ['-arg1- _arg2_', '-%s- _%s_', ['arg1', 'arg2']],
'with an array of 1 elements' => ['_"arg1"_', '_%s_', [['arg1']]],
'with an array of 2 elements' => ['_"arg1" and "arg2"_', '_%s_', [['arg1', 'arg2']]],
'with an array of 3+ elements' => ['_"arg1", "arg2", and "arg3"_', '_%s_', [['arg1', 'arg2', 'arg3']]],
];
}
}