|
7 | 7 | use PHPUnit\Framework\TestCase; |
8 | 8 | use Tempest\Reflection\ClassReflector; |
9 | 9 | use Tempest\Validation\Exceptions\ValidationFailed; |
| 10 | +use Tempest\Validation\FailingRule; |
10 | 11 | use Tempest\Validation\HasErrorMessage; |
| 12 | +use Tempest\Validation\Rule; |
11 | 13 | use Tempest\Validation\Rules\HasLength; |
12 | 14 | use Tempest\Validation\Rules\IsBoolean; |
13 | 15 | use Tempest\Validation\Rules\IsEmail; |
@@ -292,4 +294,47 @@ public function test_validate_values_all_valid(): void |
292 | 294 |
|
293 | 295 | $this->assertCount(0, $failingRules); |
294 | 296 | } |
| 297 | + |
| 298 | + public function test_validator_returns_correct_error_message_when_rule_has_error_message(): void |
| 299 | + { |
| 300 | + $ruleMessage = $this->validator->getErrorMessage(new IsForTestingHasErrorMessage()); |
| 301 | + |
| 302 | + $this->assertSame('This is a test error message.', $ruleMessage); |
| 303 | + |
| 304 | + $failingRuleMessage = $this->validator->getErrorMessage(new FailingRule(rule: new IsForTestingHasErrorMessage())); |
| 305 | + |
| 306 | + $this->assertSame('This is a test error message.', $failingRuleMessage); |
| 307 | + } |
| 308 | + |
| 309 | + public function test_validator_returns_correct_error_message(): void |
| 310 | + { |
| 311 | + $ruleMessage = $this->validator->getErrorMessage(new IsForTestingMessage()); |
| 312 | + |
| 313 | + $this->assertSame('validation_error.is_for_testing_message', $ruleMessage); |
| 314 | + |
| 315 | + $failingRuleMessage = $this->validator->getErrorMessage(new FailingRule(rule: new IsForTestingMessage())); |
| 316 | + |
| 317 | + $this->assertSame('validation_error.is_for_testing_message', $failingRuleMessage); |
| 318 | + } |
| 319 | +} |
| 320 | + |
| 321 | +final readonly class IsForTestingMessage implements Rule |
| 322 | +{ |
| 323 | + public function isValid(mixed $value): bool |
| 324 | + { |
| 325 | + return false; |
| 326 | + } |
| 327 | +} |
| 328 | + |
| 329 | +final readonly class IsForTestingHasErrorMessage implements Rule, HasErrorMessage |
| 330 | +{ |
| 331 | + public function isValid(mixed $value): bool |
| 332 | + { |
| 333 | + return false; |
| 334 | + } |
| 335 | + |
| 336 | + public function getErrorMessage(): string |
| 337 | + { |
| 338 | + return 'This is a test error message.'; |
| 339 | + } |
295 | 340 | } |
0 commit comments