Passed
Push — master ( dbf0a8...b29633 )
by
unknown
02:35
created

DataSetUsageTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 156
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A dataArrayDataSetUsage() 0 6 1
A testArrayDataSetUsage() 0 6 1
A dataRulesProvidedDataSetUsage() 0 6 1
A testArrayDataSetResult() 0 26 2
A testExplicitRulesHavePriority() 0 28 1
A testEmptyExplicitRulesHavePriority() 0 24 1
A testRulesProvidedDataSetResult() 0 28 2
A testRulesProvidedDataSetUsage() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\DataSet;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Validator\DataSet\ArrayDataSet;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\Rule\Boolean;
11
use Yiisoft\Validator\Rule\HasLength;
12
use Yiisoft\Validator\Rule\Number;
13
use Yiisoft\Validator\Rule\Regex;
14
use Yiisoft\Validator\Tests\Support\DataSet\RulesProvidedDataSet;
15
use Yiisoft\Validator\Tests\Support\ValidatorFactory;
16
17
final class DataSetUsageTest extends TestCase
18
{
19
    public function dataArrayDataSetUsage(): array
20
    {
21
        return [
22
            [
23
                ['bool' => true, 'int' => 41],
24
                ['bool' => [new Boolean()], 'int' => [new Number()]],
25
            ],
26
        ];
27
    }
28
29
    /**
30
     * @dataProvider dataArrayDataSetUsage
31
     */
32
    public function testArrayDataSetUsage(array $dataSet, array $rules): void
33
    {
34
        $dataObject = new ArrayDataSet($dataSet);
35
        $result = ValidatorFactory::make()->validate($dataObject, $rules);
36
37
        $this->assertTrue($result->isValid());
38
    }
39
40
    public function dataRulesProvidedDataSetUsage(): array
41
    {
42
        return [
43
            [
44
                ['bool' => true, 'int' => 41],
45
                ['bool' => [new Boolean()], 'int' => [new Number()]],
46
            ],
47
        ];
48
    }
49
50
    /**
51
     * @dataProvider dataRulesProvidedDataSetUsage
52
     */
53
    public function testRulesProvidedDataSetUsage(array $dataSet, array $rules): void
54
    {
55
        $dataObject = new RulesProvidedDataSet($dataSet, $rules);
56
        $result = ValidatorFactory::make()->validate($dataObject);
57
58
        $this->assertTrue($result->isValid());
59
    }
60
61
    public function testArrayDataSetResult(): void
62
    {
63
        $dataObject = new ArrayDataSet([
64
            'bool' => true,
65
            'int' => 41,
66
        ]);
67
        $rules = [
68
            'bool' => [new Boolean()],
69
            'int' => [
70
                new Number(asInteger: true),
71
                new Number(asInteger: true, min: 44),
72
                static function ($value): Result {
73
                    $result = new Result();
74
                    if ($value !== 42) {
75
                        $result->addError('Value should be 42!', ['int']);
76
                    }
77
78
                    return $result;
79
                },
80
            ],
81
        ];
82
        $result = ValidatorFactory::make()->validate($dataObject, $rules);
83
84
        $this->assertFalse($result->isValid());
85
        $this->assertTrue($result->isAttributeValid('bool'));
86
        $this->assertFalse($result->isAttributeValid('int'));
87
    }
88
89
    public function testRulesProvidedDataSetResult(): void
90
    {
91
        $dataObject = new RulesProvidedDataSet(
92
            [
93
                'bool' => true,
94
                'int' => 41,
95
            ],
96
            [
97
                'bool' => [new Boolean()],
98
                'int' => [
99
                    new Number(asInteger: true),
100
                    new Number(asInteger: true, min: 44),
101
                    static function ($value): Result {
102
                        $result = new Result();
103
                        if ($value !== 42) {
104
                            $result->addError('Value should be 42!', ['int']);
105
                        }
106
107
                        return $result;
108
                    },
109
                ],
110
            ]
111
        );
112
        $result = ValidatorFactory::make()->validate($dataObject);
113
114
        $this->assertFalse($result->isValid());
115
        $this->assertTrue($result->isAttributeValid('bool'));
116
        $this->assertFalse($result->isAttributeValid('int'));
117
    }
118
119
    public function testExplicitRulesHavePriority(): void
120
    {
121
        $dataSet = new RulesProvidedDataSet(
122
            [
123
                'username' => 'test123',
124
                'age' => 42,
125
            ],
126
            [
127
                'username' => [
128
                    new Regex('^[a-z]+$'),
129
                    new HasLength(max: 3),
130
                ],
131
                'age' => [
132
                    new Number(max: 25),
133
                ],
134
            ]
135
        );
136
        $validator = ValidatorFactory::make();
137
        $result = $validator->validate(
138
            $dataSet,
139
            [
140
                'username' => [
141
                    new HasLength(max: 10),
142
                ],
143
            ]
144
        );
145
146
        $this->assertTrue($result->isValid());
147
    }
148
149
    public function testEmptyExplicitRulesHavePriority(): void
150
    {
151
        $dataSet = new RulesProvidedDataSet(
152
            [
153
                'username' => 'test123',
154
                'age' => 42,
155
            ],
156
            [
157
                'username' => [
158
                    new Regex('^[a-z]+$'),
159
                    new HasLength(max: 3),
160
                ],
161
                'age' => [
162
                    new Number(max: 25),
163
                ],
164
            ]
165
        );
166
        $validator = ValidatorFactory::make();
167
        $result = $validator->validate(
168
            $dataSet,
169
            []
170
        );
171
172
        $this->assertTrue($result->isValid());
173
    }
174
}
175