Completed
Push — master ( 906e59...08ade2 )
by AJ
02:01
created

PatternTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testPatternURL() 0 18 2
A testPatternMAC() 0 28 2
A testPatternEmail() 0 18 2
A testPatternIP() 0 38 2
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
}