Passed
Pull Request — master (#416)
by Sergei
04:39 queued 01:56
created

UrlTest::dataOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 73
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 49
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 73
rs 9.1127

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\TestEnvironments\WithIntl\Rule;
6
7
use stdClass;
8
use Yiisoft\Validator\Rule\Url;
9
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
10
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
11
12
final class UrlTest extends RuleTestCase
13
{
14
    use RuleWithOptionsTestTrait;
15
16
    public function dataOptions(): array
17
    {
18
        return [
19
            [
20
                new Url(),
21
                [
22
                    'pattern' => '/^((?i)http|https):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
23
                    'validSchemes' => ['http', 'https'],
24
                    'enableIDN' => false,
25
                    'incorrectInputMessage' => [
26
                        'template' => 'The value must have a string type.',
27
                        'parameters' => [],
28
                    ],
29
                    'message' => [
30
                        'template' => 'This value is not a valid URL.',
31
                        'parameters' => [],
32
                    ],
33
                    'skipOnEmpty' => false,
34
                    'skipOnError' => false,
35
                ],
36
            ],
37
            [
38
                new Url(enableIDN: true),
39
                [
40
                    'pattern' => '/^((?i)http|https):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
41
                    'validSchemes' => ['http', 'https'],
42
                    'enableIDN' => true,
43
                    'incorrectInputMessage' => [
44
                        'template' => 'The value must have a string type.',
45
                        'parameters' => [],
46
                    ],
47
                    'message' => [
48
                        'template' => 'This value is not a valid URL.',
49
                        'parameters' => [],
50
                    ],
51
                    'skipOnEmpty' => false,
52
                    'skipOnError' => false,
53
                ],
54
            ],
55
            [
56
                new Url(validSchemes: ['http']),
57
                [
58
                    'pattern' => '/^((?i)http):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
59
                    'validSchemes' => ['http'],
60
                    'enableIDN' => false,
61
                    'incorrectInputMessage' => [
62
                        'template' => 'The value must have a string type.',
63
                        'parameters' => [],
64
                    ],
65
                    'message' => [
66
                        'template' => 'This value is not a valid URL.',
67
                        'parameters' => [],
68
                    ],
69
                    'skipOnEmpty' => false,
70
                    'skipOnError' => false,
71
                ],
72
            ],
73
            [
74
                new Url(pattern: '/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+).*$/', enableIDN: true),
75
                [
76
                    'pattern' => '/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+).*$/',
77
                    'validSchemes' => ['http', 'https'],
78
                    'enableIDN' => true,
79
                    'incorrectInputMessage' => [
80
                        'template' => 'The value must have a string type.',
81
                        'parameters' => [],
82
                    ],
83
                    'message' => [
84
                        'template' => 'This value is not a valid URL.',
85
                        'parameters' => [],
86
                    ],
87
                    'skipOnEmpty' => false,
88
                    'skipOnError' => false,
89
                ],
90
            ],
91
        ];
92
    }
93
94
    public function dataValidationPassed(): array
95
    {
96
        return [
97
            ['http://google.de', [new Url()]],
98
            ['https://google.de', [new Url()]],
99
            [
100
                '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',
101
                [new Url()],
102
            ],
103
            ['http://example.com/*12', [new Url()]],
104
            ['http://example.com/?test', [new Url()]],
105
            ['http://example.com/#test', [new Url()]],
106
            ['http://example.com:80/#test', [new Url()]],
107
            ['http://example.com:65535/#test', [new Url()]],
108
            ['http://example.com:81/?good', [new Url()]],
109
            ['http://example.com?test', [new Url()]],
110
            ['http://example.com#test', [new Url()]],
111
            ['http://example.com:81#test', [new Url()]],
112
            ['http://example.com:81?good', [new Url()]],
113
114
            ['yiiframework.com', [new Url(pattern: '/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)/')]],
115
116
            ['ftp://ftp.ruhr-uni-bochum.de/', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])]],
117
            ['http://google.de', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])]],
