Passed
Pull Request — master (#11)
by Harry Osmar
01:51
created

ValidationTest::test_run_name_validation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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