1+ <?php
2+
3+ namespace ProgrammatorDev \YetAnotherPhpValidator \Test ;
4+
5+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \LessThanOrEqualException ;
6+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \LessThanOrEqual ;
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 LessThanOrEqualTest extends AbstractTest
13+ {
14+ use TestRuleUnexpectedValueTrait;
15+ use TestRuleFailureConditionTrait;
16+ use TestRuleSuccessConditionTrait;
17+ use TestRuleMessageOptionTrait;
18+
19+ public static function provideRuleUnexpectedValueData (): \Generator
20+ {
21+ $ message = '/Cannot compare a type "(.*)" with a type "(.*)"/ ' ;
22+
23+ yield 'datetime constraint with int value ' => [new LessThanOrEqual (new \DateTime ()), 10 , $ message ];
24+ yield 'datetime constraint with float value ' => [new LessThanOrEqual (new \DateTime ()), 1.0 , $ message ];
25+ yield 'datetime constraint with string value ' => [new LessThanOrEqual (new \DateTime ()), 'a ' , $ message ];
26+ yield 'int constraint with string value ' => [new LessThanOrEqual (10 ), 'a ' , $ message ];
27+ yield 'float constraint with string value ' => [new LessThanOrEqual (1.0 ), 'a ' , $ message ];
28+ yield 'array constraint ' => [new LessThanOrEqual ([10 ]), 10 , $ message ];
29+ yield 'null constraint ' => [new LessThanOrEqual (null ), 10 , $ message ];
30+ }
31+
32+ public static function provideRuleFailureConditionData (): \Generator
33+ {
34+ $ exception = LessThanOrEqualException::class;
35+ $ message = '/The "(.*)" value should be less than or equal to "(.*)", "(.*)" given./ ' ;
36+
37+ yield 'datetime ' => [new LessThanOrEqual (new \DateTime ('today ' )), new \DateTime ('tomorrow ' ), $ exception , $ message ];
38+ yield 'int ' => [new LessThanOrEqual (10 ), 20 , $ exception , $ message ];
39+ yield 'float ' => [new LessThanOrEqual (10.0 ), 20.0 , $ exception , $ message ];
40+ yield 'int with float ' => [new LessThanOrEqual (10 ), 20.0 , $ exception , $ message ];
41+ yield 'string ' => [new LessThanOrEqual ('a ' ), 'z ' , $ exception , $ message ];
42+ }
43+
44+ public static function provideRuleSuccessConditionData (): \Generator
45+ {
46+ yield 'datetime ' => [new LessThanOrEqual (new \DateTime ('today ' )), new \DateTime ('yesterday ' )];
47+ yield 'same datetime ' => [new LessThanOrEqual (new \DateTime ('today ' )), new \DateTime ('today ' )];
48+ yield 'int ' => [new LessThanOrEqual (10 ), 1 ];
49+ yield 'same int ' => [new LessThanOrEqual (10 ), 10 ];
50+ yield 'float ' => [new LessThanOrEqual (10.0 ), 1.0 ];
51+ yield 'same float ' => [new LessThanOrEqual (10.0 ), 10.0 ];
52+ yield 'int with float ' => [new LessThanOrEqual (10 ), 1.0 ];
53+ yield 'same int with float ' => [new LessThanOrEqual (10 ), 10.0 ];
54+ yield 'string ' => [new LessThanOrEqual ('z ' ), 'a ' ];
55+ yield 'same string ' => [new LessThanOrEqual ('a ' ), 'a ' ];
56+ }
57+
58+ public static function provideRuleMessageOptionData (): \Generator
59+ {
60+ yield 'message ' => [
61+ new LessThanOrEqual (
62+ constraint: 10 ,
63+ options: [
64+ 'message ' => 'The "{{ name }}" value "{{ value }}" is not less than or equal to "{{ constraint }}". '
65+ ]
66+ ), 20 , 'The "test" value "20" is not less than or equal to "10". '
67+ ];
68+ }
69+ }
0 commit comments