Completed
Push — master ( 17ecb7...725fdf )
by Alexander
15:14
created

IpTest::testvalidateIPvBoth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 54
cc 1
rs 9.216

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
namespace Yiisoft\Validator\Tests\Rule;
3
4
use PHPUnit\Framework\TestCase;
5
use Yiisoft\Validator\Rule\Ip;
6
7
/**
8
 * @group validators
9
 */
10
class IpTest extends TestCase
11
{
12
    public function testInitException()
13
    {
14
        $this->expectException(\RuntimeException::class);
15
        $this->expectExceptionMessage('Both IPv4 and IPv6 checks can not be disabled at the same time');
16
        new Ip(['ipv4' => false, 'ipv6' => false]);
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Validator\Rule\Ip::__construct() has too many arguments starting with array('ipv4' => false, 'ipv6' => false). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
        /** @scrutinizer ignore-call */ 
17
        new Ip(['ipv4' => false, 'ipv6' => false]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
17
    }
18
19
    public function provideRangesForSubstitution()
20
    {
21
        return [
22
            ['10.0.0.1', ['10.0.0.1']],
23
            [['192.168.0.32', 'fa::/32', 'any'], ['192.168.0.32', 'fa::/32', '0.0.0.0/0', '::/0']],
24
            [['10.0.0.1', '!private'], ['10.0.0.1', '!10.0.0.0/8', '!172.16.0.0/12', '!192.168.0.0/16', '!fd00::/8']],
25
            [['private', '!system'], ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', 'fd00::/8', '!224.0.0.0/4', '!ff00::/8', '!169.254.0.0/16', '!fe80::/10', '!127.0.0.0/8', '!::1', '!192.0.2.0/24', '!198.51.100.0/24', '!203.0.113.0/24', '!2001:db8::/32']],
26
        ];
27
    }
28
29
    /**
30
     * @dataProvider provideRangesForSubstitution
31
     * @param array $range
32
     * @param array $expectedRange
33
     */
34
    public function testRangesSubstitution($range, $expectedRange)
35
    {
36
        $validator = new Ip(['ranges' => $range]);
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Validator\Rule\Ip::__construct() has too many arguments starting with array('ranges' => $range). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $validator = /** @scrutinizer ignore-call */ new Ip(['ranges' => $range]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
37
        $this->assertEquals($expectedRange, $validator->ranges);
38
    }
39
40
41
    public function testValidateOrder()
42
    {
43
        $validator = new Ip([
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Validator\Rule\Ip::__construct() has too many arguments starting with array('ranges' => array(...', '!babe::/8', 'any')). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $validator = /** @scrutinizer ignore-call */ new Ip([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
44
            'ranges' => ['10.0.0.1', '!10.0.0.0/8', '!babe::/8', 'any'],
45
        ]);
46
47
        $this->assertTrue($validator->validate('10.0.0.1'));
48
        $this->assertFalse($validator->validate('10.0.0.2'));
49
        $this->assertTrue($validator->validate('192.168.5.101'));
50
        $this->assertTrue($validator->validate('cafe::babe'));
51
        $this->assertFalse($validator->validate('babe::cafe'));
52
    }
53
54
    public function provideBadIps()
55
    {
56
        return [['not.an.ip'], [['what an array', '??']], [123456], [true], [false], ['bad:forSure']];
57
    }
58
59
    /**
60
     * @dataProvider provideBadIps
61
     * @param mixed $badIp
62
     */
63
    public function testvalidateNotAnIP($badIp)
64
    {
65
        $validator = new Ip();
66
67
        $this->assertFalse($validator->validate($badIp));
68
    }
69
70
    /**
71
     * @dataProvider provideBadIps
72
     * @param mixed $badIp
73
     */
74
    public function testValidateModelAttributeNotAnIP($badIp)
75
    {
76
        $validator = new Ip();
77
        $model = new FakedValidationModel();
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\Tests\Rule\FakedValidationModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
78
79
        $model->attr_ip = $badIp;
80
        $validator->validateAttribute($model, 'attr_ip');
0 ignored issues
show
Bug introduced by
The method validateAttribute() does not exist on Yiisoft\Validator\Rule\Ip. Did you maybe mean validate()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        $validator->/** @scrutinizer ignore-call */ 
81
                    validateAttribute($model, 'attr_ip');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
        $this->assertEquals('attr_ip must be a valid IP address.', $model->getFirstError('attr_ip'));
82
        $model->clearErrors();
83
84
85
        $validator->ipv4 = false;
0 ignored issues
show
Bug introduced by
The property ipv4 is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
86
87
        $model->attr_ip = $badIp;
88
        $validator->validateAttribute($model, 'attr_ip');
89
        $this->assertEquals('attr_ip must be a valid IP address.', $model->getFirstError('attr_ip'));
90
        $model->clearErrors();
91
92
93
        $validator->ipv4 = true;
94
        $validator->ipv6 = false;
0 ignored issues
show
Bug introduced by
The property ipv6 is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
95
96
        $model->attr_ip = $badIp;
97
        $validator->validateAttribute($model, 'attr_ip');
98
        $this->assertEquals('attr_ip must be a valid IP address.', $model->getFirstError('attr_ip'));
99
        $model->clearErrors();
100
    }
101
102
    public function testvalidateIPv4()
103
    {
104
        $validator = new Ip();
105
106
        $this->assertTrue($validator->validate('192.168.10.11'));
107
        $this->assertTrue($validator->validate('192.168.005.001'));
108
        $this->assertFalse($validator->validate('192.168.5.321'));
109
        $this->assertFalse($validator->validate('!192.168.5.32'));
110
        $this->assertFalse($validator->validate('192.168.5.32/11'));
111
112
        $validator->ipv4 = false;
0 ignored issues
show
Bug introduced by
The property ipv4 is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
113
        $this->assertFalse($validator->validate('192.168.10.11'));
114
115
        $validator->ipv4 = true;
116
        $validator->subnet = null;
0 ignored issues
show
Bug introduced by
The property subnet is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
117
118
        $this->assertTrue($validator->validate('192.168.5.32/11'));
119
        $this->assertTrue($validator->validate('192.168.5.32/32'));
120
        $this->assertTrue($validator->validate('0.0.0.0/0'));
121
        $this->assertFalse($validator->validate('192.168.5.32/33'));
122
        $this->assertFalse($validator->validate('192.168.5.32/33'));
123
        $this->assertFalse($validator->validate('192.168.5.32/af'));
124
        $this->assertFalse($validator->validate('192.168.5.32/11/12'));
125
126
        $validator->subnet = true;
127
        $this->assertTrue($validator->validate('10.0.0.1/24'));
128
        $this->assertTrue($validator->validate('10.0.0.1/0'));
129
        $this->assertFalse($validator->validate('10.0.0.1'));
130
131
        $validator->negation = true;
0 ignored issues
show
Bug introduced by
The property negation is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
132
        $this->assertTrue($validator->validate('!192.168.5.32/32'));
133
        $this->assertFalse($validator->validate('!!192.168.5.32/32'));
134
    }
135
136
137
    public function testvalidateIPv6()
138
    {
139
        $validator = new Ip();
140
141
        $this->assertTrue($validator->validate('2008:fa::1'));
142
        $this->assertTrue($validator->validate('2008:00fa::0001'));
143
        $this->assertFalse($validator->validate('2008:fz::0'));
144
        $this->assertFalse($validator->validate('2008:fa::0::1'));
145
        $this->assertFalse($validator->validate('!2008:fa::0::1'));
146
        $this->assertFalse($validator->validate('2008:fa::0:1/64'));
147
148
        $validator->ipv4 = false;
0 ignored issues
show
Bug introduced by
The property ipv4 is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
149
        $this->assertTrue($validator->validate('2008:fa::1'));
150
151
        $validator->ipv6 = false;
0 ignored issues
show
Bug introduced by
The property ipv6 is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
152
        $this->assertFalse($validator->validate('2008:fa::1'));
153
154
        $validator->ipv6 = true;
155
        $validator->subnet = null;
0 ignored issues
show
Bug introduced by
The property subnet is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
156
157
        $this->assertTrue($validator->validate('2008:fa::0:1/64'));
158
        $this->assertTrue($validator->validate('2008:fa::0:1/128'));
159
        $this->assertTrue($validator->validate('2008:fa::0:1/0'));
160
        $this->assertFalse($validator->validate('!2008:fa::0:1/0'));
161
        $this->assertFalse($validator->validate('2008:fz::0/129'));
162
163
        $validator->subnet = true;
164
        $this->assertTrue($validator->validate('2008:db0::1/64'));
165
        $this->assertFalse($validator->validate('2008:db0::1'));
166
167
        $validator->negation = true;
0 ignored issues
show
Bug introduced by
The property negation is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
168
        $this->assertTrue($validator->validate('!2008:fa::0:1/64'));
169
        $this->assertFalse($validator->validate('!!2008:fa::0:1/64'));
170
    }
171
172
    public function testvalidateIPvBoth()
173
    {
174
        $validator = new Ip();
175
176
        $this->assertTrue($validator->validate('192.168.10.11'));
177
        $this->assertTrue($validator->validate('2008:fa::1'));
178
        $this->assertTrue($validator->validate('2008:00fa::0001'));
179
        $this->assertTrue($validator->validate('192.168.005.001'));
180
        $this->assertFalse($validator->validate('192.168.5.321'));
181
        $this->assertFalse($validator->validate('!192.168.5.32'));
182
        $this->assertFalse($validator->validate('192.168.5.32/11'));
183
        $this->assertFalse($validator->validate('2008:fz::0'));
184
        $this->assertFalse($validator->validate('2008:fa::0::1'));
185
        $this->assertFalse($validator->validate('!2008:fa::0::1'));
186
        $this->assertFalse($validator->validate('2008:fa::0:1/64'));
187
188
        $validator->ipv4 = false;
0 ignored issues
show
Bug introduced by
The property ipv4 is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
189
        $this->assertFalse($validator->validate('192.168.10.11'));
190
        $this->assertTrue($validator->validate('2008:fa::1'));
191
192
        $validator->ipv6 = false;
0 ignored issues
show
Bug introduced by
The property ipv6 is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
193
        $validator->ipv4 = true;
194
        $this->assertTrue($validator->validate('192.168.10.11'));
195
        $this->assertFalse($validator->validate('2008:fa::1'));
196
197
        $validator->ipv6 = true;
198
        $validator->subnet = null;
0 ignored issues
show
Bug introduced by
The property subnet is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
199
200
        $this->assertTrue($validator->validate('192.168.5.32/11'));
201
        $this->assertTrue($validator->validate('192.168.5.32/32'));
202
        $this->assertTrue($validator->validate('0.0.0.0/0'));
203
        $this->assertTrue($validator->validate('2008:fa::0:1/64'));
204
        $this->assertTrue($validator->validate('2008:fa::0:1/128'));
205
        $this->assertTrue($validator->validate('2008:fa::0:1/0'));
206
        $this->assertFalse($validator->validate('!2008:fa::0:1/0'));
207
        $this->assertFalse($validator->validate('192.168.5.32/33'));
208
        $this->assertFalse($validator->validate('2008:fz::0/129'));
209
        $this->assertFalse($validator->validate('192.168.5.32/33'));
210
        $this->assertFalse($validator->validate('192.168.5.32/af'));
211
        $this->assertFalse($validator->validate('192.168.5.32/11/12'));
212
213
        $validator->subnet = true;
214
        $this->assertTrue($validator->validate('10.0.0.1/24'));
215
        $this->assertTrue($validator->validate('10.0.0.1/0'));
216
        $this->assertTrue($validator->validate('2008:db0::1/64'));
217
        $this->assertFalse($validator->validate('2008:db0::1'));
218
        $this->assertFalse($validator->validate('10.0.0.1'));
219
220
        $validator->negation = true;
0 ignored issues
show
Bug introduced by
The property negation is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
221
222
        $this->assertTrue($validator->validate('!192.168.5.32/32'));
223
        $this->assertTrue($validator->validate('!2008:fa::0:1/64'));
224
        $this->assertFalse($validator->validate('!!192.168.5.32/32'));
225
        $this->assertFalse($validator->validate('!!2008:fa::0:1/64'));
226
    }
227
228
    public function testValidateRangeIPv4()
229
    {
230
        $validator = new Ip([
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Validator\Rule\Ip::__construct() has too many arguments starting with array('ranges' => array('10.0.1.0/24')). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

230
        $validator = /** @scrutinizer ignore-call */ new Ip([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
231
            'ranges' => ['10.0.1.0/24'],
232
        ]);
233
        $this->assertTrue($validator->validate('10.0.1.2'));
234
        $this->assertFalse($validator->validate('192.5.1.1'));
235
236
        $validator->ranges = ['10.0.1.0/24'];
237
        $this->assertTrue($validator->validate('10.0.1.2'));
238
        $this->assertFalse($validator->validate('10.0.3.2'));
239
240
        $validator->ranges = ['!10.0.1.0/24', '10.0.0.0/8', 'localhost'];
241
        $this->assertFalse($validator->validate('10.0.1.2'));
242
        $this->assertTrue($validator->validate('127.0.0.1'));
243
244
        $validator->subnet = null;
0 ignored issues
show
Bug introduced by
The property subnet is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
245
        $validator->ranges = ['10.0.1.0/24', '!10.0.0.0/8', 'localhost'];
246
        $this->assertTrue($validator->validate('10.0.1.2'));
247
        $this->assertTrue($validator->validate('127.0.0.1'));
248
        $this->assertTrue($validator->validate('10.0.1.28/28'));
249
        $this->assertFalse($validator->validate('10.2.2.2'));
250
        $this->assertFalse($validator->validate('10.0.1.1/22'));
251
    }
252
253
    public function testValidateRangeIPv6()
254
    {
255
        $validator = new Ip([
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Validator\Rule\Ip::__construct() has too many arguments starting with array('ranges' => '2001:db0:1:1::/64'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

255
        $validator = /** @scrutinizer ignore-call */ new Ip([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
256
            'ranges' => '2001:db0:1:1::/64',
257
        ]);
258
        $this->assertTrue($validator->validate('2001:db0:1:1::6'));
259
        $this->assertFalse($validator->validate('2001:db0:1:2::7'));
260
261
        $validator->ranges = ['2001:db0:1:2::/64'];
262
        $this->assertTrue($validator->validate('2001:db0:1:2::7'));
263
264
        $validator->ranges = ['!2001:db0::/32', '2001:db0:1:2::/64'];
265
        $this->assertFalse($validator->validate('2001:db0:1:2::7'));
266
267
        $validator->subnet = null;
0 ignored issues
show
Bug introduced by
The property subnet is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
268
        $validator->ranges = array_reverse($validator->ranges);
269
        $this->assertTrue($validator->validate('2001:db0:1:2::7'));
270
    }
271
272
    public function testValidateRangeIPvBoth()
273
    {
274
        $validator = new Ip([
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Validator\Rule\Ip::__construct() has too many arguments starting with array('ranges' => '10.0.1.0/24'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

274
        $validator = /** @scrutinizer ignore-call */ new Ip([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
275
            'ranges' => '10.0.1.0/24',
276
        ]);
277
        $this->assertTrue($validator->validate('10.0.1.2'));
278
        $this->assertFalse($validator->validate('192.5.1.1'));
279
        $this->assertFalse($validator->validate('2001:db0:1:2::7'));
280
281
        $validator->ranges = ['10.0.1.0/24', '2001:db0:1:2::/64', '127.0.0.1'];
282
        $this->assertTrue($validator->validate('2001:db0:1:2::7'));
283
        $this->assertTrue($validator->validate('10.0.1.2'));
284
        $this->assertFalse($validator->validate('10.0.3.2'));
285
286
        $validator->ranges = ['!system', 'any'];
287
        $this->assertFalse($validator->validate('127.0.0.1'));
288
        $this->assertFalse($validator->validate('fe80::face'));
289
        $this->assertTrue($validator->validate('8.8.8.8'));
290
291
        $validator->subnet = null;
0 ignored issues
show
Bug introduced by
The property subnet is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
292
        $validator->ranges = ['10.0.1.0/24', '2001:db0:1:2::/64', 'localhost', '!all'];
293
        $this->assertTrue($validator->validate('10.0.1.2'));
294
        $this->assertTrue($validator->validate('2001:db0:1:2::7'));
295
        $this->assertTrue($validator->validate('127.0.0.1'));
296
        $this->assertTrue($validator->validate('10.0.1.28/28'));
297
        $this->assertFalse($validator->validate('10.2.2.2'));
298
        $this->assertFalse($validator->validate('10.0.1.1/22'));
299
    }
300
301
    public function testValidateAttributeIPv4()
302
    {
303
        $validator = new Ip();
304
        $model = new FakedValidationModel();
305
306
        $validator->subnet = null;
0 ignored issues
show
Bug introduced by
The property subnet is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
307
308
        $model->attr_ip = '8.8.8.8';
309
        $validator->validateAttribute($model, 'attr_ip');
310
        $this->assertFalse($model->hasErrors('attr_ip'));
311
        $this->assertEquals('8.8.8.8', $model->attr_ip);
312
313
        $validator->subnet = false;
314
315
        $model->attr_ip = '8.8.8.8';
316
        $validator->validateAttribute($model, 'attr_ip');
317
        $this->assertFalse($model->hasErrors('attr_ip'));
318
        $this->assertEquals('8.8.8.8', $model->attr_ip);
319
320
        $model->attr_ip = '8.8.8.8/24';
321
        $validator->validateAttribute($model, 'attr_ip');
322
        $this->assertTrue($model->hasErrors('attr_ip'));
323
        $this->assertEquals('attr_ip must not be a subnet.', $model->getFirstError('attr_ip'));
324
        $model->clearErrors();
325
326
        $validator->subnet = null;
327
        $validator->normalize = true;
0 ignored issues
show
Bug introduced by
The property normalize is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
328
329
        $model->attr_ip = '8.8.8.8';
330
        $validator->validateAttribute($model, 'attr_ip');
331
        $this->assertFalse($model->hasErrors('attr_ip'));
332
        $this->assertEquals('8.8.8.8/32', $model->attr_ip);
333
    }
334
335
336
    public function testValidateAttributeIPv6()
337
    {
338
        $validator = new Ip();
339
        $model = new FakedValidationModel();
340
341
        $validator->subnet = null;
0 ignored issues
show
Bug introduced by
The property subnet is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
342
343
        $model->attr_ip = '2001:db0:1:2::1';
344
        $validator->validateAttribute($model, 'attr_ip');
345
        $this->assertFalse($model->hasErrors('attr_ip'));
346
        $this->assertEquals('2001:db0:1:2::1', $model->attr_ip);
347
348
        $validator->subnet = false;
349
350
        $model->attr_ip = '2001:db0:1:2::7';
351
        $validator->validateAttribute($model, 'attr_ip');
352
        $this->assertFalse($model->hasErrors('attr_ip'));
353
        $this->assertEquals('2001:db0:1:2::7', $model->attr_ip);
354
355
        $model->attr_ip = '2001:db0:1:2::7/64';
356
        $validator->validateAttribute($model, 'attr_ip');
357
        $this->assertTrue($model->hasErrors('attr_ip'));
358
        $this->assertEquals('attr_ip must not be a subnet.', $model->getFirstError('attr_ip'));
359
        $model->clearErrors();
360
361
        $validator->subnet = null;
362
        $validator->normalize = true;
0 ignored issues
show
Bug introduced by
The property normalize is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
363
364
        $model->attr_ip = 'fa01::1';
365
        $validator->validateAttribute($model, 'attr_ip');
366
        $this->assertFalse($model->hasErrors('attr_ip'));
367
        $this->assertEquals('fa01::1/128', $model->attr_ip);
368
369
        $model->attr_ip = 'fa01::1/64';
370
        $validator->validateAttribute($model, 'attr_ip');
371
        $this->assertFalse($model->hasErrors('attr_ip'));
372
        $this->assertEquals('fa01::1/64', $model->attr_ip);
373
374
        $validator->expandIPv6 = true;
0 ignored issues
show
Bug introduced by
The property expandIPv6 is declared private in Yiisoft\Validator\Rule\Ip and cannot be accessed from this context.
Loading history...
375
376
        $model->attr_ip = 'fa01::1/64';
377
        $validator->validateAttribute($model, 'attr_ip');
378
        $this->assertFalse($model->hasErrors('attr_ip'));
379
        $this->assertEquals('fa01:0000:0000:0000:0000:0000:0000:0001/64', $model->attr_ip);
380
381
        $model->attr_ip = 'fa01::2/614';
382
        $validator->validateAttribute($model, 'attr_ip');
383
        $this->assertTrue($model->hasErrors('attr_ip'));
384
        $this->assertEquals('fa01::2/614', $model->attr_ip);
385
        $this->assertEquals('attr_ip contains wrong subnet mask.', $model->getFirstError('attr_ip'));
386
    }
387
}
388