1+ <?php
2+
3+ namespace ProgrammatorDev \Validator \Test ;
4+
5+ use ProgrammatorDev \Validator \Exception \RegexException ;
6+ use ProgrammatorDev \Validator \Rule \Regex ;
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 RegexTest extends AbstractTest
13+ {
14+ use TestRuleUnexpectedValueTrait;
15+ use TestRuleFailureConditionTrait;
16+ use TestRuleSuccessConditionTrait;
17+ use TestRuleMessageOptionTrait;
18+
19+ public static function provideRuleUnexpectedValueData (): \Generator
20+ {
21+ $ unexpectedPatternMessage = '/Invalid regular expression pattern./ ' ;
22+ $ unexpectedTypeMessage = '/Expected value of type "array|\Stringable", "(.*)" given./ ' ;
23+
24+ yield 'invalid pattern ' => [new Regex ('invalid ' ), 'abc ' , $ unexpectedPatternMessage ];
25+ yield 'invalid value type ' => [new Regex ('/[a-z]/ ' ), ['abc ' ], $ unexpectedTypeMessage ];
26+ }
27+
28+ public static function provideRuleFailureConditionData (): \Generator
29+ {
30+ $ value = 'abc ' ;
31+ $ exception = RegexException::class;
32+ $ message = '/The (.*) value is not valid./ ' ;
33+
34+ yield 'match true ' => [new Regex ('/[0-9]/ ' ), $ value , $ exception , $ message ];
35+ yield 'match false ' => [new Regex ('/[a-z]/ ' , match: false ), $ value , $ exception , $ message ];
36+ }
37+
38+ public static function provideRuleSuccessConditionData (): \Generator
39+ {
40+ $ value = 'abc ' ;
41+
42+ yield 'match true ' => [new Regex ('/[a-z]/ ' ), $ value ];
43+ yield 'match false ' => [new Regex ('/[0-9]/ ' , match: false ), $ value ];
44+ yield 'normalizer ' => [new Regex ('/^\S*$/ ' , normalizer: 'trim ' ), 'abc ' ];
45+ }
46+
47+ public static function provideRuleMessageOptionData (): \Generator
48+ {
49+ yield 'message ' => [
50+ new Regex (
51+ pattern: '/[a-z]/ ' ,
52+ message: 'The {{ name }} value does not match the pattern {{ pattern }}. '
53+ ),
54+ '123 ' ,
55+ 'The test value does not match the pattern "/[a-z]/". '
56+ ];
57+ }
58+ }
0 commit comments