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>
This commit is contained in:
Henrique Moody 2024-03-25 21:58:13 +01:00
parent ae7a20f6d3
commit d1f108dc87
No known key found for this signature in database
GPG key ID: 221E9281655813A6
23 changed files with 121 additions and 58 deletions

View file

@ -9,9 +9,9 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
use Exception;
use LogicException;
use Throwable;
class ComponentException extends Exception implements Throwable
class ComponentException extends LogicException implements Throwable
{
}

View file

@ -9,7 +9,7 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
@ -17,7 +17,6 @@ use Respect\Validation\Rules\Core\Standard;
use function mb_strlen;
use function mb_substr;
use function preg_match;
use function sprintf;
#[Template(
'{{name}} must be a number in the base {{base|raw}}',
@ -31,7 +30,7 @@ final class Base extends Standard
) {
$max = mb_strlen($this->chars);
if ($base > $max) {
throw new ComponentException(sprintf('a base between 1 and %s is required', $max));
throw new InvalidRuleConstructorException('a base between 1 and %s is required', (string) $max);
}
}

View file

@ -9,7 +9,7 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Helpers\CanCompareValues;
use Respect\Validation\Message\Template;
use Respect\Validation\Rules\Core\Envelope;
@ -25,7 +25,7 @@ final class Between extends Envelope
public function __construct(mixed $minValue, mixed $maxValue)
{
if ($this->toComparable($minValue) >= $this->toComparable($maxValue)) {
throw new ComponentException('Minimum cannot be less than or equals to maximum');
throw new InvalidRuleConstructorException('Minimum cannot be less than or equals to maximum');
}
parent::__construct(

View file

@ -15,7 +15,6 @@ use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
use function array_keys;
use function implode;
use function is_scalar;
use function preg_match;
use function preg_replace;
@ -60,7 +59,7 @@ final class CreditCard extends Standard
throw new InvalidRuleConstructorException(
'"%s" is not a valid credit card brand (Available: %s)',
$brand,
implode(', ', array_keys(self::BRAND_REGEX_LIST))
array_keys(self::BRAND_REGEX_LIST)
);
}
}

View file

@ -9,7 +9,7 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Helpers\CanValidateDateTime;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
@ -18,7 +18,6 @@ use Respect\Validation\Rules\Core\Standard;
use function date;
use function is_scalar;
use function preg_match;
use function sprintf;
use function strtotime;
#[Template(
@ -33,7 +32,7 @@ final class Date extends Standard
private readonly string $format = 'Y-m-d'
) {
if (!preg_match('/^[djSFmMnYy\W]+$/', $format)) {
throw new ComponentException(sprintf('"%s" is not a valid date format', $format));
throw new InvalidRuleConstructorException('"%s" is not a valid date format', $format);
}
}

View file

@ -9,7 +9,7 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Message\Template;
use Respect\Validation\Rules\Core\Envelope;
@ -47,7 +47,7 @@ final class FilterVar extends Envelope
public function __construct(int $filter, mixed $options = null)
{
if (!array_key_exists($filter, self::ALLOWED_FILTERS)) {
throw new ComponentException('Cannot accept the given filter');
throw new InvalidRuleConstructorException('Cannot accept the given filter');
}
$arguments = [$filter];

View file

@ -9,7 +9,7 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
@ -103,11 +103,11 @@ final class Ip extends Standard
[$this->startAddress, $this->endAddress] = explode('-', $input);
if ($this->startAddress !== null && !$this->verifyAddress($this->startAddress)) {
throw new ComponentException('Invalid network range');
throw new InvalidRuleConstructorException('Invalid network range');
}
if ($this->endAddress !== null && !$this->verifyAddress($this->endAddress)) {
throw new ComponentException('Invalid network range');
throw new InvalidRuleConstructorException('Invalid network range');
}
return;
@ -125,7 +125,7 @@ final class Ip extends Standard
return;
}
throw new ComponentException('Invalid network range');
throw new InvalidRuleConstructorException('Invalid network range');
}
private function fillAddress(string $address, string $fill = '*'): string
@ -155,7 +155,7 @@ final class Ip extends Standard
}
if ($isAddressMask || $parts[1] < 8 || $parts[1] > 30) {
throw new ComponentException('Invalid network mask');
throw new InvalidRuleConstructorException('Invalid network mask');
}
$this->mask = sprintf('%032b', ip2long((string) long2ip(~(2 ** (32 - (int) $parts[1]) - 1))));

