Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit ea55416

Browse files
committed
feat(tests): added custom message rule tests
1 parent 2ad4d0c commit ea55416

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Test\Util;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
7+
8+
trait TestRuleCustomMessageTrait
9+
{
10+
public static abstract function provideRuleCustomMessageData(): \Generator;
11+
12+
#[DataProvider('provideRuleCustomMessageData')]
13+
public function testRuleCustomMessage(RuleInterface $rule, mixed $value, string $exceptionMessage): void
14+
{
15+
$this->expectExceptionMessage($exceptionMessage);
16+
$rule->assert($value, 'test');
17+
}
18+
}

tests/GreaterThanTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\GreaterThan;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleCustomMessageTrait;
78
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
89
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
910

1011
class GreaterThanTest extends AbstractTest
1112
{
1213
use TestRuleFailureConditionTrait;
1314
use TestRuleSuccessConditionTrait;
15+
use TestRuleCustomMessageTrait;
1416

1517
public static function provideRuleFailureConditionData(): \Generator
1618
{
@@ -46,4 +48,14 @@ public static function provideRuleSuccessConditionData(): \Generator
4648
yield 'int with float' => [new GreaterThan(10), 20.0];
4749
yield 'string' => [new GreaterThan('a'), 'z'];
4850
}
51+
52+
public static function provideRuleCustomMessageData(): \Generator
53+
{
54+
yield 'message' => [
55+
new GreaterThan(
56+
constraint: 10,
57+
message: 'The "{{ name }}" value "{{ value }}" is not greater than "{{ constraint }}".'
58+
), 1, 'The "test" value "1" is not greater than "10".'
59+
];
60+
}
4961
}

tests/NotBlankTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\NotBlankException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\NotBlank;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleCustomMessageTrait;
78
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
89
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
910

1011
class NotBlankTest extends AbstractTest
1112
{
1213
use TestRuleFailureConditionTrait;
1314
use TestRuleSuccessConditionTrait;
15+
use TestRuleCustomMessageTrait;
1416

1517
public static function provideRuleFailureConditionData(): \Generator
1618
{
@@ -39,4 +41,13 @@ public static function provideRuleSuccessConditionData(): \Generator
3941
yield 'number' => [new NotBlank(), 10];
4042
yield 'zero number' => [new NotBlank(), 0];
4143
}
44+
45+
public static function provideRuleCustomMessageData(): \Generator
46+
{
47+
yield 'message' => [
48+
new NotBlank(
49+
message: 'The "{{ name }}" value "{{ value }}" is not blank.'
50+
), '', 'The "test" value "" is not blank.'
51+
];
52+
}
4253
}

0 commit comments

Comments
 (0)