Passed
Push — master ( eabf67...a606bb )
by Alexander
07:46
created

ValidatorTest.php$0 ➔ testAddingRulesViaConstructor()   A

Complexity

Conditions 2

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Validator\DataSetInterface;
9
use Yiisoft\Validator\MissingAttributeException;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\Rule\Boolean;
12
use Yiisoft\Validator\Rule\Number;
13
use Yiisoft\Validator\Validator;
14
15
class ValidatorTest extends TestCase
16
{
17
    public function getDataObject(array $attributes): DataSetInterface
18
    {
19
        return new class($attributes) implements DataSetInterface {
20
            private array $data;
21
22
            public function __construct(array $data)
23
            {
24
                $this->data = $data;
25
            }
26
27
            public function getAttributeValue(string $attribute)
28
            {
29
                if (!$this->hasAttribute($attribute)) {
30
                    throw new MissingAttributeException("There is no \"$attribute\" attribute in the class.");
31
                }
32
33
                return $this->data[$attribute];
34
            }
35
36
            public function hasAttribute(string $attribute): bool
37
            {
38
                return isset($this->data[$attribute]);
39
            }
40
        };
41
    }
42
43
    public function testAddingRulesViaConstructor(): void
44
    {
45
        $dataObject = $this->getDataObject(
46
            [
47
                'bool' => true,
48
                'int' => 41,
49
            ]
50
        );
51
52
        $validator = new Validator(
53
            [
54
                'bool' => [new Boolean()],
55
                'int' => [
56
                    (new Number())->integer(),
57
                    (new Number())->integer()->min(44),
58
                    static function ($value): Result {
59
                        $result = new Result();
60
                        if ($value !== 42) {
61
                            $result->addError('Value should be 42!');
62
                        }
63
                        return $result;
64
                    }
65
                ],
66
            ]
67
        );
68
69
        $results = $validator->validate($dataObject);
70
71
        $this->assertTrue($results->getResult('bool')->isValid());
72
73
        $intResult = $results->getResult('int');
74
        $this->assertFalse($intResult->isValid());
75
        $this->assertCount(1, $intResult->getErrors());
76
    }
77
78
    public function testAddingRulesOneByOne(): void
79
    {
80
        $dataObject = $this->getDataObject(
81
            [
82
                'bool' => true,
83
                'int' => 42,
84
            ]
85
        );
86
87
        $validator = new Validator();
88
        $validator->addRule('bool', new Boolean());
89
        $validator->addRule('int', (new Number())->integer());
90
        $validator->addRule('int', (new Number())->integer()->min(44));
91
92
        $results = $validator->validate($dataObject);
93
94
        $this->assertTrue($results->getResult('bool')->isValid());
95
96
        $intResult = $results->getResult('int');
97
        $this->assertFalse($intResult->isValid());
98
        $this->assertCount(1, $intResult->getErrors());
99
    }
100
}
101