1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Yiisoft\Validator\EmptyCriteria\NeverEmpty; |
9
|
|
|
use Yiisoft\Validator\Rule\Required; |
10
|
|
|
use Yiisoft\Validator\Rule\RequiredHandler; |
11
|
|
|
use Yiisoft\Validator\EmptyCriteria\WhenNull; |
12
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait; |
13
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; |
14
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; |
15
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; |
16
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; |
17
|
|
|
use Yiisoft\Validator\ValidationContext; |
18
|
|
|
|
19
|
|
|
final class RequiredTest extends RuleTestCase |
20
|
|
|
{ |
21
|
|
|
use DifferentRuleInHandlerTestTrait; |
22
|
|
|
use RuleWithOptionsTestTrait; |
23
|
|
|
use SkipOnErrorTestTrait; |
24
|
|
|
use WhenTestTrait; |
25
|
|
|
|
26
|
|
|
public function testDefaultValues(): void |
27
|
|
|
{ |
28
|
|
|
$rule = new Required(); |
29
|
|
|
|
30
|
|
|
$this->assertNull($rule->getEmptyCriteria()); |
31
|
|
|
$this->assertSame(RequiredHandler::class, $rule->getHandlerClassName()); |
32
|
|
|
$this->assertSame('Value cannot be blank.', $rule->getMessage()); |
33
|
|
|
$this->assertSame('required', $rule->getName()); |
34
|
|
|
$this->assertSame('Value not passed.', $rule->getNotPassedMessage()); |
35
|
|
|
$this->assertNull($rule->getWhen()); |
36
|
|
|
$this->assertFalse($rule->shouldSkipOnError()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function dataGetEmptyCriteria(): array |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
'skip on null' => [new WhenNull(), WhenNull::class], |
43
|
|
|
'closure' => [static fn () => false, Closure::class], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @dataProvider dataGetEmptyCriteria |
49
|
|
|
*/ |
50
|
|
|
public function testGetEmptyCriteria(?callable $callback, string $expectedCallbackClassName): void |
51
|
|
|
{ |
52
|
|
|
$rule = new Required(emptyCriteria: $callback); |
53
|
|
|
|
54
|
|
|
$this->assertInstanceOf($expectedCallbackClassName, $rule->getEmptyCriteria()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function dataOptions(): array |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
[ |
61
|
|
|
new Required(), |
62
|
|
|
[ |
63
|
|
|
'message' => [ |
64
|
|
|
'template' => 'Value cannot be blank.', |
65
|
|
|
'parameters' => [], |
66
|
|
|
], |
67
|
|
|
'notPassedMessage' => [ |
68
|
|
|
'template' => 'Value not passed.', |
69
|
|
|
'parameters' => [], |
70
|
|
|
], |
71
|
|
|
'skipOnError' => false, |
72
|
|
|
], |
73
|
|
|
], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function dataValidationPassed(): array |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
|
|
['not empty', [new Required()]], |
81
|
|
|
[['with', 'elements'], [new Required()]], |
82
|
|
|
'skip on null' => [ |
83
|
|
|
'', |
84
|
|
|
[new Required(emptyCriteria: new WhenNull())], |
85
|
|
|
], |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function dataValidationFailed(): array |
90
|
|
|
{ |
91
|
|
|
$singleMessageCannotBeBlank = ['' => ['Value cannot be blank.']]; |
92
|
|
|
|
93
|
|
|
return [ |
94
|
|
|
[null, [new Required()], $singleMessageCannotBeBlank], |
95
|
|
|
[[], [new Required()], $singleMessageCannotBeBlank], |
96
|
|
|
'custom empty callback' => [ |
97
|
|
|
'42', |
98
|
|
|
[new Required(emptyCriteria: static fn (mixed $value): bool => $value === '42')], |
99
|
|
|
$singleMessageCannotBeBlank, |
100
|
|
|
], |
101
|
|
|
'custom error' => [null, [new Required(message: 'Custom error')], ['' => ['Custom error']]], |
102
|
|
|
'empty after trimming' => [' ', [new Required()], $singleMessageCannotBeBlank], |
103
|
|
|
'custom message with attribute' => [ |
104
|
|
|
['name' => ''], |
105
|
|
|
['name' => new Required(message: '{attribute} is bad.')], |
106
|
|
|
['name' => ['name is bad.']], |
107
|
|
|
], |
108
|
|
|
'custom not passed message with attribute' => [ |
109
|
|
|
[], |
110
|
|
|
['name' => new Required(notPassedMessage: 'Field "{attribute}" is not passed.')], |
111
|
|
|
['name' => ['Field "name" is not passed.']], |
112
|
|
|
], |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testSkipOnError(): void |
117
|
|
|
{ |
118
|
|
|
$this->testSkipOnErrorInternal(new Required(), new Required(skipOnError: true)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testWhen(): void |
122
|
|
|
{ |
123
|
|
|
$when = static fn(mixed $value): bool => $value !== null; |
124
|
|
|
$this->testWhenInternal(new Required(), new Required(when: $when)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testDefaultEmptyCriteria(): void |
128
|
|
|
{ |
129
|
|
|
$handler = new RequiredHandler(defaultEmptyCriteria: new NeverEmpty()); |
130
|
|
|
|
131
|
|
|
$result = $handler->validate('', new Required(), new ValidationContext()); |
132
|
|
|
|
133
|
|
|
$this->assertTrue($result->isValid()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
137
|
|
|
{ |
138
|
|
|
return [Required::class, RequiredHandler::class]; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|