|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Nuxtifyts\PhpDto\Tests\Unit\Validation; |
| 4 | + |
| 5 | +use Nuxtifyts\PhpDto\Enums\Property\Type; |
| 6 | +use PHPUnit\Framework\Attributes\Test; |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use Nuxtifyts\PhpDto\Validation\Rules\NumericRule; |
| 9 | +use Nuxtifyts\PhpDto\Exceptions\ValidationRuleException; |
| 10 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 11 | +use Throwable; |
| 12 | + |
| 13 | +#[CoversClass(NumericRule::class)] |
| 14 | +#[CoversClass(ValidationRuleException::class)] |
| 15 | +#[UsesClass(Type::class)] |
| 16 | +final class NumericRuleTest extends ValidationRuleTestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @throws Throwable |
| 20 | + */ |
| 21 | + #[Test] |
| 22 | + public function validate_validation_message(): void |
| 23 | + { |
| 24 | + self::assertEquals( |
| 25 | + 'The :attribute field must be a valid integer.', |
| 26 | + NumericRule::make()->validationMessage() |
| 27 | + ); |
| 28 | + |
| 29 | + self::assertEquals( |
| 30 | + 'The :attribute field must be a valid float.', |
| 31 | + NumericRule::make(['type' => Type::FLOAT])->validationMessage() |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @return array<string, array{ |
| 37 | + * validationRuleClassString: class-string<NumericRule>, |
| 38 | + * makeParams: ?array<string, mixed>, |
| 39 | + * expectedMakeException: ?class-string<ValidationRuleException>, |
| 40 | + * valueToBeEvaluated: mixed, |
| 41 | + * expectedResult: bool |
| 42 | + * }> |
| 43 | + */ |
| 44 | + public static function data_provider(): array |
| 45 | + { |
| 46 | + return [ |
| 47 | + 'Will evaluate false when value is not a number' => [ |
| 48 | + 'validationRuleClassString' => NumericRule::class, |
| 49 | + 'makeParams' => null, |
| 50 | + 'expectedMakeException' => null, |
| 51 | + 'valueToBeEvaluated' => 'test', |
| 52 | + 'expectedResult' => false |
| 53 | + ], |
| 54 | + 'Will evaluate true when an integer value is provided' => [ |
| 55 | + 'validationRuleClassString' => NumericRule::class, |
| 56 | + 'makeParams' => ['type' => Type::INT], |
| 57 | + 'expectedMakeException' => null, |
| 58 | + 'valueToBeEvaluated' => 123, |
| 59 | + 'expectedResult' => true |
| 60 | + ], |
| 61 | + 'Will evaluate false when an integer value is provided but min is greater than the value' => [ |
| 62 | + 'validationRuleClassString' => NumericRule::class, |
| 63 | + 'makeParams' => ['type' => Type::INT, 'min' => 124], |
| 64 | + 'expectedMakeException' => null, |
| 65 | + 'valueToBeEvaluated' => 123, |
| 66 | + 'expectedResult' => false |
| 67 | + ], |
| 68 | + 'Will evaluate false when an integer value is provided but max is less than the value' => [ |
| 69 | + 'validationRuleClassString' => NumericRule::class, |
| 70 | + 'makeParams' => ['type' => Type::INT, 'max' => 122], |
| 71 | + 'expectedMakeException' => null, |
| 72 | + 'valueToBeEvaluated' => 123, |
| 73 | + 'expectedResult' => false |
| 74 | + ], |
| 75 | + 'Will evaluate true when a float value is provided' => [ |
| 76 | + 'validationRuleClassString' => NumericRule::class, |
| 77 | + 'makeParams' => ['type' => Type::FLOAT], |
| 78 | + 'expectedMakeException' => null, |
| 79 | + 'valueToBeEvaluated' => 123.45, |
| 80 | + 'expectedResult' => true |
| 81 | + ], |
| 82 | + 'Will evaluate false when a float value is provided but min is greater than the value' => [ |
| 83 | + 'validationRuleClassString' => NumericRule::class, |
| 84 | + 'makeParams' => ['type' => Type::FLOAT, 'min' => 123.46], |
| 85 | + 'expectedMakeException' => null, |
| 86 | + 'valueToBeEvaluated' => 123.45, |
| 87 | + 'expectedResult' => false |
| 88 | + ], |
| 89 | + 'Will evaluate false when a float value is provided but max is less than the value' => [ |
| 90 | + 'validationRuleClassString' => NumericRule::class, |
| 91 | + 'makeParams' => ['type' => Type::FLOAT, 'max' => 123.44], |
| 92 | + 'expectedMakeException' => null, |
| 93 | + 'valueToBeEvaluated' => 123.45, |
| 94 | + 'expectedResult' => false |
| 95 | + ], |
| 96 | + 'Will throw an exception if an invalid type is provided' => [ |
| 97 | + 'validationRuleClassString' => NumericRule::class, |
| 98 | + 'makeParams' => ['type' => Type::BOOLEAN], |
| 99 | + 'expectedMakeException' => ValidationRuleException::class, |
| 100 | + 'valueToBeEvaluated' => 123, |
| 101 | + 'expectedResult' => false |
| 102 | + ], |
| 103 | + ]; |
| 104 | + } |
| 105 | +} |
0 commit comments