View file

@ -9,12 +9,10 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Message\Template;
use Respect\Validation\Rules\Core\Envelope;
use function sprintf;
/**
* @see http://download.geonames.org/export/dump/countryInfo.txt
*/
@ -211,7 +209,7 @@ final class PostalCode extends Envelope
{
$countryCodeRule = new CountryCode();
if (!$countryCodeRule->evaluate($countryCode)->isValid) {
throw new ComponentException(sprintf('Cannot validate postal code from "%s" country', $countryCode));
throw new InvalidRuleConstructorException('Cannot validate postal code from "%s" country', $countryCode);
}
parent::__construct(

View file

@ -11,7 +11,7 @@ namespace Respect\Validation\Rules;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
@ -22,7 +22,6 @@ use function floatval;
use function is_numeric;
use function is_string;
use function preg_match;
use function sprintf;
#[Template(
'{{name}} must be between {{minSize}} and {{maxSize}}',
@ -118,7 +117,7 @@ final class Size extends Standard
}
if (!is_numeric($value)) {
throw new ComponentException(sprintf('"%s" is not a recognized file size.', $size));
throw new InvalidRuleConstructorException('"%s" is not a recognized file size.', $size);
}
return (float) $value;

View file

@ -9,7 +9,7 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
@ -18,7 +18,6 @@ use function array_values;
use function count;
use function is_array;
use function is_string;
use function sprintf;
use function str_split;
#[Template(
@ -43,8 +42,10 @@ final class Sorted extends Standard
private readonly string $direction
) {
if ($direction !== self::ASCENDING && $direction !== self::DESCENDING) {
throw new ComponentException(
sprintf('Direction should be either "%s" or "%s"', self::ASCENDING, self::DESCENDING)
throw new InvalidRuleConstructorException(
'Direction should be either "%s" or "%s"',
self::ASCENDING,
self::DESCENDING
);
}
}

View file

@ -9,7 +9,7 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Helpers\CanValidateDateTime;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
@ -18,7 +18,6 @@ use Respect\Validation\Rules\Core\Standard;
use function date;
use function is_scalar;
use function preg_match;
use function sprintf;
use function strtotime;
#[Template(
@ -33,7 +32,7 @@ final class Time extends Standard
private readonly string $format = 'H:i:s'
) {
if (!preg_match('/^[gGhHisuvaA\W]+$/', $format)) {
throw new ComponentException(sprintf('"%s" is not a valid date format', $format));
throw new InvalidRuleConstructorException('"%s" is not a valid date format', $format);
}
}

View file

@ -0,0 +1,41 @@
<?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']]],
];
}
}

View file

@ -0,0 +1,28 @@
<?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\Test;
use Respect\Validation\Test\TestCase;
#[CoversClass(MissingComposerDependencyException::class)]
final class MissingComposerDependencyExceptionTest extends TestCase
{
#[Test]
public function itShouldCreateMessageForMultipleDependencies(): void
{
$exception = new MissingComposerDependencyException('message', 'dependency1', 'dependency2');
$expected = 'message. Run `composer require dependency1 dependency2` to fix this issue.';
self::assertEquals($expected, $exception->getMessage());
}
}

View file

@ -13,7 +13,7 @@ use DateTime;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;
use Respect\Validation\Test\Stubs\CountableStub;
@ -24,7 +24,8 @@ final class BetweenTest extends RuleTestCase
#[Test]
public function minimumValueShouldNotBeGreaterThanMaximumValue(): void
{
$this->expectExceptionObject(new ComponentException('Minimum cannot be less than or equals to maximum'));
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessage('Minimum cannot be less than or equals to maximum');
new Between(10, 5);
}
@ -32,7 +33,8 @@ final class BetweenTest extends RuleTestCase
#[Test]
public function minimumValueShouldNotBeEqualsToMaximumValue(): void
{
$this->expectExceptionObject(new ComponentException('Minimum cannot be less than or equals to maximum'));
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessage('Minimum cannot be less than or equals to maximum');
new Between(5, 5);
}

