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
|
|
|
$validator->addRules( |
55
|
|
|
[ |
56
|
|
|
'bool' => [new Boolean()], |
57
|
|
|
'int' => [ |
58
|
|
|
(new Number())->integer(), |
59
|
|
|
(new Number())->integer()->min(44), |
60
|
|
|
static function ($value): Result { |
61
|
|
|
$result = new Result(); |
62
|
|
|
if ($value !== 42) { |
63
|
|
|
$result->addError('Value should be 42!'); |
64
|
|
|
} |
65
|
|
|
return $result; |
66
|
|
|
} |
67
|
|
|
], |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$results = $validator->validate($dataObject); |
72
|
|
|
|
73
|
|
|
$this->assertTrue($results->getResult('bool')->isValid()); |
74
|
|
|
|
75
|
|
|
$intResult = $results->getResult('int'); |
76
|
|
|
$this->assertFalse($intResult->isValid()); |
77
|
|
|
$this->assertCount(1, $intResult->getErrors()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testAddingRulesOneByOne(): void |
81
|
|
|
{ |
82
|
|
|
$dataObject = $this->getDataObject( |
83
|
|
|
[ |
84
|
|
|
'bool' => true, |
85
|
|
|
'int' => 42, |
86
|
|
|
] |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$validator = new Validator(); |
90
|
|
|
$validator->addRule('bool', new Boolean()); |
91
|
|
|
$validator->addRule('int', (new Number())->integer()); |
92
|
|
|
$validator->addRule('int', (new Number())->integer()->min(44)); |
93
|
|
|
|
94
|
|
|
$results = $validator->validate($dataObject); |
95
|
|
|
|
96
|
|
|
$this->assertTrue($results->getResult('bool')->isValid()); |
97
|
|
|
|
98
|
|
|
$intResult = $results->getResult('int'); |
99
|
|
|
$this->assertFalse($intResult->isValid()); |
100
|
|
|
$this->assertCount(1, $intResult->getErrors()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|