Passed
Push — master ( 401d75...a81b07 )
by AJ
02:09
created

PatternTest::testPatternNotRequired()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 62
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 42
nc 4
nop 0
dl 0
loc 62
rs 9.248
c 0
b 0
f 0

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
 *       __  ___      ____  _     ___                           _                    __
4
 *      /  |/  /_  __/ / /_(_)___/ (_)___ ___  ___  ____  _____(_)___  ____   ____ _/ /
5
 *     / /|_/ / / / / / __/ / __  / / __ `__ \/ _ \/ __ \/ ___/ / __ \/ __ \ / __ `/ /
6
 *    / /  / / /_/ / / /_/ / /_/ / / / / / / /  __/ / / (__  ) / /_/ / / / // /_/ / /
7
 *   /_/  /_/\__,_/_/\__/_/\__,_/_/_/ /_/ /_/\___/_/ /_/____/_/\____/_/ /_(_)__,_/_/
8
 *
9
 *  Array Validation Library
10
 *  Copyright (c) Multidimension.al (http://multidimension.al)
11
 *  Github : https://github.com/multidimension-al/array-validation
12
 *
13
 *  Licensed under The MIT License
14
 *  For full copyright and license information, please see the LICENSE file
15
 *  Redistributions of files must retain the above copyright notice.
16
 *
17
 *  @copyright  Copyright © 2017-2019 Multidimension.al (http://multidimension.al)
18
 *  @link       https://github.com/multidimension-al/array-validation Github
19
 *  @license    http://www.opensource.org/licenses/mit-license.php MIT License
20
 */
21
22
namespace Multidimensional\ArrayValidation\Test;
23
24
use Exception;
25
use Multidimensional\ArrayValidation\Validation;
26
use PHPUnit\Framework\TestCase;
27
28
class PatternTest extends TestCase
29
{
30
31
    public function testPatternURL()
32
    {
33
        $rules = [
34
            'a' => ['type' => 'string', 'pattern' => 'URL']
35
        ];
36
        $array = [
37
            'a' => 'http://www.google.com/'
38
        ];
39
        $this->assertTrue(Validation::validate($array, $rules));
40
41
        $array = [
42
            'a' => '<a href="http://www.google.com/">Google</a>'
43
        ];
44
45
        try {
46
            $this->assertFalse(Validation::validate($array, $rules));
47
        } catch (Exception $e) {
48
            $this->assertEquals('Invalid value "<a href="http://www.google.com/">Google</a>" does not match URL pattern for key: a.', $e->getMessage());
49
        }
50
    }
51
52
    public function testPatternEmail()
53
    {
54
        $rules = [
55
            'a' => ['type' => 'string', 'pattern' => 'EMAIL']
56
        ];
57
        $array = [
58
            'a' => '[email protected]'
59
        ];
60
        $this->assertTrue(Validation::validate($array, $rules));
61
62
        $array = [
63
            'a' => 'noreply AT domain.com'
64
        ];
65
66
        try {
67
            $this->assertFalse(Validation::validate($array, $rules));
68
        } catch (Exception $e) {
69
            $this->assertEquals('Invalid value "noreply AT domain.com" does not match email pattern for key: a.', $e->getMessage());
70
        }
71
    }
72
73
    public function testPatternMAC()
74
    {
75
        $rules = [
76
            'a' => ['type' => 'string', 'pattern' => 'MAC']
77
        ];
78
        $array = [
79
            'a' => 'AB:CD:EF:12:34:56'
80
        ];
81
        $this->assertTrue(Validation::validate($array, $rules));
82
83
        $array = [
84
            'a' => 'AB-CD-EF-12-34-56'
85
        ];
86
        $this->assertTrue(Validation::validate($array, $rules));
87
88
        $array = [
89
            'a' => 'ABCD.EF12.3456'
90
        ];
91
        $this->assertTrue(Validation::validate($array, $rules));
92
93
        $array = [
94
            'a' => '127.0.0.1'
95
        ];
96
97
        try {
98
            $this->assertFalse(Validation::validate($array, $rules));
99
        } catch (Exception $e) {
100
            $this->assertEquals('Invalid value "127.0.0.1" does not match MAC address pattern for key: a.', $e->getMessage());
101
        }
102
    }
103
104
    public function testPatternIP()
105
    {
106
        $rules = [
107
            'a' => ['type' => 'string', 'pattern' => 'IP']
108
        ];
109
        $array = [
110
            'a' => '127.0.0.1'
111
        ];
112
        $this->assertTrue(Validation::validate($array, $rules));
113
114
        $array = [
115
            'a' => '255.255.255.255'
116
        ];
117
        $this->assertTrue(Validation::validate($array, $rules));
118
119
        $array = [
120
            'a' => '0.0.0.0'
121
        ];
122
        $this->assertTrue(Validation::validate($array, $rules));
123
124
        $array = [
125
            'a' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
126
        ];
127
        $this->assertTrue(Validation::validate($array, $rules));
128
129
        $array = [
130
            'a' => '2001:0DB8:1111::'
131
        ];
132
        $this->assertTrue(Validation::validate($array, $rules));
133
134
        $array = [
135
            'a' => '127/0/0/1'
136
        ];
137
138
        try {
139
            $this->assertFalse(Validation::validate($array, $rules));
140
        } catch (Exception $e) {
141
            $this->assertEquals('Invalid value "127/0/0/1" does not match IP address pattern for key: a.', $e->getMessage());
142
        }
143
    }
144
145
    public function testPatternNotRequired()
146
    {
147
        $rules = [
148
            'a' => ['type' => 'int', 'pattern' => '\d{5}'],
149
            'b' => ['type' => 'int', 'pattern' => '\d{5}'],
150
            'c' => ['type' => 'int', 'pattern' => '\d{5}']
151
        ];
152
        $array = [
153
            'a' => null,
154
            'b' => '',
155
            'c' => 55555
156
        ];
157
        $this->assertTrue(Validation::validate($array, $rules));
158
159
        $rules = [
160
            'a' => ['type' => 'int', 'pattern' => '\d{5}', 'required' => true]
161
        ];
162
        $array = [
163
            'a' => null
164
        ];
165
        try {
166
            $this->assertFalse(Validation::validate($array, $rules));
167
        } catch (Exception $e) {
168
            $this->assertEquals('Required value not found for key: a.', $e->getMessage());
169
        }
170
171
        $array = [
172
            'a' => ''
173
        ];
174
        try {
175
            $this->assertFalse(Validation::validate($array, $rules));
176
        } catch (Exception $e) {
177
            $this->assertEquals('Required value not found for key: a.', $e->getMessage());
178
        }
179
180
        $rules = [
181
            'a' => ['type' => 'int', 'pattern' => '\d{1}', 'required' => true]
182
        ];
183
        $array = [
184
            'a' => 0
185
        ];
186
        $this->assertTrue(Validation::validate($array, $rules));
187
188
        $rules = [
189
            'a' => ['type' => 'int', 'required' => true]
190
        ];
191
        $array = [
192
            'a' => 0
193
        ];
194
        $this->assertTrue(Validation::validate($array, $rules));
195
196
        $rules = [
197
            'a' => ['type' => 'string', 'pattern' => 'IP'],
198
            'b' => ['type' => 'string', 'pattern' => 'IP'],
199
            'c' => ['type' => 'string', 'pattern' => 'IP']
200
        ];
201
        $array = [
202
            'a' => null,
203
            'b' => '',
204
            'c' => '127.0.0.1'
205
        ];
206
        $this->assertTrue(Validation::validate($array, $rules));
207
208
    }
209
210
211
}