Passed
Pull Request — master (#418)
by Alexander
18:49 queued 16:20
created

UrlTest::testEnableIdnWithMissingIntlExtension()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use stdClass;
8
use Yiisoft\Validator\Rule\Url;
9
use Yiisoft\Validator\Rule\UrlHandler;
10
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
11
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
12
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
13
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
14
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
15
16
final class UrlTest extends RuleTestCase
17
{
18
    use DifferentRuleInHandlerTestTrait;
19
    use RuleWithOptionsTestTrait;
20
    use SkipOnErrorTestTrait;
21
    use WhenTestTrait;
22
23
    public function testDefaultValues(): void
24
    {
25
        $rule = new Url();
26
27
        $this->assertSame('url', $rule->getName());
28
        $this->assertSame(['http', 'https'], $rule->getValidSchemes());
29
    }
30
31
    public function testGetValidSchemes(): void
32
    {
33
        $rule = new Url(validSchemes: ['http', 'https', 'ftp', 'ftps']);
34
        $this->assertSame(['http', 'https', 'ftp', 'ftps'], $rule->getValidSchemes());
35
    }
36
37
    public function dataOptions(): array
38
    {
39
        return [
40
            [
41
                new Url(),
42
                [
43
                    'pattern' => '/^((?i)http|https):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
44
                    'validSchemes' => ['http', 'https'],
45
                    'enableIDN' => false,
46
                    'incorrectInputMessage' => [
47
                        'template' => 'The value must have a string type.',
48
                        'parameters' => [],
49
                    ],
50
                    'message' => [
51
                        'template' => 'This value is not a valid URL.',
52
                        'parameters' => [],
53
                    ],
54
                    'skipOnEmpty' => false,
55
                    'skipOnError' => false,
56
                ],
57
            ],
58
            [
59
                new Url(enableIDN: true),
60
                [
61
                    'pattern' => '/^((?i)http|https):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
62
                    'validSchemes' => ['http', 'https'],
63
                    'enableIDN' => true,
64
                    'incorrectInputMessage' => [
65
                        'template' => 'The value must have a string type.',
66
                        'parameters' => [],
67
                    ],
68
                    'message' => [
69
                        'template' => 'This value is not a valid URL.',
70
                        'parameters' => [],
71
                    ],
72
                    'skipOnEmpty' => false,
73
                    'skipOnError' => false,
74
                ],
75
            ],
76
            [
77
                new Url(validSchemes: ['http']),
78
                [
79
                    'pattern' => '/^((?i)http):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
80
                    'validSchemes' => ['http'],
81
                    'enableIDN' => false,
82
                    'incorrectInputMessage' => [
83
                        'template' => 'The value must have a string type.',
84
                        'parameters' => [],
85
                    ],
86
                    'message' => [
87
                        'template' => 'This value is not a valid URL.',
88
                        'parameters' => [],
89
                    ],
90
                    'skipOnEmpty' => false,
91
                    'skipOnError' => false,
92
                ],
93
            ],
94
            [
95
                new Url(pattern: '/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+).*$/', enableIDN: true),
96
                [
97
                    'pattern' => '/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+).*$/',
98
                    'validSchemes' => ['http', 'https'],
99
                    'enableIDN' => true,
100
                    'incorrectInputMessage' => [
101
                        'template' => 'The value must have a string type.',
102
                        'parameters' => [],
103
                    ],
104
                    'message' => [
105
                        'template' => 'This value is not a valid URL.',
106
                        'parameters' => [],
107
                    ],
108
                    'skipOnEmpty' => false,
109
                    'skipOnError' => false,
110
                ],
111
            ],
112
        ];
113
    }
114
115
    public function dataValidationPassed(): array
116
    {
117
        return [
118
            ['http://google.de', [new Url()]],
119
            ['https://google.de', [new Url()]],
120
            [
121
                'https://www.google.de/search?q=yii+framework&ie=utf-8&oe=utf-8&rls=org.mozilla:de:official&client=firefox-a&gws_rd=cr',
122
                [new Url()],
123
            ],
124
            ['http://example.com/*12', [new Url()]],
125
            ['http://example.com/?test', [new Url()]],
126
            ['http://example.com/#test', [new Url()]],
127
            ['http://example.com:80/#test', [new Url()]],
128
            ['http://example.com:65535/#test', [new Url()]],
129
            ['http://example.com:81/?good', [new Url()]],
130
            ['http://example.com?test', [new Url()]],
131
            ['http://example.com#test', [new Url()]],
132
            ['http://example.com:81#test', [new Url()]],
133
            ['http://example.com:81?good', [new Url()]],
134
135
            ['yiiframework.com', [new Url(pattern: '/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)/')]],
136
137
            ['ftp://ftp.ruhr-uni-bochum.de/', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])]],
138
            ['http://google.de', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])]],
