1+ <?php
2+
3+ namespace ProgrammatorDev \Validator \Test ;
4+
5+ use ProgrammatorDev \Validator \Exception \DateTimeException ;
6+ use ProgrammatorDev \Validator \Rule \DateTime ;
7+ use ProgrammatorDev \Validator \Test \Util \TestRuleFailureConditionTrait ;
8+ use ProgrammatorDev \Validator \Test \Util \TestRuleMessageOptionTrait ;
9+ use ProgrammatorDev \Validator \Test \Util \TestRuleSuccessConditionTrait ;
10+ use ProgrammatorDev \Validator \Test \Util \TestRuleUnexpectedValueTrait ;
11+
12+ class DateTimeTest extends AbstractTest
13+ {
14+ use TestRuleUnexpectedValueTrait;
15+ use TestRuleFailureConditionTrait;
16+ use TestRuleSuccessConditionTrait;
17+ use TestRuleMessageOptionTrait;
18+
19+ public static function provideRuleUnexpectedValueData (): \Generator
20+ {
21+ $ unexpectedTypeMessage = '/Expected value of type "string|\Stringable", "(.*)" given./ ' ;
22+
23+ yield 'invalid value type ' => [new DateTime (), ['2024-01-01 00:00:00 ' ], $ unexpectedTypeMessage ];
24+ }
25+
26+ public static function provideRuleFailureConditionData (): \Generator
27+ {
28+ $ exception = DateTimeException::class;
29+ $ message = '/The (.*) value is not a valid datetime./ ' ;
30+
31+ yield 'invalid format ' => [new DateTime (format: 'invalid ' ), '2024-01-01 00:00:00 ' , $ exception , $ message ];
32+ yield 'invalid datetime ' => [new DateTime (), '2024-01-01 ' , $ exception , $ message ];
33+ yield 'invalid overflow date ' => [new DateTime (format: 'Y-m-d ' ), '2024-01-35 ' , $ exception , $ message ];
34+ yield 'invalid overflow time ' => [new DateTime (format: 'H:i:s ' ), '35:00:00 ' , $ exception , $ message ];
35+ }
36+
37+ public static function provideRuleSuccessConditionData (): \Generator
38+ {
39+ yield 'datetime ' => [new DateTime (), '2024-01-01 00:00:00 ' ];
40+ yield 'date ' => [new DateTime (format: 'Y-m-d ' ), '2024-01-01 ' ];
41+ yield 'time ' => [new DateTime (format: 'H:i:s ' ), '21:00:00 ' ];
42+ }
43+
44+ public static function provideRuleMessageOptionData (): \Generator
45+ {
46+ yield 'message ' => [
47+ new DateTime (
48+ message: 'The {{ name }} datetime does not match the format {{ format }}. '
49+ ),
50+ '2024-01-01 ' ,
51+ 'The test datetime does not match the format "Y-m-d H:i:s". '
52+ ];
53+ }
54+ }
0 commit comments