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

Commit d92755b

Browse files
committed
feat(test): added All tests
1 parent cd8f1b9 commit d92755b

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

src/Rule/All.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function assert(mixed $value, string $name): void
3737

3838
if (!\is_array($value)) {
3939
throw new UnexpectedValueException(
40-
\sprintf('Expected value of type "array", "%s" given', get_debug_type($value))
40+
\sprintf('Expected value of type "array", "%s" given.', get_debug_type($value))
4141
);
4242
}
4343

src/Rule/Util/AssertIsComparableTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private function assertIsComparable(mixed $value1, mixed $value2): bool
2222

2323
throw new UnexpectedValueException(
2424
\sprintf(
25-
'Cannot compare a type "%s" with a type "%s"',
25+
'Cannot compare a type "%s" with a type "%s".',
2626
get_debug_type($value1),
2727
get_debug_type($value2)
2828
)

tests/AllTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Test;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\AllException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\All;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\GreaterThan;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\NotBlank;
9+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
10+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleMessageOptionTrait;
11+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
12+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleUnexpectedValueTrait;
13+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
14+
15+
class AllTest extends AbstractTest
16+
{
17+
use TestRuleUnexpectedValueTrait;
18+
use TestRuleFailureConditionTrait;
19+
use TestRuleSuccessConditionTrait;
20+
use TestRuleMessageOptionTrait;
21+
22+
public static function provideRuleUnexpectedValueData(): \Generator
23+
{
24+
yield 'invalid constraint' => [
25+
new All([new NotBlank(), 'invalid']),
26+
[1, 2, 3],
27+
'/Expected constraint of type "RuleInterface", "(.*)" given./'
28+
];
29+
yield 'invalid value type' => [
30+
new All([new NotBlank()]),
31+
'invalid',
32+
'/Expected value of type "array", "(.*)" given./'
33+
];
34+
yield 'unexpected value propagation' => [
35+
new All([new GreaterThan(10)]),
36+
['a'],
37+
'/Cannot compare a type "(.*)" with a type "(.*)"./'
38+
];
39+
}
40+
41+
public static function provideRuleFailureConditionData(): \Generator
42+
{
43+
$exception = AllException::class;
44+
$message = '/At "(.*)": The "(.*)" value should not be blank, "(.*)" given./';
45+
46+
yield 'constraint' => [
47+
new All([new NotBlank()]),
48+
[1, 2, ''],
49+
$exception,
50+
$message
51+
];
52+
yield 'validator' => [
53+
new All([(new Validator(new NotBlank()))]),
54+
[1, 2, ''],
55+
$exception,
56+
$message
57+
];
58+
}
59+
60+
public static function provideRuleSuccessConditionData(): \Generator
61+
{
62+
yield 'constraints' => [
63+
new All([new NotBlank(), new GreaterThan(1)]),
64+
[2, 3, 4]
65+
];
66+
yield 'validators' => [
67+
new All([
68+
(new Validator(new NotBlank())),
69+
(new Validator(new GreaterThan(1)))
70+
]),
71+
[2, 3, 4]
72+
];
73+
yield 'constraints and validators' => [
74+
new All([
75+
new NotBlank(),
76+
(new Validator(new GreaterThan(1)))
77+
]),
78+
[2, 3, 4]
79+
];
80+
}
81+
82+
public static function provideRuleMessageOptionData(): \Generator
83+
{
84+
yield 'constraint' => [
85+
new All(
86+
constraints: [new NotBlank()],
87+
options: [
88+
'message' => 'The "{{ name }}" value "{{ value }}" failed at key "{{ key }}".'
89+
]
90+
), [1, 2, ''], 'The "test" value "[1, 2, ]" failed at key "2".'
91+
];
92+
}
93+
}

0 commit comments

Comments
 (0)