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

Commit cb9a110

Browse files
committed
feat(tests): added Choice tests
1 parent 729ecff commit cb9a110

5 files changed

Lines changed: 156 additions & 54 deletions

File tree

src/Rule/Choice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function assert(mixed $value, string $name): void
3939
{
4040
if ($this->isMultiple && !\is_array($value)) {
4141
throw new UnexpectedValueException(
42-
\sprintf('Expected value of type "array", "%s" given', get_debug_type($value))
42+
\sprintf('Expected value of type "array" when is multiple, "%s" given', get_debug_type($value))
4343
);
4444
}
4545

tests/ChoiceTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Test;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ChoiceException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Choice;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleMessageOptionTrait;
9+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
10+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleUnexpectedValueTrait;
11+
12+
class ChoiceTest extends AbstractTest
13+
{
14+
use TestRuleUnexpectedValueTrait;
15+
use TestRuleFailureConditionTrait;
16+
use TestRuleSuccessConditionTrait;
17+
use TestRuleMessageOptionTrait;
18+
19+
public static function provideRuleUnexpectedValueData(): \Generator
20+
{
21+
yield 'multiple but not array' => [
22+
new Choice([1, 2], true), 1, '/Expected value of type "array" when is multiple, "(.*)" given/'
23+
];
24+
}
25+
26+
public static function provideRuleFailureConditionData(): \Generator
27+
{
28+
$constraints = [1, 2, 3, 4, 5];
29+
$exception = ChoiceException::class;
30+
$message = '/The "(.*)" value is not a valid choice, "(.*)" given. Accepted values are: "(.*)"./';
31+
$multipleMessage = '/The "(.*)" value has one or more invalid choices, "(.*)" given. Accepted values are: "(.*)"./';
32+
$maxMessage = '/The "(.*)" value must have at most (.*) choices, (.*) choices given./';
33+
$minMessage = '/The "(.*)" value must have at least (.*) choices, (.*) choices given./';
34+
35+
yield 'invalid choice' => [new Choice($constraints), 10, $exception, $message];
36+
yield 'invalid choice type' => [new Choice($constraints), '1', $exception, $message];
37+
yield 'multiple invalid choice' => [new Choice($constraints, true), [10], $exception, $multipleMessage];
38+
yield 'multiple invalid choice type' => [new Choice($constraints, true), ['1'], $exception, $multipleMessage];
39+
yield 'multiple invalid choice with valid' => [new Choice($constraints, true), [1, 10], $exception, $multipleMessage];
40+
yield 'min constraint' => [new Choice($constraints, true, 2), [1], $exception, $minMessage];
41+
yield 'max constraint' => [new Choice($constraints, true, null, 2), [1, 2, 3], $exception, $maxMessage];
42+
}
43+
44+
public static function provideRuleSuccessConditionData(): \Generator
45+
{
46+
$constraints = [1, 2, 3, 4, 5];
47+
48+
yield 'valid choice' => [new Choice($constraints), 1];
49+
yield 'multiple valid choice' => [new Choice($constraints, true), [1]];
50+
yield 'multiple valid choices' => [new Choice($constraints, true), [1, 2, 3]];
51+
yield 'min constraint' => [new Choice($constraints, true, 2), [1, 2]];
52+
yield 'max constraint' => [new Choice($constraints, true, null, 2), [1, 2]];
53+
yield 'min and max constraint' => [new Choice($constraints, true, 2, 2), [1, 2]];
54+
}
55+
56+
public static function provideRuleMessageOptionData(): \Generator
57+
{
58+
$constraints = [1, 2, 3, 4, 5];
59+
60+
yield 'message' => [
61+
new Choice(
62+
constraints: $constraints,
63+
options: [
64+
'message' => 'The "{{ name }}" value "{{ value }}" is not a valid choice.'
65+
]
66+
), 10, 'The "test" value "10" is not a valid choice.'
67+
];
68+
yield 'multiple message' => [
69+
new Choice(
70+
constraints: $constraints,
71+
isMultiple: true,
72+
options: [
73+
'multipleMessage' => 'The "{{ name }}" value "{{ value }}" is not a valid choice.'
74+
]
75+
), [10], 'The "test" value "[10]" is not a valid choice.'
76+
];
77+
yield 'min message' => [
78+
new Choice(
79+
constraints: $constraints,
80+
isMultiple: true,
81+
minConstraint: 2,
82+
options: [
83+
'minMessage' => 'The "{{ name }}" value should have at least {{ minConstraint }} choices.'
84+
]
85+
), [1], 'The "test" value should have at least 2 choices.'
86+
];
87+
yield 'max message' => [
88+
new Choice(
89+
constraints: $constraints,
90+
isMultiple: true,
91+
maxConstraint: 2,
92+
options: [
93+
'maxMessage' => 'The "{{ name }}" value should have at most {{ maxConstraint }} choices.'
94+
]
95+
), [1, 2, 3], 'The "test" value should have at most 2 choices.'
96+
];
97+
}
98+
99+
100+
}

tests/GreaterThanTest.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,32 @@ class GreaterThanTest extends AbstractTest
1818

1919
public static function provideRuleUnexpectedValueData(): \Generator
2020
{
21-
$exceptionMessage = '/Cannot compare a type "(.*)" with a type "(.*)"/';
21+
$message = '/Cannot compare a type "(.*)" with a type "(.*)"/';
2222

23-
yield 'datetime constraint with int value' => [new GreaterThan(new \DateTime()), 10, $exceptionMessage];
24-
yield 'datetime constraint with float value' => [new GreaterThan(new \DateTime()), 1.0, $exceptionMessage];
25-
yield 'datetime constraint with string value' => [new GreaterThan(new \DateTime()), 'a', $exceptionMessage];
26-
yield 'int constraint with string value' => [new GreaterThan(10), 'a', $exceptionMessage];
27-
yield 'float constraint with string value' => [new GreaterThan(1.0), 'a', $exceptionMessage];
28-
yield 'array constraint' => [new GreaterThan([10]), 10, $exceptionMessage];
29-
yield 'null constraint' => [new GreaterThan(null), 10, $exceptionMessage];
23+
yield 'datetime constraint with int value' => [new GreaterThan(new \DateTime()), 10, $message];
24+
yield 'datetime constraint with float value' => [new GreaterThan(new \DateTime()), 1.0, $message];
25+
yield 'datetime constraint with string value' => [new GreaterThan(new \DateTime()), 'a', $message];
26+
yield 'int constraint with string value' => [new GreaterThan(10), 'a', $message];
27+
yield 'float constraint with string value' => [new GreaterThan(1.0), 'a', $message];
28+
yield 'array constraint' => [new GreaterThan([10]), 10, $message];
29+
yield 'null constraint' => [new GreaterThan(null), 10, $message];
3030
}
3131

3232
public static function provideRuleFailureConditionData(): \Generator
3333
{
3434
$exception = GreaterThanException::class;
35-
$exceptionMessage = '/The "(.*)" value should be greater than "(.*)", "(.*)" given./';
35+
$message = '/The "(.*)" value should be greater than "(.*)", "(.*)" given./';
3636

37-
yield 'datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('yesterday'), $exception, $exceptionMessage];
38-
yield 'same datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('today'), $exception, $exceptionMessage];
39-
yield 'int' => [new GreaterThan(10), 1, $exception, $exceptionMessage];
40-
yield 'same int' => [new GreaterThan(10), 10, $exception, $exceptionMessage];
41-
yield 'float' => [new GreaterThan(10.0), 1.0, $exception, $exceptionMessage];
42-
yield 'same float' => [new GreaterThan(10.0), 10.0, $exception, $exceptionMessage];
43-
yield 'int with float' => [new GreaterThan(10), 1.0, $exception, $exceptionMessage];
44-
yield 'same int with float' => [new GreaterThan(10), 10.0, $exception, $exceptionMessage];
45-
yield 'string' => [new GreaterThan('z'), 'a', $exception, $exceptionMessage];
46-
yield 'same string' => [new GreaterThan('a'), 'a', $exception, $exceptionMessage];
37+
yield 'datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('yesterday'), $exception, $message];
38+
yield 'same datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('today'), $exception, $message];
39+
yield 'int' => [new GreaterThan(10), 1, $exception, $message];
40+
yield 'same int' => [new GreaterThan(10), 10, $exception, $message];
41+
yield 'float' => [new GreaterThan(10.0), 1.0, $exception, $message];
42+
yield 'same float' => [new GreaterThan(10.0), 10.0, $exception, $message];
43+
yield 'int with float' => [new GreaterThan(10), 1.0, $exception, $message];
44+
yield 'same int with float' => [new GreaterThan(10), 10.0, $exception, $message];
45+
yield 'string' => [new GreaterThan('z'), 'a', $exception, $message];
46+
yield 'same string' => [new GreaterThan('a'), 'a', $exception, $message];
4747
}
4848

4949
public static function provideRuleSuccessConditionData(): \Generator
@@ -58,11 +58,12 @@ public static function provideRuleSuccessConditionData(): \Generator
5858
public static function provideRuleMessageOptionData(): \Generator
5959
{
6060
yield 'message' => [
61-
new GreaterThan(10, [
62-
'message' => 'The "{{ name }}" value "{{ value }}" is not greater than "{{ constraint }}".'
63-
]),
64-
1,
65-
'The "test" value "1" is not greater than "10".'
61+
new GreaterThan(
62+
constraint: 10,
63+
options: [
64+
'message' => 'The "{{ name }}" value "{{ value }}" is not greater than "{{ constraint }}".'
65+
]
66+
), 1, 'The "test" value "1" is not greater than "10".'
6667
];
6768
}
6869
}