139
            ['https://google.de', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])]],
140
141
            ['HtTp://www.yiiframework.com/', [new Url(validSchemes: ['http', 'FTP'])]],
142
            ['fTp://www.yiiframework.com/', [new Url(validSchemes: ['http', 'FTP'])]],
143
144
            ['http://äüößìà.de', [new Url(enableIDN: true)]],
145
            ['http://xn--zcack7ayc9a.de', [new Url(enableIDN: true)]],
146
            ['домен.рф', [new Url(pattern: '/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i', enableIDN: true)]],
147
148
            ['http://' . str_repeat('a', 1989) . '.de', [new Url()]],
149
        ];
150
    }
151
152
    public function dataValidationFailed(): array
153
    {
154
        $incorrectInputErrors = ['' => ['The value must have a string type.']];
155
        $errors = ['' => ['This value is not a valid URL.']];
156
        $longUrl = 'http://' . str_repeat('u', 1990) . '.de';
157
158
        return [
159
            'incorrect input, integer' => [1, [new Url()], $incorrectInputErrors],
160
            'incorrect input, string in array' => [['yiiframework.com'], [new Url()], $incorrectInputErrors],
161
            'incorrect input, object' => [new stdClass(), [new Url()], $incorrectInputErrors],
162
            'custom incorrect input message' => [
163
                1,
164
                [new Url(incorrectInputMessage: 'Custom incorrect input message.')],
165
                ['' => ['Custom incorrect input message.']],
166
            ],
167
            'custom incorrect input message with parameters' => [
168
                1,
169
                [new Url(incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')],
170
                ['' => ['Attribute - , type - int.']],
171
            ],
172
            'custom incorrect input message with parameters, attribute set' => [
173
                ['attribute' => 1],
174
                ['attribute' => [new Url(incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')]],
175
                ['attribute' => ['Attribute - attribute, type - int.']],
176
            ],
177
178
            ['google.de', [new Url()], $errors],
179
            ['htp://yiiframework.com', [new Url()], $errors],
180
            ['ftp://ftp.ruhr-uni-bochum.de/', [new Url()], $errors],
181
            ['http://invalid,domain', [new Url()], $errors],
182
            ['http://example.com,', [new Url()], $errors],
183
            ['http://example.com*12', [new Url()], $errors],
184
            ['http://example.com,?test', [new Url()], $errors],
185
            ['http://example.com:?test', [new Url()], $errors],
186
            ['http://example.com:test', [new Url()], $errors],
187
            ['http://example.com:123456/test', [new Url()], $errors],
188
            ['http://äüö?=!"§$%&/()=}][{³²€.edu', [new Url()], $errors],
189
190
191
            ['htp://yiiframework.com', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])], $errors],
192
            // Relative URLs are not supported
193
            ['//yiiframework.com', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])], $errors],
194
195
            ['', [new Url(enableIDN: true)], $errors],
196
            [$longUrl, [new Url(enableIDN: true)], $errors],
197
            [$longUrl, [new Url()], $errors],
198
199
            'custom message' => [
200
                '',
201
                [new Url(enableIDN: true, message: 'Custom message.')],
202
                ['' => ['Custom message.']],
203
            ],
204
            'custom message with parameters' => [
205
                'not a url',
206
                [new Url(enableIDN: true, message: 'Attribute - {attribute}, value - {value}.')],
207
                ['' => ['Attribute - , value - not a url.']],
208
            ],
209
            'custom message with parameters, attribute set' => [
210
                ['attribute' => 'not a url'],
211
                ['attribute' => new Url(enableIDN: true, message: 'Attribute - {attribute}, value - {value}.')],
212
                ['attribute' => ['Attribute - attribute, value - not a url.']],
213
            ],
214
        ];
215
    }
216
217
    public function testSkipOnError(): void
218
    {
219
        $this->testSkipOnErrorInternal(new Url(), new Url(skipOnError: true));
220
    }
221
222
    public function testWhen(): void
223
    {
224
        $when = static fn (mixed $value): bool => $value !== null;
225
        $this->testWhenInternal(new Url(), new Url(when: $when));
226
    }
227
228
    protected function getDifferentRuleInHandlerItems(): array
229
    {
230
        return [Url::class, UrlHandler::class];
231
    }
232
}
233