|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Yiisoft\I18n\TranslatorInterface; |
|
9
|
|
|
use Yiisoft\Validator\DataSetInterface; |
|
10
|
|
|
use Yiisoft\Validator\Result; |
|
11
|
|
|
use Yiisoft\Validator\ValidatorFactory; |
|
12
|
|
|
|
|
13
|
|
|
class ValidatorFactoryTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testCreate() |
|
16
|
|
|
{ |
|
17
|
|
|
$validation = new ValidatorFactory(); |
|
18
|
|
|
|
|
19
|
|
|
$attribute = 'test'; |
|
20
|
|
|
$errorMessage = 'error message'; |
|
21
|
|
|
|
|
22
|
|
|
$validator = $validation->create( |
|
23
|
|
|
[ |
|
24
|
|
|
$attribute => [ |
|
25
|
|
|
static function () use ($errorMessage) { |
|
26
|
|
|
$result = new Result(); |
|
27
|
|
|
$result->addError($errorMessage); |
|
28
|
|
|
return $result; |
|
29
|
|
|
} |
|
30
|
|
|
] |
|
31
|
|
|
] |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
$result = $validator->validate($this->createDataSet([$attribute => ''])); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertSame($errorMessage, $result->getResult($attribute)->getErrors()[0]); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testCreateWithTranslator() |
|
40
|
|
|
{ |
|
41
|
|
|
$translatableMessage = 'test message'; |
|
42
|
|
|
$validation = new ValidatorFactory($this->createTranslatorMock($translatableMessage)); |
|
43
|
|
|
|
|
44
|
|
|
$attribute = 'test'; |
|
45
|
|
|
$validator = $validation->create( |
|
46
|
|
|
[ |
|
47
|
|
|
$attribute => [ |
|
48
|
|
|
static function () { |
|
49
|
|
|
$result = new Result(); |
|
50
|
|
|
$result->addError('error'); |
|
51
|
|
|
return $result; |
|
52
|
|
|
} |
|
53
|
|
|
] |
|
54
|
|
|
] |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$result = $validator->validate($this->createDataSet([$attribute => ''])); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertSame($translatableMessage, $result->getResult($attribute)->getErrors()[0]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
public function testCreateWithInvalidRule() |
|
64
|
|
|
{ |
|
65
|
|
|
$validation = new ValidatorFactory(); |
|
66
|
|
|
|
|
67
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
68
|
|
|
|
|
69
|
|
|
$attribute = 'test'; |
|
70
|
|
|
$validation->create( |
|
71
|
|
|
[ |
|
72
|
|
|
$attribute => [ |
|
73
|
|
|
'invalid rule' |
|
74
|
|
|
] |
|
75
|
|
|
] |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function createTranslatorMock(string $returnMessage = null): TranslatorInterface |
|
80
|
|
|
{ |
|
81
|
|
|
$translator = $this->createMock(TranslatorInterface::class); |
|
82
|
|
|
|
|
83
|
|
|
if ($returnMessage) { |
|
84
|
|
|
$translator |
|
85
|
|
|
->method('translate') |
|
86
|
|
|
->willReturn($returnMessage); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $translator; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function createDataSet(array $attributes): DataSetInterface |
|
93
|
|
|
{ |
|
94
|
|
|
return new class($attributes) implements DataSetInterface { |
|
95
|
|
|
private array $attributes; |
|
96
|
|
|
|
|
97
|
|
|
public function __construct(array $attributes) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->attributes = $attributes; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getAttributeValue(string $attribute) |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->hasAttribute($attribute) ? $this->attributes[$attribute] : null; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function hasAttribute(string $attribute): bool |
|
108
|
|
|
{ |
|
109
|
|
|
return array_key_exists($attribute, $this->attributes); |
|
110
|
|
|
} |
|
111
|
|
|
}; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|