44
55use PHPUnit \Framework \Attributes \DataProvider ;
66use ProgrammatorDev \YetAnotherPhpValidator \Exception \GreaterThanException ;
7- use ProgrammatorDev \YetAnotherPhpValidator \Exception \Util \FormatValueTrait ;
7+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \GreaterThan ;
8+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleFailureConditionTrait ;
9+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleSuccessConditionTrait ;
810use ProgrammatorDev \YetAnotherPhpValidator \Validator ;
911
1012class GreaterThanTest extends AbstractTest
1113{
12- use FormatValueTrait;
14+ use TestRuleFailureConditionTrait;
15+ use TestRuleSuccessConditionTrait;
1316
1417 #[DataProvider('provideInvalidConditionData ' )]
1518 public function testGreaterThanValidateInvalidCondition (mixed $ constraint , mixed $ value )
@@ -52,66 +55,51 @@ public static function provideInvalidConditionData(): \Generator
5255 yield 'null constraint ' => [null , 10 ];
5356 }
5457
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-
7358 public static function provideFailureConditionData (): \Generator
7459 {
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 ' );
60+ $ 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 ];
9583 }
9684
9785 public static function provideSuccessConditionData (): \Generator
9886 {
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 ' ];
87+ yield 'datetime ' => [new GreaterThan ( new \DateTime ('today ' ) ), new \DateTime ('tomorrow ' )];
88+ yield 'int ' => [new GreaterThan ( 10 ) , 20 ];
89+ yield 'float ' => [new GreaterThan ( 10.0 ) , 20.0 ];
90+ yield 'int with float ' => [new GreaterThan ( 10 ) , 20.0 ];
91+ yield 'string ' => [new GreaterThan ( 'a ' ) , 'z ' ];
10492 }
10593
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- }
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+ // }
117105}
0 commit comments