View file

@ -12,7 +12,7 @@ namespace Respect\Validation\Rules;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;
#[Group('rule')]
@ -22,11 +22,8 @@ final class CreditCardTest extends RuleTestCase
#[Test]
public function itShouldThrowExceptionWhenCreditCardBrandIsNotValid(): void
{
$message = '"RespectCard" is not a valid credit card brand';
$message .= ' (Available: Any, American Express, Diners Club, Discover, JCB, MasterCard, Visa, RuPay)';
$this->expectException(ComponentException::class);
$this->expectExceptionMessage($message);
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessageMatches('/"RespectCard" is not a valid credit card brand \(Available: .+\)/');
new CreditCard('RespectCard');
}

View file

@ -15,7 +15,7 @@ use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;
#[Group('rule')]
@ -26,7 +26,7 @@ final class DateTest extends RuleTestCase
#[DataProvider('validFormatsProvider')]
public function shouldThrowAnExceptionWhenFormatIsNotValid(string $format): void
{
$this->expectException(ComponentException::class);
$this->expectException(InvalidRuleConstructorException::class);
new Date($format);
}

View file

@ -12,7 +12,7 @@ namespace Respect\Validation\Rules;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;
use const FILTER_FLAG_HOSTNAME;
@ -32,7 +32,7 @@ final class FilterVarTest extends RuleTestCase
#[Test]
public function itShouldThrowsExceptionWhenFilterIsNotValid(): void
{
$this->expectException(ComponentException::class);
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessage('Cannot accept the given filter');
new FilterVar(FILTER_SANITIZE_EMAIL);

View file

@ -13,7 +13,7 @@ use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;
use function extension_loaded;
@ -29,7 +29,7 @@ final class IpTest extends RuleTestCase
#[DataProvider('providerForInvalidRanges')]
public function invalidRangeShouldRaiseException(string $range): void
{
$this->expectException(ComponentException::class);
$this->expectException(InvalidRuleConstructorException::class);
new Ip($range);
}

View file

@ -12,7 +12,7 @@ namespace Respect\Validation\Rules;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;
#[Group('rule')]
@ -38,7 +38,7 @@ final class PostalCodeTest extends RuleTestCase
#[Test]
public function shouldThrowsExceptionWhenCountryCodeIsNotValid(): void
{
$this->expectException(ComponentException::class);
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessage('Cannot validate postal code from "Whatever" country');
new PostalCode('Whatever');

View file

@ -14,7 +14,7 @@ use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;
use Respect\Validation\Test\Stubs\StreamStub;
use Respect\Validation\Test\Stubs\UploadedFileStub;
@ -27,7 +27,7 @@ final class SizeTest extends RuleTestCase
#[Test]
public function shouldThrowsAnExceptionWhenSizeIsNotValid(): void
{
$this->expectException(ComponentException::class);
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessage('"42jb" is not a recognized file size');
new Size('42jb');

View file

@ -12,7 +12,7 @@ namespace Respect\Validation\Rules;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;
use stdClass;
@ -23,7 +23,8 @@ final class SortedTest extends RuleTestCase
#[Test]
public function itShouldNotAcceptWrongSortingDirection(): void
{
$this->expectExceptionObject(new ComponentException('Direction should be either "ASC" or "DESC"'));
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessage('Direction should be either "ASC" or "DESC"');
new Sorted('something');
}

View file

@ -12,7 +12,7 @@ namespace Respect\Validation\Rules;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;
#[Group('rule')]
@ -22,7 +22,7 @@ final class SubdivisionCodeTest extends RuleTestCase
#[Test]
public function shouldNotAcceptWrongNamesOnConstructor(): void
{
$this->expectException(ComponentException::class);
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessage('"whatever" is not a supported country code');
new SubdivisionCode('whatever');

View file

@ -15,7 +15,7 @@ use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;
#[Group('rule')]
@ -26,7 +26,7 @@ final class TimeTest extends RuleTestCase
#[DataProvider('invalidFormatsProvider')]
public function shouldThrowAnExceptionWhenFormatIsNotValid(string $format): void
{
$this->expectException(ComponentException::class);
$this->expectException(InvalidRuleConstructorException::class);
new Time($format);
}