generateValidationMultiFieldMultiRules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 9
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 13
rs 9.4285
1
<?php
2
3
namespace PluginSimpleValidate\Tests\unit;
4
5
use PluginSimpleValidate\Field;
6
use PluginSimpleValidate\Validation;
7
8
class ValidationTest extends Base
9
{
10
    public function setUp()
11
    {
12
        parent::setUp();
13
    }
14
15
    public function test_run()
16
    {
17
        $validation = $this->generateValidationMultiFieldMultiRules();
18
19
        $this->assertFalse($validation->run());
20
21
        $this->assertEquals(
22
            [
23
                'email' => [
24
                    'field is required',
25
                    'field must be a valid email address',
26
                ],
27
                'password' => [
28
                    'field is required',
29
                    'field may only letters and numbers',
30
                    'field length must be greater than 5',
31
                ],
32
            ],
33
            $validation->getErrors()
34
        );
35
    }
36
37
    public function test_run_contain_multi_field_multi_value()
38
    {
39
        $validation = $this->generateValidationMultiFieldMultiRules();
40
        $name = '';
41
        $validation->addField((new \PluginSimpleValidate\MultiValues\Field('name'))
42
            ->isTrue($name !== '', 'required')
43
            ->isTrue(strlen($name) > 6, 'length must be greater than 6'));
44
45
        $this->assertFalse($validation->run());
46
47
        $this->assertEquals(
48
            [
49
                'email' => [
50
                    'field is required',
51
                    'field must be a valid email address',
52
                ],
53
                'password' => [
54
                    'field is required',
55
                    'field may only letters and numbers',
56
                    'field length must be greater than 5',
57
                ],
58
                'name' => [
59
                    'required',
60
                    'length must be greater than 6'
61
                ]
62
            ],
63
            $validation->getErrors()
64
        );
65
    }
66
67
    public function test_run_name_validation()
68
    {
69
        $firstNameField = (new Field('firstname', ''))->required()->lengthGreaterOrEqualThan(4);
70
        $lastNameField = (new Field('lastname', ''))->required()->lengthGreaterOrEqualThan(4);
71
        $fullNameField = (new Field(
72
            'fullname',
73
            $firstNameField->getValue() . ' ' . $lastNameField->getValue()
74
        ))->lengthGreaterOrEqualThan(10);
75
76
        $validation = new Validation($this->language);
77
78
        $validation->addField($firstNameField)->addField($lastNameField)->addField($fullNameField);
79
80
        $this->assertFalse($validation->run());
81
82
        $this->assertEquals(
83
            [
84
                'firstname' => [
85
                    'field is required',
86
                    'field length must be greater or equal than 4'
87
                ],
88
                'lastname' => [
89
                    'field is required',
90
                    'field length must be greater or equal than 4'
91
                ],
92
                'fullname' => [
93
                    'field length must be greater or equal than 10',
94
                ],
95
            ],
96
            $validation->getErrors()
97
        );
98
    }
99
100
    public function test_run_with_error_break()
101
    {
102
        $validation = $this->generateValidationMultiFieldMultiRules();
103
104
        $this->assertFalse($validation->run(true));
105
106
        $this->assertEquals(
107
            [
108
                'email' => [
109
                    'field is required',
110
                    'field must be a valid email address',
111
                ],
112
            ],
113
            $validation->getErrors()
114
        );
115
    }
116
117
    private function generateValidationMultiFieldMultiRules()
118
    {
119
        $validation = new Validation($this->language);
120
121
        return $validation->addField((new Field('email', ''))
122
            ->required()
123
            ->validEmail()
124
        )->addField((new Field('password', ''))
125
            ->required()
126
            ->isAlphaOrNumeric()
127
            ->lengthGreaterThan(5)
128
        );
129
    }
130
}
131