Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions packages/validation/tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -292,4 +294,47 @@

$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);

Check failure on line 306 in packages/validation/tests/ValidatorTest.php

View workflow job for this annotation

GitHub Actions / Run tests: validation - PHP 8.5 - prefer-lowest

Failed asserting that two strings are identical.

Check failure on line 306 in packages/validation/tests/ValidatorTest.php

View workflow job for this annotation

GitHub Actions / Run tests: validation - PHP 8.5 - prefer-stable

Failed asserting that two strings are identical.

Check failure on line 306 in packages/validation/tests/ValidatorTest.php

View workflow job for this annotation

GitHub Actions / Run tests: PHP 8.5 - postgres - prefer-stable - ubuntu-latest

Failed asserting that two strings are identical.

Check failure on line 306 in packages/validation/tests/ValidatorTest.php

View workflow job for this annotation

GitHub Actions / Run tests: PHP 8.5 - sqlite - prefer-lowest - ubuntu-latest

Failed asserting that two strings are identical.

Check failure on line 306 in packages/validation/tests/ValidatorTest.php

View workflow job for this annotation

GitHub Actions / Run tests: PHP 8.5 - sqlite - prefer-stable - ubuntu-latest

Failed asserting that two strings are identical.

Check failure on line 306 in packages/validation/tests/ValidatorTest.php

View workflow job for this annotation

GitHub Actions / Run tests: PHP 8.5 - mysql - prefer-stable - ubuntu-latest

Failed asserting that two strings are identical.

Check failure on line 306 in packages/validation/tests/ValidatorTest.php

View workflow job for this annotation

GitHub Actions / Run tests: PHP 8.5 - sqlite - prefer-stable - windows-latest

Failed asserting that two strings are identical.
}

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.';
}
}
Loading