Completed
Push — master ( 17ecb7...725fdf )
by Alexander
15:14
created

php$0 ➔ testAddingRulesViaConstructor()   A

Complexity

Conditions 2

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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