1+ <?php
2+
3+ namespace ProgrammatorDev \YetAnotherPhpValidator \Test ;
4+
5+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \LessThanException ;
6+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \LessThan ;
7+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleCustomMessageTrait ;
8+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleFailureConditionTrait ;
9+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleSuccessConditionTrait ;
10+
11+ class LessThanTest extends AbstractTest
12+ {
13+ use TestRuleFailureConditionTrait;
14+ use TestRuleSuccessConditionTrait;
15+ use TestRuleCustomMessageTrait;
16+
17+ public static function provideRuleFailureConditionData (): \Generator
18+ {
19+ $ exception = LessThanException::class;
20+ $ exceptionMessageInvalid = '/Cannot compare a type "(.*)" with a type "(.*)"/ ' ;
21+ $ exceptionMessageFailure = '/The "(.*)" value should be less than "(.*)", "(.*)" given./ ' ;
22+
23+ yield 'datetime constraint with int value ' => [new LessThan (new \DateTime ()), 10 , $ exception , $ exceptionMessageInvalid ];
24+ yield 'datetime constraint with float value ' => [new LessThan (new \DateTime ()), 1.0 , $ exception , $ exceptionMessageInvalid ];
25+ yield 'datetime constraint with string value ' => [new LessThan (new \DateTime ()), 'a ' , $ exception , $ exceptionMessageInvalid ];
26+ yield 'int constraint with string value ' => [new LessThan (10 ), 'a ' , $ exception , $ exceptionMessageInvalid ];
27+ yield 'float constraint with string value ' => [new LessThan (1.0 ), 'a ' , $ exception , $ exceptionMessageInvalid ];
28+ yield 'array constraint ' => [new LessThan ([10 ]), 10 , $ exception , $ exceptionMessageInvalid ];
29+ yield 'null constraint ' => [new LessThan (null ), 10 , $ exception , $ exceptionMessageInvalid ];
30+
31+ yield 'datetime ' => [new LessThan (new \DateTime ('today ' )), new \DateTime ('tomorrow ' ), $ exception , $ exceptionMessageFailure ];
32+ yield 'same datetime ' => [new LessThan (new \DateTime ('today ' )), new \DateTime ('today ' ), $ exception , $ exceptionMessageFailure ];
33+ yield 'int ' => [new LessThan (10 ), 20 , $ exception , $ exceptionMessageFailure ];
34+ yield 'same int ' => [new LessThan (10 ), 10 , $ exception , $ exceptionMessageFailure ];
35+ yield 'float ' => [new LessThan (10.0 ), 20.0 , $ exception , $ exceptionMessageFailure ];
36+ yield 'same float ' => [new LessThan (10.0 ), 10.0 , $ exception , $ exceptionMessageFailure ];
37+ yield 'int with float ' => [new LessThan (10 ), 20.0 , $ exception , $ exceptionMessageFailure ];
38+ yield 'same int with float ' => [new LessThan (10 ), 10.0 , $ exception , $ exceptionMessageFailure ];
39+ yield 'string ' => [new LessThan ('a ' ), 'z ' , $ exception , $ exceptionMessageFailure ];
40+ yield 'same string ' => [new LessThan ('a ' ), 'a ' , $ exception , $ exceptionMessageFailure ];
41+ }
42+
43+ public static function provideRuleSuccessConditionData (): \Generator
44+ {
45+ yield 'datetime ' => [new LessThan (new \DateTime ('today ' )), new \DateTime ('yesterday ' )];
46+ yield 'int ' => [new LessThan (10 ), 1 ];
47+ yield 'float ' => [new LessThan (10.0 ), 1.0 ];
48+ yield 'int with float ' => [new LessThan (10 ), 1.0 ];
49+ yield 'string ' => [new LessThan ('z ' ), 'a ' ];
50+ }
51+
52+ public static function provideRuleCustomMessageData (): \Generator
53+ {
54+ yield 'message ' => [
55+ new LessThan (10 , [
56+ 'message ' => 'The "{{ name }}" value "{{ value }}" is not less than "{{ constraint }}". '
57+ ]),
58+ 20 ,
59+ 'The "test" value "20" is not less than "10". '
60+ ];
61+ }
62+ }
0 commit comments