tests/LessThanTest.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,32 @@ class LessThanTest extends AbstractTest
1818

1919
public static function provideRuleUnexpectedValueData(): \Generator
2020
{
21-
$exceptionMessage = '/Cannot compare a type "(.*)" with a type "(.*)"/';
21+
$message = '/Cannot compare a type "(.*)" with a type "(.*)"/';
2222

23-
yield 'datetime constraint with int value' => [new LessThan(new \DateTime()), 10, $exceptionMessage];
24-
yield 'datetime constraint with float value' => [new LessThan(new \DateTime()), 1.0, $exceptionMessage];
25-
yield 'datetime constraint with string value' => [new LessThan(new \DateTime()), 'a', $exceptionMessage];
26-
yield 'int constraint with string value' => [new LessThan(10), 'a', $exceptionMessage];
27-
yield 'float constraint with string value' => [new LessThan(1.0), 'a', $exceptionMessage];
28-
yield 'array constraint' => [new LessThan([10]), 10, $exceptionMessage];
29-
yield 'null constraint' => [new LessThan(null), 10, $exceptionMessage];
23+
yield 'datetime constraint with int value' => [new LessThan(new \DateTime()), 10, $message];
24+
yield 'datetime constraint with float value' => [new LessThan(new \DateTime()), 1.0, $message];
25+
yield 'datetime constraint with string value' => [new LessThan(new \DateTime()), 'a', $message];
26+
yield 'int constraint with string value' => [new LessThan(10), 'a', $message];
27+
yield 'float constraint with string value' => [new LessThan(1.0), 'a', $message];
28+
yield 'array constraint' => [new LessThan([10]), 10, $message];
29+
yield 'null constraint' => [new LessThan(null), 10, $message];
3030
}
3131

