Passed
Pull Request — master (#415)
by
unknown
29:22 queued 26:39
created

ResultTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 86
dl 0
loc 179
rs 10
c 0
b 0
f 0
wmc 17

17 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidByDefault() 0 4 1
A testGetErrorMessagesIndexedByPath() 0 5 1
A testGetErrors() 0 5 1
A testGetAttributeErrorMessages() 0 11 1
A testIsAttributeValid() 0 7 1
A testGetErrorMessages() 0 3 1
A testGetErrorMessagesIndexedByAttribute() 0 9 1
A testGetAttributeErrorMessagesIndexedByPath() 0 14 1
A createAttributeErrorResult() 0 14 1
A createErrorResult() 0 8 1
A errorsAreProperlyAdded() 0 7 1
A testGetAttributeErrors() 0 21 1
A errorsAreEmptyByDefault() 0 4 1
A testGetCommonErrorMessages() 0 3 1
A testGetErrorMessagesIndexedByAttribute_IncorrectType() 0 8 1
A testGetErrorMessagesIndexedByPathWithAttributes() 0 11 1
A addingErrorChangesIsValid() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests;
6
7
use InvalidArgumentException;
8
use PHPUnit\Framework\TestCase;
9
use Yiisoft\Validator\Error;
10
use Yiisoft\Validator\Result;
11
12
class ResultTest extends TestCase
13
{
14
    public function isValidByDefault(): void
15
    {
16
        $result = new Result();
17
        $this->assertTrue($result->isValid());
18
    }
19
20
    public function errorsAreEmptyByDefault(): void
21
    {
22
        $result = new Result();
23
        $this->assertEmpty($result->getErrorMessages());
24
    }
25
26
    public function errorsAreProperlyAdded(): void
27
    {
28
        $result = new Result();
29
        $result->addError('Error 1')
30
            ->addError('Error 2');
31
32
        $this->assertEquals(['Error 1', 'Error 2'], $result->getErrorMessages());
33
    }
34
35
    public function addingErrorChangesIsValid(): void
36
    {
37
        $result = new Result();
38
        $result->addError('Error');
39
40
        $this->assertFalse($result->isValid());
41
    }
42
43
    public function testGetErrors(): void
44
    {
45
        $this->assertEquals(
46
            [new Error('error1'), new Error('error2', [], ['path', 2])],
47
            $this->createErrorResult()->getErrors()
48
        );
49
    }
50
51
    public function testGetErrorMessages(): void
52
    {
53
        $this->assertSame(['error1', 'error2'], $this->createErrorResult()->getErrorMessages());
54
    }
55
56
    public function testGetErrorMessagesIndexedByPath(): void
57
    {
58
        $this->assertEquals(
59
            ['' => ['error1'], 'path.2' => ['error2']],
60
            $this->createErrorResult()->getErrorMessagesIndexedByPath()
61
        );
62
    }
63
64
    public function testGetErrorMessagesIndexedByPathWithAttributes(): void
65
    {
66
        $this->assertEquals(
67
            [
68
                'attribute2' => ['error2.1', 'error2.2'],
69
                'attribute2.nested' => ['error2.3', 'error2.4'],
70
                '' => ['error3.1', 'error3.2'],
71
                'attribute4.subattribute4\.1.subattribute4\*2' => ['error4.1'],
72
                'attribute4.subattribute4\.3.subattribute4\*4' => ['error4.2'],
73
            ],
74
            $this->createAttributeErrorResult()->getErrorMessagesIndexedByPath()
75
        );
76
    }
77
78
    private function createErrorResult(): Result
79
    {
80
        $result = new Result();
81
        $result
82
            ->addError('error1')
83
            ->addError('error2', [], ['path', 2]);
84
85
        return $result;
86
    }
87
88
    public function testIsAttributeValid(): void
89
    {
90
        $result = $this->createAttributeErrorResult();
91
92
        $this->assertTrue($result->isAttributeValid('attribute1'));
93
        $this->assertFalse($result->isAttributeValid('attribute2'));
94
        $this->assertFalse($result->isAttributeValid('attribute4'));
95
    }
96
97
    public function testGetErrorMessagesIndexedByAttribute(): void
98
    {
99
        $this->assertEquals(
100
            [
101
                'attribute2' => ['error2.1', 'error2.2', 'error2.3', 'error2.4'],
102
                '' => ['error3.1', 'error3.2'],
103
                'attribute4' => ['error4.1', 'error4.2'],
104
            ],
105
            $this->createAttributeErrorResult()->getErrorMessagesIndexedByAttribute()
106
        );
107
    }
108
109
    public function testGetErrorMessagesIndexedByAttribute_IncorrectType(): void
0 ignored issues
show
Coding Style introduced by
Method name "ResultTest::testGetErrorMessagesIndexedByAttribute_IncorrectType" is not in camel caps format
Loading history...
110
    {
111
        $result = new Result();
112
113
        $result->addError('error1', [], [1]);
114
115
        $this->expectException(InvalidArgumentException::class);
116
        $result->getErrorMessagesIndexedByAttribute();
117
    }
118
119
    public function testGetAttributeErrors(): void
120
    {
121
        $result = $this->createAttributeErrorResult();
122
123
        $this->assertEquals([], $result->getAttributeErrors('attribute1'));
124
        $this->assertEquals(
125
            [
126
                new Error('error2.1', [], ['attribute2']),
127
                new Error('error2.2', [], ['attribute2']),
128
                new Error('error2.3', [], ['attribute2', 'nested']),
129
                new Error('error2.4', [], ['attribute2', 'nested']),
130
            ],
131
            $result->getAttributeErrors('attribute2')
132
        );
133
        $this->assertEquals([new Error('error3.1'), new Error('error3.2')], $result->getAttributeErrors(''));
134
        $this->assertEquals(
135
            [
136
                new Error('error4.1', [], ['attribute4', 'subattribute4.1', 'subattribute4*2']),
137
                new Error('error4.2', [], ['attribute4', 'subattribute4.3', 'subattribute4*4']),
138
            ],
139
            $result->getAttributeErrors('attribute4')
140
        );
141
    }
142
143
    public function testGetAttributeErrorMessages(): void
144
    {
145
        $result = $this->createAttributeErrorResult();
146
147
        $this->assertEquals([], $result->getAttributeErrorMessages('attribute1'));
148
        $this->assertEquals(
149
            ['error2.1', 'error2.2', 'error2.3', 'error2.4'],
150
            $result->getAttributeErrorMessages('attribute2')
151
        );
152
        $this->assertEquals(['error3.1', 'error3.2'], $result->getAttributeErrorMessages(''));
153
        $this->assertEquals(['error4.1', 'error4.2'], $result->getAttributeErrorMessages('attribute4'));
154
    }
155
156
    public function testGetAttributeErrorMessagesIndexedByPath(): void
157
    {
158
        $result = $this->createAttributeErrorResult();
159
160
        $this->assertEquals([], $result->getAttributeErrorMessagesIndexedByPath('attribute1'));
161
        $this->assertEquals(
162
            ['' => ['error2.1', 'error2.2'], 'nested' => ['error2.3', 'error2.4']],
163
            $result->getAttributeErrorMessagesIndexedByPath('attribute2')
164
        );
165
        $this->assertEquals(['' => ['error3.1', 'error3.2']], $result->getAttributeErrorMessagesIndexedByPath(''));
166
        $this->assertEquals([
167
            'subattribute4\.1.subattribute4\*2' => ['error4.1'],
168
            'subattribute4\.3.subattribute4\*4' => ['error4.2'],
169
        ], $result->getAttributeErrorMessagesIndexedByPath('attribute4'));
170
    }
171
172
    public function testGetCommonErrorMessages(): void
173
    {
174
        $this->assertEquals(['error3.1', 'error3.2'], $this->createAttributeErrorResult()->getCommonErrorMessages());
175
    }
176
177
    private function createAttributeErrorResult(): Result
178
    {
179
        $result = new Result();
180
        $result
181
            ->addError('error2.1', [], ['attribute2'])
182
            ->addError('error2.2', [], ['attribute2'])
183
            ->addError('error2.3', [], ['attribute2', 'nested'])
184
            ->addError('error2.4', [], ['attribute2', 'nested'])
185
            ->addError('error3.1')
186
            ->addError('error3.2')
187
            ->addError('error4.1', [], ['attribute4', 'subattribute4.1', 'subattribute4*2'])
188
            ->addError('error4.2', [], ['attribute4', 'subattribute4.3', 'subattribute4*4']);
189
190
        return $result;
191
    }
192
}
193