1+ <?php
2+
3+ namespace ProgrammatorDev \YetAnotherPhpValidator \Test ;
4+
5+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \UrlException ;
6+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \Url ;
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 UrlTest extends AbstractTest
13+ {
14+ use TestRuleUnexpectedValueTrait;
15+ use TestRuleFailureConditionTrait;
16+ use TestRuleSuccessConditionTrait;
17+ use TestRuleMessageOptionTrait;
18+
19+ public static function provideRuleUnexpectedValueData (): \Generator
20+ {
21+ $ typeMessage = '/Expected value of type "string", "(.*)" given./ ' ;
22+
23+ yield 'invalid type ' => [new Url (), 1 , $ typeMessage ];
24+ }
25+
26+ public static function provideRuleFailureConditionData (): \Generator
27+ {
28+ $ exception = UrlException::class;
29+ $ message = '/The (.*) value is not a valid URL address, (.*) given./ ' ;
30+
31+ yield 'invalid url ' => [new URL (), 'invalid ' , $ exception , $ message ];
32+ yield 'unallowed protocol ' => [new URL (protocols: ['https ' ]), 'http://test.com ' , $ exception , $ message ];
33+ yield 'unallowed relative protocol ' => [new URL (), '//test.com ' , $ exception , $ message ];
34+ }
35+
36+ public static function provideRuleSuccessConditionData (): \Generator
37+ {
38+ yield 'domain ' => [new URL (), 'https://test.com ' ];
39+ yield 'multi-level domain ' => [new URL (), 'https://multi.level.url.test.com ' ];
40+ yield 'chars ' => [new URL (), 'https://テスト.com ' ];
41+ yield 'punycode ' => [new URL (), 'https://xn--zckzah.com ' ];
42+ yield 'port ' => [new URL (), 'https://test.com:8000 ' ];
43+ yield 'path ' => [new URL (), 'https://test.com/path ' ];
44+ yield 'query ' => [new URL (), 'https://test.com?test=1 ' ];
45+ yield 'fragment ' => [new URL (), 'https://test.com#test ' ];
46+ yield 'ipv4 ' => [new URL (), 'https://127.0.0.1 ' ];
47+ yield 'ipv6 ' => [new URL (), 'https://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff] ' ];
48+ yield 'basic auth ' => [new URL (), 'https://username:password@test.com ' ];
49+ yield 'full domain ' => [new URL (), 'https://username:password@test.com:8000/path?test=1#test ' ];
50+ yield 'full ipv4 ' => [new URL (), 'https://username:password@127.0.0.1:8000/path?test=1#test ' ];
51+ yield 'full ipv6 ' => [new URL (), 'https://username:password@[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:8000/path?test=1#test ' ];
52+ yield 'custom protocol ' => [new URL (protocols: ['ftp ' ]), 'ftp://test.com ' ];
53+ yield 'allow relative protocol with protocol ' => [new URL (allowRelativeProtocol: true ), 'https://test.com ' ];
54+ yield 'allow relative protocol without protocol ' => [new URL (allowRelativeProtocol: true ), '//test.com ' ];
55+ yield 'allow relative protocol only ' => [new URL (protocols: [], allowRelativeProtocol: true ), '//test.com ' ];
56+ yield 'normalizer ' => [new URL (normalizer: 'trim ' ), 'https://test.com ' ];
57+ }
58+
59+ public static function provideRuleMessageOptionData (): \Generator
60+ {
61+ yield 'message ' => [
62+ new Url (
63+ message: 'The {{ name }} value {{ value }} is not a valid URL address. Allowed protocols: {{ protocols }}. '
64+ ),
65+ 'invalid ' ,
66+ 'The test value "invalid" is not a valid URL address. Allowed protocols: ["http", "https"]. '
67+ ];
68+ }
69+ }
0 commit comments