1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
6
|
|
|
|
7
|
|
|
use ArrayObject; |
8
|
|
|
use Yiisoft\Validator\DataSet\SingleValueDataSet; |
9
|
|
|
use Yiisoft\Validator\Rule\Subset; |
10
|
|
|
use Yiisoft\Validator\Rule\SubsetHandler; |
11
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait; |
12
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; |
13
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; |
14
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; |
15
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; |
16
|
|
|
|
17
|
|
|
final class SubsetTest extends RuleTestCase |
18
|
|
|
{ |
19
|
|
|
use DifferentRuleInHandlerTestTrait; |
20
|
|
|
use RuleWithOptionsTestTrait; |
21
|
|
|
use SkipOnErrorTestTrait; |
22
|
|
|
use WhenTestTrait; |
23
|
|
|
|
24
|
|
|
public function testGetName(): void |
25
|
|
|
{ |
26
|
|
|
$rule = new Subset([]); |
27
|
|
|
$this->assertSame('subset', $rule->getName()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function dataOptions(): array |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
[ |
34
|
|
|
new Subset([]), |
35
|
|
|
[ |
36
|
|
|
'values' => [], |
37
|
|
|
'strict' => false, |
38
|
|
|
'iterableMessage' => [ |
39
|
|
|
'template' => 'Value must be iterable.', |
40
|
|
|
'parameters' => [], |
41
|
|
|
], |
42
|
|
|
'subsetMessage' => [ |
43
|
|
|
'template' => 'Values must be ones of {values}.', |
44
|
|
|
'parameters' => [], |
45
|
|
|
], |
46
|
|
|
'skipOnEmpty' => false, |
47
|
|
|
'skipOnError' => false, |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function dataValidationPassed(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
[[], [new Subset(range(1, 10))]], |
57
|
|
|
[[1, 2, 3, 4, 5], [new Subset(range(1, 10))]], |
58
|
|
|
[[6, 7, 8, 9, 10], [new Subset(range(1, 10))]], |
59
|
|
|
[['1', '2', '3', 4, 5, 6], [new Subset(range(1, 10))]], |
60
|
|
|
|
61
|
|
|
[['a', 'b'], [new Subset(['a', 'b', 'c'])]], |
62
|
|
|
[new SingleValueDataSet(new ArrayObject(['a', 'b'])), [new Subset(new ArrayObject(['a', 'b', 'c']))]], |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function dataValidationFailed(): array |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
'non-iterable' => [ |
70
|
|
|
1, |
71
|
|
|
[new Subset([1, 2, 3])], |
72
|
|
|
['' => ['Value must be iterable.']], |
73
|
|
|
], |
74
|
|
|
'custom non-iterable message' => [ |
75
|
|
|
1, |
76
|
|
|
[new Subset([1, 2, 3], iterableMessage: 'Custom non-iterable message.')], |
77
|
|
|
['' => ['Custom non-iterable message.']], |
78
|
|
|
], |
79
|
|
|
'custom non-iterable message with parameters' => [ |
80
|
|
|
1, |
81
|
|
|
[new Subset([1, 2, 3], iterableMessage: 'Attribute - {attribute}, type - {type}.')], |
82
|
|
|
['' => ['Attribute - , type - int.']], |
83
|
|
|
], |
84
|
|
|
'custom non-iterable message with parameters, attribute set' => [ |
85
|
|
|
['data' => 1], |
86
|
|
|
['data' => new Subset([1, 2, 3], iterableMessage: 'Attribute - {attribute}, type - {type}.')], |
87
|
|
|
['data' => ['Attribute - data, type - int.']], |
88
|
|
|
], |
89
|
|
|
[ |
90
|
|
|
[0, 1, 2], |
91
|
|
|
[new Subset(range(1, 10))], |
92
|
|
|
['' => ['Values must be ones of "1", "2", "3", "4", "5", "6", "7", "8", "9", "10".']], |
93
|
|
|
], |
94
|
|
|
[ |
95
|
|
|
[10, 11, 12], |
96
|
|
|
[new Subset(range(1, 10))], |
97
|
|
|
['' => ['Values must be ones of "1", "2", "3", "4", "5", "6", "7", "8", "9", "10".']], |
98
|
|
|
], |
99
|
|
|
'iterator as a value' => [ |
100
|
|
|
new SingleValueDataSet(new ArrayObject(['c', 'd'])), |
101
|
|
|
[new Subset(new ArrayObject(['a', 'b', 'c']))], |
102
|
|
|
['' => ['Values must be ones of "a", "b", "c".']], |
103
|
|
|
], |
104
|
|
|
'custom message' => [ |
105
|
|
|
['' => ['c']], |
106
|
|
|
['' => new Subset(['a', 'b'], subsetMessage: 'Custom message.')], |
107
|
|
|
['' => ['Custom message.']], |
108
|
|
|
], |
109
|
|
|
'custom subset message with parameters' => [ |
110
|
|
|
['' => ['c']], |
111
|
|
|
['' => new Subset(['a', 'b'], subsetMessage: 'Attribute - {attribute}, values - {values}.')], |
112
|
|
|
['' => ['Attribute - , values - "a", "b".']], |
113
|
|
|
], |
114
|
|
|
'custom subset message with parameters, attribute set' => [ |
115
|
|
|
['data' => ['c']], |
116
|
|
|
['data' => new Subset(['a', 'b'], subsetMessage: 'Attribute - {attribute}, values - {values}.')], |
117
|
|
|
['data' => ['Attribute - data, values - "a", "b".']], |
118
|
|
|
], |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testSkipOnError(): void |
123
|
|
|
{ |
124
|
|
|
$this->testSkipOnErrorInternal(new Subset([]), new Subset([], skipOnError: true)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testWhen(): void |
128
|
|
|
{ |
129
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
130
|
|
|
$this->testWhenInternal(new Subset([]), new Subset([], when: $when)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
134
|
|
|
{ |
135
|
|
|
return [Subset::class, SubsetHandler::class]; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|