From ce097a7e9ac3fa2e7f364358f8eec3fcf2e17ba0 Mon Sep 17 00:00:00 2001 From: NeoIsRecursive Date: Thu, 25 Jun 2026 23:45:44 +0200 Subject: [PATCH] feat: add failing `Validator::getErrorMessage` test and passing for translation keys --- packages/validation/tests/ValidatorTest.php | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/packages/validation/tests/ValidatorTest.php b/packages/validation/tests/ValidatorTest.php index d8f99cfe6..7726e326e 100644 --- a/packages/validation/tests/ValidatorTest.php +++ b/packages/validation/tests/ValidatorTest.php @@ -7,7 +7,9 @@ use PHPUnit\Framework\TestCase; use Tempest\Reflection\ClassReflector; use Tempest\Validation\Exceptions\ValidationFailed; +use Tempest\Validation\FailingRule; use Tempest\Validation\HasErrorMessage; +use Tempest\Validation\Rule; use Tempest\Validation\Rules\HasLength; use Tempest\Validation\Rules\IsBoolean; use Tempest\Validation\Rules\IsEmail; @@ -292,4 +294,47 @@ public function test_validate_values_all_valid(): void $this->assertCount(0, $failingRules); } + + public function test_validator_returns_correct_error_message_when_rule_has_error_message(): void + { + $ruleMessage = $this->validator->getErrorMessage(new IsForTestingHasErrorMessage()); + + $this->assertSame('This is a test error message.', $ruleMessage); + + $failingRuleMessage = $this->validator->getErrorMessage(new FailingRule(rule: new IsForTestingHasErrorMessage())); + + $this->assertSame('This is a test error message.', $failingRuleMessage); + } + + public function test_validator_returns_correct_error_message(): void + { + $ruleMessage = $this->validator->getErrorMessage(new IsForTestingMessage()); + + $this->assertSame('validation_error.is_for_testing_message', $ruleMessage); + + $failingRuleMessage = $this->validator->getErrorMessage(new FailingRule(rule: new IsForTestingMessage())); + + $this->assertSame('validation_error.is_for_testing_message', $failingRuleMessage); + } +} + +final readonly class IsForTestingMessage implements Rule +{ + public function isValid(mixed $value): bool + { + return false; + } +} + +final readonly class IsForTestingHasErrorMessage implements Rule, HasErrorMessage +{ + public function isValid(mixed $value): bool + { + return false; + } + + public function getErrorMessage(): string + { + return 'This is a test error message.'; + } }