1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Helper; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Yiisoft\Validator\Rule\Boolean; |
10
|
|
|
use Yiisoft\Validator\Rule\Number; |
11
|
|
|
use Yiisoft\Validator\Helper\RulesDumper; |
12
|
|
|
use Yiisoft\Validator\Tests\Support\Data\IteratorWithBooleanKey; |
13
|
|
|
use Yiisoft\Validator\Tests\Support\Rule\RuleWithoutOptions; |
14
|
|
|
|
15
|
|
|
final class RulesDumperTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function asArrayDataProvider(): array |
18
|
|
|
{ |
19
|
|
|
return [ |
20
|
|
|
[ |
21
|
|
|
[ |
22
|
|
|
'attributeName' => [ |
23
|
|
|
$rule = new Number( |
24
|
|
|
asInteger: true, |
25
|
|
|
min: 10, |
26
|
|
|
max: 100, |
27
|
|
|
tooSmallMessage: 'Value must be greater than 10.', |
28
|
|
|
tooBigMessage: 'Value must be no greater than 100.', |
29
|
|
|
skipOnEmpty: true, |
30
|
|
|
skipOnError: true |
31
|
|
|
), |
32
|
|
|
(fn () => yield from [$rule, $rule])(), |
33
|
|
|
], |
34
|
|
|
], |
35
|
|
|
[ |
36
|
|
|
'attributeName' => [ |
37
|
|
|
$dump = [ |
38
|
|
|
'number', |
39
|
|
|
'asInteger' => true, |
40
|
|
|
'min' => 10, |
41
|
|
|
'max' => 100, |
42
|
|
|
'incorrectInputMessage' => [ |
43
|
|
|
'template' => 'The allowed types are integer, float and string.', |
44
|
|
|
'parameters' => [], |
45
|
|
|
], |
46
|
|
|
'notNumberMessage' => [ |
47
|
|
|
'template' => 'Value must be an integer.', |
48
|
|
|
'parameters' => [], |
49
|
|
|
], |
50
|
|
|
'tooBigMessage' => [ |
51
|
|
|
'template' => 'Value must be no greater than 100.', |
52
|
|
|
'parameters' => ['max' => 100], |
53
|
|
|
], |
54
|
|
|
'tooSmallMessage' => [ |
55
|
|
|
'template' => 'Value must be greater than 10.', |
56
|
|
|
'parameters' => ['min' => 10], |
57
|
|
|
], |
58
|
|
|
'skipOnEmpty' => true, |
59
|
|
|
'skipOnError' => true, |
60
|
|
|
'integerPattern' => '/^\s*[+-]?\d+\s*$/', |
61
|
|
|
'numberPattern' => '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/', |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
$dump, |
65
|
|
|
$dump, |
66
|
|
|
], |
67
|
|
|
], |
68
|
|
|
], |
69
|
|
|
], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @dataProvider asArrayDataProvider |
75
|
|
|
*/ |
76
|
|
|
public function testAsArray($rules, array $expected): void |
77
|
|
|
{ |
78
|
|
|
$dumper = new RulesDumper(); |
79
|
|
|
$result = $dumper->asArray($rules); |
80
|
|
|
|
81
|
|
|
$this->assertEquals($expected, $result); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testWrongRuleException(): void |
85
|
|
|
{ |
86
|
|
|
$dumper = new RulesDumper(); |
87
|
|
|
|
88
|
|
|
$this->expectException(InvalidArgumentException::class); |
89
|
|
|
|
90
|
|
|
$message = 'Every rule must implement "Yiisoft\Validator\RuleInterface". Type "string" given.'; |
91
|
|
|
$this->expectExceptionMessage($message); |
92
|
|
|
|
93
|
|
|
$dumper->asArray(['not a rule']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testWrongKeyException(): void |
97
|
|
|
{ |
98
|
|
|
$dumper = new RulesDumper(); |
99
|
|
|
|
100
|
|
|
$this->expectException(InvalidArgumentException::class); |
101
|
|
|
$this->expectExceptionMessage('An attribute can only have an integer or a string type. bool given.'); |
102
|
|
|
$dumper->asArray(new IteratorWithBooleanKey()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testRuleWithoutOptions(): void |
106
|
|
|
{ |
107
|
|
|
$dumper = new RulesDumper(); |
108
|
|
|
$rules = [ |
109
|
|
|
new Boolean(), |
110
|
|
|
new RuleWithoutOptions(), |
111
|
|
|
]; |
112
|
|
|
$expectedRules = [ |
113
|
|
|
[ |
114
|
|
|
'boolean', |
115
|
|
|
'trueValue' => '1', |
116
|
|
|
'falseValue' => '0', |
117
|
|
|
'strict' => false, |
118
|
|
|
'nonScalarMessage' => [ |
119
|
|
|
'template' => 'Value must be either "{true}" or "{false}".', |
120
|
|
|
'parameters' => [ |
121
|
|
|
'true' => '1', |
122
|
|
|
'false' => '0', |
123
|
|
|
], |
124
|
|
|
], |
125
|
|
|
'scalarMessage' => [ |
126
|
|
|
'template' => 'Value must be either "{true}" or "{false}".', |
127
|
|
|
'parameters' => [ |
128
|
|
|
'true' => '1', |
129
|
|
|
'false' => '0', |
130
|
|
|
], |
131
|
|
|
], |
132
|
|
|
'skipOnEmpty' => false, |
133
|
|
|
'skipOnError' => false, |
134
|
|
|
], |
135
|
|
|
[ |
136
|
|
|
'test', |
137
|
|
|
], |
138
|
|
|
]; |
139
|
|
|
|
140
|
|
|
$this->assertSame($expectedRules, $dumper->asArray($rules)); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|