Passed
Push — master ( 7f3d17...4895fa )
by AJ
02:47
created

PatternTest::testPatternEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 19
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 *       __  ___      ____  _     ___                           _                    __
4
 *      /  |/  /_  __/ / /_(_)___/ (_)___ ___  ___  ____  _____(_)___  ____   ____ _/ /
5
 *     / /|_/ / / / / / __/ / __  / / __ `__ \/ _ \/ __ \/ ___/ / __ \/ __ \ / __ `/ /
6
 *    / /  / / /_/ / / /_/ / /_/ / / / / / / /  __/ / / (__  ) / /_/ / / / // /_/ / /
7
 *   /_/  /_/\__,_/_/\__/_/\__,_/_/_/ /_/ /_/\___/_/ /_/____/_/\____/_/ /_(_)__,_/_/
8
 *
9
 *  Array Sanitization Library
10
 *  Copyright (c) Multidimension.al (http://multidimension.al)
11
 *  Github : https://github.com/multidimension-al/array-sanitization
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-sanitization Github
19
 *  @license    http://www.opensource.org/licenses/mit-license.php MIT License
20
 */
21
22
namespace Multidimensional\ArraySanitization\Test;
23
24
use Multidimensional\ArraySanitization\Sanitization;
25
use PHPUnit\Framework\TestCase;
26
27
class PatternTest extends TestCase
28
{
29
    public function testPatternURL()
30
    {
31
        $rules = [
32
            'a' => ['type' => 'string', 'pattern' => 'URL'],
33
            'b' => ['type' => 'string', 'pattern' => 'URL'],
34
            'c' => ['type' => 'string', 'pattern' => 'URL']
35
        ];
36
        $array = [
37
            'a' => 'www.domain.com',
38
            'b' => 'http://www.google.com/index.html',
39
            'c' => '™dožma Òin.com©',
40
        ];
41
        $result = Sanitization::sanitize($array, $rules);
42
        $expected = [
43
            'a' => 'www.domain.com',
44
            'b' => 'http://www.google.com/index.html',
45
            'c' => 'domain.com'
46
        ];
47
        $this->assertEquals($expected, $result);
48
    }
49
50
    public function testPatternEmail()
51
    {
52
        $rules = [
53
            'a' => ['type' => 'string', 'pattern' => 'EMAIL'],
54
            'b' => ['type' => 'string', 'pattern' => 'EMAIL'],
55
            'c' => ['type' => 'string', 'pattern' => 'EMAIL']
56
        ];
57
        $array = [
58
            'a' => '[email protected]',
59
            'b' => '[email protected]',
60
            'c' => '(noreply) at :domain.©com',
61
        ];
62
        $result = Sanitization::sanitize($array, $rules);
63
        $expected = [
64
            'a' => '[email protected]',
65
            'b' => '[email protected]',
66
            'c' => 'noreplyatdomain.com'
67
        ];
68
        $this->assertEquals($expected, $result);
69
    }
70
71
    public function testPatternMAC()
72
    {
73
        $rules = [
74
            'a' => ['type' => 'string', 'pattern' => 'MAC'],
75
            'b' => ['type' => 'string', 'pattern' => 'MAC'],
76
            'c' => ['type' => 'string', 'pattern' => 'MAC'],
77
            'd' => ['type' => 'string', 'pattern' => 'MAC']
78
        ];
79
        $array = [
80
            'a' => 'AB:CD:EF:12:34:56',
81
            'b' => 'AB-CD-EF-12-34-56',
82
            'c' => 'ABCD.EF12.3456',
83
            'd' => 'STDD:RE5P:DD:00:BV4:$#!@%^&*12'
84
        ];
85
        $result = Sanitization::sanitize($array, $rules);
86
        $expected = [
87
            'a' => 'AB:CD:EF:12:34:56',
88
            'b' => 'AB-CD-EF-12-34-56',
89
            'c' => 'ABCD.EF12.3456',
90
            'd' => 'DD:E5:DD:00:B4:12'
91
        ];
92
        $this->assertEquals($expected, $result);
93
    }
94
95
    public function testPatternIP()
96
    {
97
        $rules = [
98
            'a' => ['type' => 'string', 'pattern' => 'IP'],
99
            'b' => ['type' => 'string', 'pattern' => 'IP'],
100
            'c' => ['type' => 'string', 'pattern' => 'IP'],
101
            'd' => ['type' => 'string', 'pattern' => 'IP']
102
        ];
103
        $array = [
104
            'a' => '127.0.0.1',
105
            'b' => '255.255.255.255',
106
            'c' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
107
            'd' => 'H1GGR12.$@#31^^%.FFD.123::$334D'
108
        ];
109
        $result = Sanitization::sanitize($array, $rules);
110
        $expected = [
111
            'a' => '127.0.0.1',
112
            'b' => '255.255.255.255',
113
            'c' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
114
            'd' => '112.31.FFD.123::334D' //Okay this isn't valid, but its sanitized correctly.
115
        ];
116
        $this->assertEquals($expected, $result);
117
    }
118
}