22
33namespace ProgrammatorDev \YetAnotherPhpValidator \Test ;
44
5- use PHPUnit \Framework \Attributes \DataProvider ;
65use ProgrammatorDev \YetAnotherPhpValidator \Exception \GreaterThanException ;
76use ProgrammatorDev \YetAnotherPhpValidator \Rule \GreaterThan ;
87use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleFailureConditionTrait ;
98use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleSuccessConditionTrait ;
10- use ProgrammatorDev \YetAnotherPhpValidator \Validator ;
119
1210class GreaterThanTest extends AbstractTest
1311{
1412 use TestRuleFailureConditionTrait;
1513 use TestRuleSuccessConditionTrait;
1614
17- #[DataProvider('provideInvalidConditionData ' )]
18- public function testGreaterThanValidateInvalidCondition (mixed $ constraint , mixed $ value )
19- {
20- $ this ->expectException (\LogicException::class);
21- $ this ->expectExceptionMessage (
22- \sprintf (
23- 'Cannot compare a constraint type "%s" with a value type "%s" ' ,
24- get_debug_type ($ constraint ),
25- get_debug_type ($ value )
26- )
27- );
28-
29- Validator::greaterThan ($ constraint )->validate ($ value );
30- }
31-
32- #[DataProvider('provideInvalidConditionData ' )]
33- public function testGreaterThanAssertInvalidCondition (mixed $ constraint , mixed $ value )
34- {
35- $ this ->expectException (\LogicException::class);
36- $ this ->expectExceptionMessage (
37- \sprintf (
38- 'Cannot compare a constraint type "%s" with a value type "%s" ' ,
39- get_debug_type ($ constraint ),
40- get_debug_type ($ value )
41- )
42- );
43-
44- Validator::greaterThan ($ constraint )->assert ($ value , 'test ' );
45- }
46-
47- public static function provideInvalidConditionData (): \Generator
48- {
49- yield 'datetime constraint with int value ' => [new \DateTime (), 10 ];
50- yield 'datetime constraint with float value ' => [new \DateTime (), 1.0 ];
51- yield 'datetime constraint with string value ' => [new \DateTime (), 'a ' ];
52- yield 'int constraint with string value ' => [10 , 'a ' ];
53- yield 'float constraint with string value ' => [1.0 , 'a ' ];
54- yield 'array constraint ' => [[10 ], 10 ];
55- yield 'null constraint ' => [null , 10 ];
56- }
57-
5815 public static function provideFailureConditionData (): \Generator
5916 {
6017 $ exception = GreaterThanException::class;
61- $ exceptionMessage = '/The "(.*)" value should be greater than "(.*)", "(.*)" given./ ' ;
62-
63- yield 'datetime ' => [
64- new GreaterThan (new \DateTime ('today ' )),
65- new \DateTime ('yesterday ' ),
66- $ exception ,
67- $ exceptionMessage
68- ];
69- yield 'same datetime ' => [
70- new GreaterThan (new \DateTime ('2000-01-01 ' )),
71- new \DateTime ('2000-01-01 ' ),
72- $ exception ,
73- $ exceptionMessage
74- ];
75- yield 'int ' => [new GreaterThan (10 ), 1 , $ exception , $ exceptionMessage ];
76- yield 'same int ' => [new GreaterThan (10 ), 10 , $ exception , $ exceptionMessage ];
77- yield 'float ' => [new GreaterThan (10.0 ), 1.0 , $ exception , $ exceptionMessage ];
78- yield 'same float ' => [new GreaterThan (10.0 ), 10.0 , $ exception , $ exceptionMessage ];
79- yield 'int with float ' => [new GreaterThan (10 ), 1.0 , $ exception , $ exceptionMessage ];
80- yield 'same int with float ' => [new GreaterThan (10 ), 10.0 , $ exception , $ exceptionMessage ];
81- yield 'string ' => [new GreaterThan ('z ' ), 'a ' , $ exception , $ exceptionMessage ];
82- yield 'same string ' => [new GreaterThan ('a ' ), 'a ' , $ exception , $ exceptionMessage ];
18+ $ exceptionMessageInvalid = '/Cannot compare a type "(.*)" with a type "(.*)"/ ' ;
19+ $ exceptionMessageFailure = '/The "(.*)" value should be greater than "(.*)", "(.*)" given./ ' ;
20+
21+ yield 'datetime constraint with int value ' => [new GreaterThan (new \DateTime ()), 10 , $ exception , $ exceptionMessageInvalid ];
22+ yield 'datetime constraint with float value ' => [new GreaterThan (new \DateTime ()), 1.0 , $ exception , $ exceptionMessageInvalid ];
23+ yield 'datetime constraint with string value ' => [new GreaterThan (new \DateTime ()), 'a ' , $ exception , $ exceptionMessageInvalid ];
24+ yield 'int constraint with string value ' => [new GreaterThan (10 ), 'a ' , $ exception , $ exceptionMessageInvalid ];
25+ yield 'float constraint with string value ' => [new GreaterThan (1.0 ), 'a ' , $ exception , $ exceptionMessageInvalid ];
26+ yield 'array constraint ' => [new GreaterThan ([10 ]), 10 , $ exception , $ exceptionMessageInvalid ];
27+ yield 'null constraint ' => [new GreaterThan (null ), 10 , $ exception , $ exceptionMessageInvalid ];
28+
29+ yield 'datetime ' => [new GreaterThan (new \DateTime ('today ' )), new \DateTime ('yesterday ' ), $ exception , $ exceptionMessageFailure ];
30+ yield 'same datetime ' => [new GreaterThan (new \DateTime ('2000-01-01 ' )), new \DateTime ('2000-01-01 ' ), $ exception , $ exceptionMessageFailure ];
31+ yield 'int ' => [new GreaterThan (10 ), 1 , $ exception , $ exceptionMessageFailure ];
32+ yield 'same int ' => [new GreaterThan (10 ), 10 , $ exception , $ exceptionMessageFailure ];
33+ yield 'float ' => [new GreaterThan (10.0 ), 1.0 , $ exception , $ exceptionMessageFailure ];
34+ yield 'same float ' => [new GreaterThan (10.0 ), 10.0 , $ exception , $ exceptionMessageFailure ];
35+ yield 'int with float ' => [new GreaterThan (10 ), 1.0 , $ exception , $ exceptionMessageFailure ];
36+ yield 'same int with float ' => [new GreaterThan (10 ), 10.0 , $ exception , $ exceptionMessageFailure ];
37+ yield 'string ' => [new GreaterThan ('z ' ), 'a ' , $ exception , $ exceptionMessageFailure ];
38+ yield 'same string ' => [new GreaterThan ('a ' ), 'a ' , $ exception , $ exceptionMessageFailure ];
8339 }
8440
8541 public static function provideSuccessConditionData (): \Generator
@@ -90,16 +46,4 @@ public static function provideSuccessConditionData(): \Generator
9046 yield 'int with float ' => [new GreaterThan (10 ), 20.0 ];
9147 yield 'string ' => [new GreaterThan ('a ' ), 'z ' ];
9248 }
93-
94- // public function testGreaterThanMessageArgument()
95- // {
96- // $this->expectExceptionMessage('The "test" value "1" is invalid. Must not be greater than "10".');
97- //
98- // Validator
99- // ::greaterThan(
100- // constraint: 10,
101- // message: 'The "{{ name }}" value "{{ value }}" is invalid. Must not be greater than "{{ constraint }}".'
102- // )
103- // ->assert(1, 'test');
104- // }
10549}
0 commit comments