Passed
Pull Request — master (#412)
by
unknown
17:58 queued 15:20
created

UrlTest::testWhen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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