3232
public static function provideRuleFailureConditionData(): \Generator
3333
{
3434
$exception = LessThanException::class;
35-
$exceptionMessage = '/The "(.*)" value should be less than "(.*)", "(.*)" given./';
35+
$message = '/The "(.*)" value should be less than "(.*)", "(.*)" given./';
3636

37-
yield 'datetime' => [new LessThan(new \DateTime('today')), new \DateTime('tomorrow'), $exception, $exceptionMessage];
38-
yield 'same datetime' => [new LessThan(new \DateTime('today')), new \DateTime('today'), $exception, $exceptionMessage];
39-
yield 'int' => [new LessThan(10), 20, $exception, $exceptionMessage];
40-
yield 'same int' => [new LessThan(10), 10, $exception, $exceptionMessage];
41-
yield 'float' => [new LessThan(10.0), 20.0, $exception, $exceptionMessage];
42-
yield 'same float' => [new LessThan(10.0), 10.0, $exception, $exceptionMessage];
43-
yield 'int with float' => [new LessThan(10), 20.0, $exception, $exceptionMessage];
44-
yield 'same int with float' => [new LessThan(10), 10.0, $exception, $exceptionMessage];
45-
yield 'string' => [new LessThan('a'), 'z', $exception, $exceptionMessage];
46-
yield 'same string' => [new LessThan('a'), 'a', $exception, $exceptionMessage];
37+
yield 'datetime' => [new LessThan(new \DateTime('today')), new \DateTime('tomorrow'), $exception, $message];
38+
yield 'same datetime' => [new LessThan(new \DateTime('today')), new \DateTime('today'), $exception, $message];
39+
yield 'int' => [new LessThan(10), 20, $exception, $message];
40+
yield 'same int' => [new LessThan(10), 10, $exception, $message];
41+
yield 'float' => [new LessThan(10.0), 20.0, $exception, $message];
42+
yield 'same float' => [new LessThan(10.0), 10.0, $exception, $message];
43+
yield 'int with float' => [new LessThan(10), 20.0, $exception, $message];
44+
yield 'same int with float' => [new LessThan(10), 10.0, $exception, $message];
45+
yield 'string' => [new LessThan('a'), 'z', $exception, $message];
46+
yield 'same string' => [new LessThan('a'), 'a', $exception, $message];
4747
}
4848

4949
public static function provideRuleSuccessConditionData(): \Generator
@@ -58,11 +58,12 @@ public static function provideRuleSuccessConditionData(): \Generator
5858
public static function provideRuleMessageOptionData(): \Generator
5959
{
6060
yield 'message' => [
61-
new LessThan(10, [
62-
'message' => 'The "{{ name }}" value "{{ value }}" is not less than "{{ constraint }}".'
63-
]),
64-
20,
65-
'The "test" value "20" is not less than "10".'
61+
new LessThan(
62+
constraint: 10,
63+
options: [
64+
'message' => 'The "{{ name }}" value "{{ value }}" is not less than "{{ constraint }}".'
65+
]
66+
), 20, 'The "test" value "20" is not less than "10".'
6667
];
6768
}
6869
}

tests/NotBlankTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ public static function provideRuleSuccessConditionData(): \Generator
4747
public static function provideRuleMessageOptionData(): \Generator
4848
{
4949
yield 'message' => [
50-
new NotBlank([
51-
'message' => 'The "{{ name }}" value "{{ value }}" is not blank.'
52-
]),
53-
'',
54-
'The "test" value "" is not blank.'
50+
new NotBlank(
51+
options: [
52+
'message' => 'The "{{ name }}" value "{{ value }}" is not blank.'
53+
]
54+
), '', 'The "test" value "" is not blank.'
5555
];
5656
}
5757
}

0 commit comments

Comments
 (0)