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