118
            ['https://google.de', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])]],
119
120
            ['HtTp://www.yiiframework.com/', [new Url(validSchemes: ['http', 'FTP'])]],
121
            ['fTp://www.yiiframework.com/', [new Url(validSchemes: ['http', 'FTP'])]],
122
123
            ['http://äüößìà.de', [new Url(enableIDN: true)]],
124
            ['http://xn--zcack7ayc9a.de', [new Url(enableIDN: true)]],
125
            ['домен.рф', [new Url(pattern: '/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i', enableIDN: true)]],
126
127
            ['http://' . str_repeat('a', 1989) . '.de', [new Url()]],
128
        ];
129
    }
130
131
    public function dataValidationFailed(): array
132
    {
133
        $incorrectInputErrors = ['' => ['The value must have a string type.']];
134
        $errors = ['' => ['This value is not a valid URL.']];
135
        $longUrl = 'http://' . str_repeat('u', 1990) . '.de';
136
137
        return [
138
            'incorrect input, integer' => [1, [new Url()], $incorrectInputErrors],
139
            'incorrect input, string in array' => [['yiiframework.com'], [new Url()], $incorrectInputErrors],
140
            'incorrect input, object' => [new stdClass(), [new Url()], $incorrectInputErrors],
141
            'custom incorrect input message' => [
142
                1,
143
                [new Url(incorrectInputMessage: 'Custom incorrect input message.')],
144
                ['' => ['Custom incorrect input message.']],
145
            ],
146
            'custom incorrect input message with parameters' => [
147
                1,
148
                [new Url(incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')],
149
                ['' => ['Attribute - , type - int.']],
150
            ],
151
            'custom incorrect input message with parameters, attribute set' => [
152
                ['attribute' => 1],
153
                ['attribute' => [new Url(incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')]],
154
                ['attribute' => ['Attribute - attribute, type - int.']],
155
            ],
156
157
            ['google.de', [new Url()], $errors],
158
            ['htp://yiiframework.com', [new Url()], $errors],
159
            ['ftp://ftp.ruhr-uni-bochum.de/', [new Url()], $errors],
160
            ['http://invalid,domain', [new Url()], $errors],
161
            ['http://example.com,', [new Url()], $errors],
162
            ['http://example.com*12', [new Url()], $errors],
163
            ['http://example.com,?test', [new Url()], $errors],
164
            ['http://example.com:?test', [new Url()], $errors],
165
            ['http://example.com:test', [new Url()], $errors],
166
            ['http://example.com:123456/test', [new Url()], $errors],
167
            ['http://äüö?=!"§$%&/()=}][{³²€.edu', [new Url()], $errors],
168
169
170
            ['htp://yiiframework.com', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])], $errors],
171
            // Relative URLs are not supported
172
            ['//yiiframework.com', [new Url(validSchemes: ['http', 'https', 'ftp', 'ftps'])], $errors],
173
174
            ['', [new Url(enableIDN: true)], $errors],
175
            [$longUrl, [new Url(enableIDN: true)], $errors],
176
            [$longUrl, [new Url()], $errors],
177
178
            'custom message' => [
179
                '',
180
                [new Url(enableIDN: true, message: 'Custom message.')],
181
                ['' => ['Custom message.']],
182
            ],
183
            'custom message with parameters' => [
184
                'not a url',
185
                [new Url(enableIDN: true, message: 'Attribute - {attribute}, value - {value}.')],
186
                ['' => ['Attribute - , value - not a url.']],
187
            ],
188
            'custom message with parameters, attribute set' => [
189
                ['attribute' => 'not a url'],
190
                ['attribute' => new Url(enableIDN: true, message: 'Attribute - {attribute}, value - {value}.')],
191
                ['attribute' => ['Attribute - attribute, value - not a url.']],
192
            ],
193
        ];
194
    }
195
}
196