1+ <?php
2+
3+ namespace ProgrammatorDev \YetAnotherPhpValidator \Test ;
4+
5+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \RangeException ;
6+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \Range ;
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 RangeTest extends AbstractTest
13+ {
14+ use TestRuleUnexpectedValueTrait;
15+ use TestRuleFailureConditionTrait;
16+ use TestRuleSuccessConditionTrait;
17+ use TestRuleMessageOptionTrait;
18+
19+ public static function provideRuleUnexpectedValueData (): \Generator
20+ {
21+ $ comparableMessage = '/Cannot compare a type "(.*)" with a type "(.*)"/ ' ;
22+ $ constraintMessage = '/Max constraint value must be greater than min constraint value./ ' ;
23+
24+ yield 'datetime constraint with int constraint ' => [new Range (new \DateTime (), 10 ), new \DateTime (), $ comparableMessage ];
25+ yield 'datetime constraint with float constraint ' => [new Range (new \DateTime (), 10.0 ), new \DateTime (), $ comparableMessage ];
26+ yield 'datetime constraint with string constraint ' => [new Range (new \DateTime (), 'a ' ), new \DateTime (), $ comparableMessage ];
27+ yield 'int constraint with string constraint ' => [new Range (10 , 'a ' ), 10 , $ comparableMessage ];
28+ yield 'float constraint with string constraint ' => [new Range (1.0 , 'a ' ), 1.0 , $ comparableMessage ];
29+ yield 'array constraint ' => [new Range ([10 ], 10 ), 10 , $ comparableMessage ];
30+ yield 'null constraint ' => [new Range (null , 10 ), 10 , $ comparableMessage ];
31+
32+ yield 'min greater than max constraint ' => [new Range (10 , 9 ), 10 , $ constraintMessage ];
33+ yield 'same min and max constraint ' => [new Range (10 , 10 ), 10 , $ constraintMessage ];
34+ }
35+
36+ public static function provideRuleFailureConditionData (): \Generator
37+ {
38+ $ exception = RangeException::class;
39+ $ message = '/The "(.*)" value should be between "(.*)" and "(.*)", "(.*)" given./ ' ;
40+
41+ yield 'min datetime ' => [new Range (new \DateTime ('today ' ), new \DateTime ('tomorrow ' )), new \DateTime ('yesterday ' ), $ exception , $ message ];
42+ yield 'min int ' => [new Range (10 , 20 ), 1 , $ exception , $ message ];
43+ yield 'min float ' => [new Range (10.0 , 20.0 ), 1.0 , $ exception , $ message ];
44+ yield 'min int with float ' => [new Range (10 , 20 ), 1.0 , $ exception , $ message ];
45+ yield 'min string ' => [new Range ('b ' , 'z ' ), 'a ' , $ exception , $ message ];
46+ yield 'max datetime ' => [new Range (new \DateTime ('today ' ), new \DateTime ('tomorrow ' )), new \DateTime ('+2 days ' ), $ exception , $ message ];
47+ yield 'max int ' => [new Range (10 , 20 ), 30 , $ exception , $ message ];
48+ yield 'max float ' => [new Range (10.0 , 20.0 ), 30.0 , $ exception , $ message ];
49+ yield 'max int with float ' => [new Range (10 , 20 ), 30.0 , $ exception , $ message ];
50+ yield 'max string ' => [new Range ('a ' , 'y ' ), 'z ' , $ exception , $ message ];
51+ }
52+
53+ public static function provideRuleSuccessConditionData (): \Generator
54+ {
55+ yield 'datetime ' => [new Range (new \DateTime ('today ' ), new \DateTime ('tomorrow ' )), new \DateTime ('+1 hour ' )];
56+ yield 'int ' => [new Range (10 , 20 ), 15 ];
57+ yield 'float ' => [new Range (10.0 , 20.0 ), 15.0 ];
58+ yield 'int with float ' => [new Range (10 , 20 ), 15.0 ];
59+ yield 'string ' => [new Range ('a ' , 'z ' ), 'b ' ];
60+ yield 'min datetime ' => [new Range (new \DateTime ('today ' ), new \DateTime ('tomorrow ' )), new \DateTime ('today ' )];
61+ yield 'min int ' => [new Range (10 , 20 ), 10 ];
62+ yield 'min float ' => [new Range (10.0 , 20.0 ), 10.0 ];
63+ yield 'min int with float ' => [new Range (10 , 20 ), 10.0 ];
64+ yield 'min string ' => [new Range ('a ' , 'z ' ), 'a ' ];
65+ yield 'max datetime ' => [new Range (new \DateTime ('today ' ), new \DateTime ('tomorrow ' )), new \DateTime ('tomorrow ' )];
66+ yield 'max int ' => [new Range (10 , 20 ), 20 ];
67+ yield 'max float ' => [new Range (10.0 , 20.0 ), 20.0 ];
68+ yield 'max int with float ' => [new Range (10 , 20 ), 20.0 ];
69+ yield 'max string ' => [new Range ('a ' , 'z ' ), 'z ' ];
70+ }
71+
72+ public static function provideRuleMessageOptionData (): \Generator
73+ {
74+ yield 'message ' => [
75+ new Range (
76+ minConstraint: 10 ,
77+ maxConstraint: 20 ,
78+ options: [
79+ 'message ' => 'The "{{ name }}" value "{{ value }}" should be between "{{ minConstraint }}" and "{{ maxConstraint }}". '
80+ ]
81+ ), 30 , 'The "test" value "30" should be between "10" and "20". '
82+ ];
83+ }
84+ }
0 commit comments