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