1+ <?php
2+
3+ namespace ProgrammatorDev \YetAnotherPhpValidator \Test ;
4+
5+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \CountryException ;
6+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \Country ;
7+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleFailureConditionTrait ;
8+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleMessageOptionTrait ;
9+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleSuccessConditionTrait ;
10+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleUnexpectedValueTrait ;
11+
12+ class CountryTest extends AbstractTest
13+ {
14+ use TestRuleUnexpectedValueTrait;
15+ use TestRuleFailureConditionTrait;
16+ use TestRuleSuccessConditionTrait;
17+ use TestRuleMessageOptionTrait;
18+
19+ public static function provideRuleUnexpectedValueData (): \Generator
20+ {
21+ $ codeMessage = '/Invalid code "(.*)". Accepted values are: "(.*)"./ ' ;
22+ $ typeMessage = '/Expected value of type "string", "(.*)" given./ ' ;
23+
24+ yield 'invalid code ' => [new Country ('invalid ' ), 'PT ' , $ codeMessage ];
25+ yield 'invalid type ' => [new Country (), 123 , $ typeMessage ];
26+ }
27+
28+ public static function provideRuleFailureConditionData (): \Generator
29+ {
30+ $ exception = CountryException::class;
31+ $ message = '/The "(.*)" value is not a valid "(.*)" country code, "(.*)" given./ ' ;
32+
33+ yield 'default ' => [new Country (), 'PRT ' , $ exception , $ message ];
34+ yield 'alpha2 ' => [new Country (code: 'alpha2 ' ), 'PRT ' , $ exception , $ message ];
35+ yield 'alpha3 ' => [new Country (code: 'alpha3 ' ), 'PT ' , $ exception , $ message ];
36+ }
37+
38+ public static function provideRuleSuccessConditionData (): \Generator
39+ {
40+ yield 'default ' => [new Country (), 'PT ' ];
41+ yield 'alpha2 ' => [new Country (code: 'alpha2 ' ), 'PT ' ];
42+ yield 'alpha2 lowercase ' => [new Country (code: 'alpha2 ' ), 'pt ' ];
43+ yield 'alpha3 ' => [new Country (code: 'alpha3 ' ), 'PRT ' ];
44+ yield 'alpha3 lowercase ' => [new Country (code: 'alpha3 ' ), 'prt ' ];
45+ }
46+
47+ public static function provideRuleMessageOptionData (): \Generator
48+ {
49+ yield 'message ' => [
50+ new Country (
51+ message: 'The "{{ name }}" value "{{ value }}" is not a valid "{{ code }}" country code. '
52+ ),
53+ 'invalid ' ,
54+ 'The "test" value "invalid" is not a valid "alpha2" country code. '
55+ ];
56+ }
57+ }
0 commit comments