Passed
Push — master ( ebf663...def3a6 )
by Alexander
02:38
created

ResultTest::testDataKeysWithDots()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 23
rs 9.9
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
use Yiisoft\Validator\Rule\Callback;
12
use Yiisoft\Validator\Rule\Nested;
13
use Yiisoft\Validator\Validator;
14
15
class ResultTest extends TestCase
16
{
17
    public function isValidByDefault(): void
18
    {
19
        $result = new Result();
20
        $this->assertTrue($result->isValid());
21
    }
22
23
    public function errorsAreEmptyByDefault(): void
24
    {
25
        $result = new Result();
26
        $this->assertEmpty($result->getErrorMessages());
27
    }
28
29
    public function errorsAreProperlyAdded(): void
30
    {
31
        $result = new Result();
32
        $result->addError('Error 1')
33
            ->addError('Error 2');
34
35
        $this->assertEquals(['Error 1', 'Error 2'], $result->getErrorMessages());
36
    }
37
38
    public function addingErrorChangesIsValid(): void
39
    {
40
        $result = new Result();
41
        $result->addError('Error');
42
43
        $this->assertFalse($result->isValid());
44
    }
45
46
    public function testGetErrors(): void
47
    {
48
        $this->assertEquals(
49
            [new Error('error1'), new Error('error2', [], ['path', 2])],
50
            $this->createErrorResult()->getErrors()
51
        );
52
    }
53
54
    public function testGetErrorMessages(): void
55
    {
56
        $this->assertSame(['error1', 'error2'], $this->createErrorResult()->getErrorMessages());
57
    }
58
59
    public function testGetErrorMessagesIndexedByPath(): void
60
    {
61
        $this->assertEquals(
62
            ['' => ['error1'], 'path.2' => ['error2']],
63
            $this->createErrorResult()->getErrorMessagesIndexedByPath()
64
        );
65
    }
66
67
    public function testGetErrorMessagesIndexedByPathWithAttributes(): void
68
    {
69
        $this->assertEquals(
70
            [
71
                'attribute2' => ['error2.1', 'error2.2'],
72
                'attribute2.nested' => ['error2.3', 'error2.4'],
73
                '' => ['error3.1', 'error3.2'],
74
                'attribute4.subattribute4\.1.subattribute4*2' => ['error4.1'],
75
                'attribute4.subattribute4\.3.subattribute4*4' => ['error4.2'],
76
            ],
77
            $this->createAttributeErrorResult()->getErrorMessagesIndexedByPath()
78
        );
79
    }
80
81
    private function createErrorResult(): Result
82
    {
83
        $result = new Result();
84
        $result
85
            ->addError('error1')
86
            ->addError('error2', [], ['path', 2]);
87
88
        return $result;
89
    }
90
91
    public function testIsAttributeValid(): void
92
    {
93
        $result = $this->createAttributeErrorResult();
94
95
        $this->assertTrue($result->isAttributeValid('attribute1'));
96
        $this->assertFalse($result->isAttributeValid('attribute2'));
97
        $this->assertFalse($result->isAttributeValid('attribute4'));
98
    }
99
100
    public function testGetErrorMessagesIndexedByAttribute(): void
101
    {
102
        $this->assertEquals(
103
            [
104
                'attribute2' => ['error2.1', 'error2.2', 'error2.3', 'error2.4'],
105
                '' => ['error3.1', 'error3.2'],
106
                'attribute4' => ['error4.1', 'error4.2'],
107
            ],
108
            $this->createAttributeErrorResult()->getErrorMessagesIndexedByAttribute()
109
        );
110
    }
111
112
    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...
113
    {
114
        $result = new Result();
115
116
        $result->addError('error1', [], [1]);
117
118
        $this->expectException(InvalidArgumentException::class);
119
        $result->getErrorMessagesIndexedByAttribute();
120
    }
121
122
    public function testGetAttributeErrors(): void
123
    {
124
        $result = $this->createAttributeErrorResult();
125
126
        $this->assertEquals([], $result->getAttributeErrors('attribute1'));
127
        $this->assertEquals(
128
            [
129
                new Error('error2.1', [], ['attribute2']),
130
                new Error('error2.2', [], ['attribute2']),
131
                new Error('error2.3', [], ['attribute2', 'nested']),
132
                new Error('error2.4', [], ['attribute2', 'nested']),
133
            ],
134
            $result->getAttributeErrors('attribute2')
135
        );
136
        $this->assertEquals([new Error('error3.1'), new Error('error3.2')], $result->getAttributeErrors(''));
137
        $this->assertEquals(
138
            [
139
                new Error('error4.1', [], ['attribute4', 'subattribute4.1', 'subattribute4*2']),
140
                new Error('error4.2', [], ['attribute4', 'subattribute4.3', 'subattribute4*4']),
141
            ],
142
            $result->getAttributeErrors('attribute4')
143
        );
144
    }
145
146
    public function testGetAttributeErrorMessages(): void
147
    {
148
        $result = $this->createAttributeErrorResult();
149
150
        $this->assertEquals([], $result->getAttributeErrorMessages('attribute1'));
151
        $this->assertEquals(
152
            ['error2.1', 'error2.2', 'error2.3', 'error2.4'],
153
            $result->getAttributeErrorMessages('attribute2')
154
        );
155
        $this->assertEquals(['error3.1', 'error3.2'], $result->getAttributeErrorMessages(''));
156
        $this->assertEquals(['error4.1', 'error4.2'], $result->getAttributeErrorMessages('attribute4'));
157
    }
158
159
    public function testGetAttributeErrorMessagesIndexedByPath(): void
160
    {
161
        $result = $this->createAttributeErrorResult();
162
163
        $this->assertEquals([], $result->getAttributeErrorMessagesIndexedByPath('attribute1'));
164
        $this->assertEquals(
165
            ['' => ['error2.1', 'error2.2'], 'nested' => ['error2.3', 'error2.4']],
166
            $result->getAttributeErrorMessagesIndexedByPath('attribute2')
167
        );
168
        $this->assertEquals(['' => ['error3.1', 'error3.2']], $result->getAttributeErrorMessagesIndexedByPath(''));
169
        $this->assertEquals([
170
            'subattribute4\.1.subattribute4*2' => ['error4.1'],
171
            'subattribute4\.3.subattribute4*4' => ['error4.2'],
172
        ], $result->getAttributeErrorMessagesIndexedByPath('attribute4'));
173
    }
174
175
    public function testGetCommonErrorMessages(): void
176
    {
177
        $this->assertEquals(['error3.1', 'error3.2'], $this->createAttributeErrorResult()->getCommonErrorMessages());
178
    }
179
180
    /**
181
     * @see https://github.com/yiisoft/validator/issues/610
182
     */
183
    public function testDataKeysWithDots(): void
184
    {
185
        $result = (new Validator())->validate(
186
            [
187
                'user.age' => 17,
188
                'meta' => [
189
                    'tag' => 'hi',
190
                ],
191
            ],
192
            [
193
                'user.age' => static fn() => (new Result())->addError('Too young.'),
194
                'meta' => new Nested([
195
                    'tag' => new Callback(static fn() => (new Result())->addError('Too short.')),
196
                ]),
197
            ],
198
        );
199
200
        $this->assertSame(
201
            [
202
                'user\.age' => ['Too young.'],
203
                'meta.tag' => ['Too short.'],
204
            ],
205
            $result->getErrorMessagesIndexedByPath()
206
        );
207
    }
208
209
    public function testEscapeInGetErrorMessagesIndexedByPath(): void
210
    {
211
        $result = (new Result())->addError('e1', valuePath: ['user', 'meta.the-age']);
212
213
        $this->assertSame(
214
            [
215
                'user.meta.the\-age' => ['e1'],
216
            ],
217
            $result->getErrorMessagesIndexedByPath(escape: '-'),
218
        );
219
    }
220
221
    public function testEscapeInGetAttributeErrorMessagesIndexedByPath(): void
222
    {
223
        $result = (new Result())->addError('e1', valuePath: ['user', 'data', 'meta.the-age']);
224
225
        $this->assertSame(
226
            [
227
                'data.meta.the\-age' => ['e1'],
228
            ],
229
            $result->getAttributeErrorMessagesIndexedByPath('user', escape: '-'),
230
        );
231
    }
232
233
    private function createAttributeErrorResult(): Result
234
    {
235
        $result = new Result();
236
        $result
237
            ->addError('error2.1', [], ['attribute2'])
238
            ->addError('error2.2', [], ['attribute2'])
239
            ->addError('error2.3', [], ['attribute2', 'nested'])
240
            ->addError('error2.4', [], ['attribute2', 'nested'])
241
            ->addError('error3.1')
242
            ->addError('error3.2')
243
            ->addError('error4.1', [], ['attribute4', 'subattribute4.1', 'subattribute4*2'])
244
            ->addError('error4.2', [], ['attribute4', 'subattribute4.3', 'subattribute4*4']);
245
246
        return $result;
247
    }
248
}
249