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