1+ <?php
2+
3+ namespace ProgrammatorDev \YetAnotherPhpValidator \Test ;
4+
5+ use PHPUnit \Framework \Attributes \DataProvider ;
6+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \GreaterThanException ;
7+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \Util \FormatValueTrait ;
8+ use ProgrammatorDev \YetAnotherPhpValidator \Validator ;
9+
10+ class GreaterThanTest extends AbstractTest
11+ {
12+ use FormatValueTrait;
13+
14+ #[DataProvider('provideInvalidConditionData ' )]
15+ public function testGreaterThanValidateInvalidCondition (mixed $ constraint , mixed $ value )
16+ {
17+ $ this ->expectException (\LogicException::class);
18+ $ this ->expectExceptionMessage (
19+ \sprintf (
20+ 'Cannot compare a constraint type "%s" with a value type "%s" ' ,
21+ get_debug_type ($ constraint ),
22+ get_debug_type ($ value )
23+ )
24+ );
25+
26+ Validator::greaterThan ($ constraint )->validate ($ value );
27+ }
28+
29+ #[DataProvider('provideInvalidConditionData ' )]
30+ public function testGreaterThanAssertInvalidCondition (mixed $ constraint , mixed $ value )
31+ {
32+ $ this ->expectException (\LogicException::class);
33+ $ this ->expectExceptionMessage (
34+ \sprintf (
35+ 'Cannot compare a constraint type "%s" with a value type "%s" ' ,
36+ get_debug_type ($ constraint ),
37+ get_debug_type ($ value )
38+ )
39+ );
40+
41+ Validator::greaterThan ($ constraint )->assert ($ value , 'test ' );
42+ }
43+
44+ public static function provideInvalidConditionData (): \Generator
45+ {
46+ yield 'datetime constraint with int value ' => [new \DateTime (), 10 ];
47+ yield 'datetime constraint with float value ' => [new \DateTime (), 1.0 ];
48+ yield 'datetime constraint with string value ' => [new \DateTime (), 'a ' ];
49+ yield 'int constraint with string value ' => [10 , 'a ' ];
50+ yield 'float constraint with string value ' => [1.0 , 'a ' ];
51+ yield 'array constraint ' => [[10 ], 10 ];
52+ yield 'null constraint ' => [null , 10 ];
53+ }
54+
55+ #[DataProvider('provideFailureConditionData ' )]
56+ public function testGreaterThanFailureCondition (mixed $ constraint , mixed $ value )
57+ {
58+ $ validator = Validator::greaterThan ($ constraint );
59+
60+ $ this ->assertFalse ($ validator ->validate ($ value ));
61+
62+ $ this ->expectException (GreaterThanException::class);
63+ $ this ->expectExceptionMessage (
64+ \sprintf (
65+ 'The "test" value should be greater than "%s", "%s" given. ' ,
66+ $ this ->formatValue ($ constraint ),
67+ $ this ->formatValue ($ value )
68+ )
69+ );
70+ $ validator ->assert ($ value , 'test ' );
71+ }
72+
73+ public static function provideFailureConditionData (): \Generator
74+ {
75+ yield 'datetime ' => [new \DateTime ('today ' ), new \DateTime ('yesterday ' )];
76+ yield 'same datetime ' => [new \DateTime ('2000-01-01 00:00:00 ' ), new \DateTime ('2000-01-01 00:00:00 ' )];
77+ yield 'int ' => [10 , 1 ];
78+ yield 'same int ' => [10 , 10 ];
79+ yield 'float ' => [10.0 , 1.0 ];
80+ yield 'same float ' => [10.0 , 10.0 ];
81+ yield 'int with float ' => [10 , 1.0 ];
82+ yield 'same int with float ' => [10 , 10.0 ];
83+ yield 'string ' => ['z ' , 'a ' ];
84+ yield 'same string ' => ['a ' , 'a ' ];
85+ }
86+
87+ #[DataProvider('provideSuccessConditionData ' )]
88+ public function testGreaterThanSuccessCondition (mixed $ constraint , mixed $ value )
89+ {
90+ $ validator = Validator::greaterThan ($ constraint );
91+
92+ $ this ->assertTrue ($ validator ->validate ($ value ));
93+
94+ $ validator ->assert ($ value , 'test ' );
95+ }
96+
97+ public static function provideSuccessConditionData (): \Generator
98+ {
99+ yield 'datetime ' => [new \DateTime ('today ' ), new \DateTime ('tomorrow ' )];
100+ yield 'int ' => [10 , 20 ];
101+ yield 'float ' => [10.0 , 20.0 ];
102+ yield 'int with float ' => [10 , 20.0 ];
103+ yield 'string ' => ['a ' , 'z ' ];
104+ }
105+
106+ public function testGreaterThanMessageArgument ()
107+ {
108+ $ this ->expectExceptionMessage ('The "test" value "1" is invalid. Must not be greater than "10". ' );
109+
110+ Validator
111+ ::greaterThan (
112+ constraint: 10 ,
113+ message: 'The "{{ name }}" value "{{ value }}" is invalid. Must not be greater than "{{ constraint }}". '
114+ )
115+ ->assert (1 , 'test ' );
116+ }
117+ }
0 commit comments