Passed
Pull Request — master (#542)
by Alexander
04:58 queued 02:22
created

EqualTest::testWithoutParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use Yiisoft\Validator\Rule\CompareType;
8
use Yiisoft\Validator\Rule\Equal;
9
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
10
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
11
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
12
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
13
14
final class EqualTest extends RuleTestCase
15
{
16
    use RuleWithOptionsTestTrait;
17
    use SkipOnErrorTestTrait;
18
    use WhenTestTrait;
19
20
    public function testGetName(): void
21
    {
22
        $rule = new Equal(1);
23
        $this->assertSame('equal', $rule->getName());
24
    }
25
26
    public function dataOptions(): array
27
    {
28
        return [
29
            [
30
                new Equal(1),
31
                [
32
                    'targetValue' => 1,
33
                    'targetAttribute' => null,
34
                    'incorrectInputMessage' => [
35
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
36
                        'parameters' => [
37
                            'targetValue' => 1,
38
                            'targetAttribute' => null,
39
                            'targetValueOrAttribute' => 1,
40
                        ],
41
                    ],
42
                    'incorrectDataSetTypeMessage' => [
43
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
44
                        'parameters' => [
45
                            'targetValue' => 1,
46
                            'targetAttribute' => null,
47
                            'targetValueOrAttribute' => 1,
48
                        ],
49
                    ],
50
                    'message' => [
51
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
52
                        'parameters' => [
53
                            'targetValue' => 1,
54
                            'targetAttribute' => null,
55
                            'targetValueOrAttribute' => 1,
56
                        ],
57
                    ],
58
                    'type' => 'string',
59
                    'operator' => '==',
60
                    'skipOnEmpty' => false,
61
                    'skipOnError' => false,
62
                ],
63
            ],
64
            [
65
                new Equal(1, type: CompareType::NUMBER),
66
                [
67
                    'targetValue' => 1,
68
                    'targetAttribute' => null,
69
                    'incorrectInputMessage' => [
70
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
71
                        'parameters' => [
72
                            'targetValue' => 1,
73
                            'targetAttribute' => null,
74
                            'targetValueOrAttribute' => 1,
75
                        ],
76
                    ],
77
                    'incorrectDataSetTypeMessage' => [
78
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
79
                        'parameters' => [
80
                            'targetValue' => 1,
81
                            'targetAttribute' => null,
82
                            'targetValueOrAttribute' => 1,
83
                        ],
84
                    ],
85
                    'message' => [
86
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
87
                        'parameters' => [
88
                            'targetValue' => 1,
89
                            'targetAttribute' => null,
90
                            'targetValueOrAttribute' => 1,
91
                        ],
92
                    ],
93
                    'type' => 'number',
94
                    'operator' => '==',
95
                    'skipOnEmpty' => false,
96
                    'skipOnError' => false,
97
                ],
98
            ],
99
            [
100
                new Equal('YES'),
101
                [
102
                    'targetValue' => 'YES',
103
                    'targetAttribute' => null,
104
                    'incorrectInputMessage' => [
105
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
106
                        'parameters' => [
107
                            'targetValue' => 'YES',
108
                            'targetAttribute' => null,
109
                            'targetValueOrAttribute' => 'YES',
110
                        ],
111
                    ],
112
                    'incorrectDataSetTypeMessage' => [
113
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
114
                        'parameters' => [
115
                            'targetValue' => 'YES',
116
                            'targetAttribute' => null,
117
                            'targetValueOrAttribute' => 'YES',
118
                        ],
119
                    ],
120
                    'message' => [
121
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
122
                        'parameters' => [
123
                            'targetValue' => 'YES',
124
                            'targetAttribute' => null,
125
                            'targetValueOrAttribute' => 'YES',
126
                        ],
127
                    ],
128
                    'type' => 'string',
129
                    'operator' => '==',
130
                    'skipOnEmpty' => false,
131
                    'skipOnError' => false,
132
                ],
133
            ],
134
            [
135
                new Equal('YES', strict: true),
136
                [
137
                    'targetValue' => 'YES',
138
                    'targetAttribute' => null,
139
                    'incorrectInputMessage' => [
140
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
141
                        'parameters' => [
142
                            'targetValue' => 'YES',
143
                            'targetAttribute' => null,
144
                            'targetValueOrAttribute' => 'YES',
145
                        ],
146
                    ],
147
                    'incorrectDataSetTypeMessage' => [
148
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
149
                        'parameters' => [
150
                            'targetValue' => 'YES',
151
                            'targetAttribute' => null,
152
                            'targetValueOrAttribute' => 'YES',
153
                        ],
154
                    ],
155
                    'message' => [
156
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
157
                        'parameters' => [
158
                            'targetValue' => 'YES',
159
                            'targetAttribute' => null,
160
                            'targetValueOrAttribute' => 'YES',
161
                        ],
162
                    ],
163
                    'type' => 'string',
164
                    'operator' => '===',
165
                    'skipOnEmpty' => false,
166
                    'skipOnError' => false,
167
                ],
168
            ],
169
            [
170
                new Equal('YES', skipOnEmpty: true),
171
                [
172
                    'targetValue' => 'YES',
173
                    'targetAttribute' => null,
174
                    'incorrectInputMessage' => [
175
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
176
                        'parameters' => [
177
                            'targetValue' => 'YES',
178
                            'targetAttribute' => null,
179
                            'targetValueOrAttribute' => 'YES',
180
                        ],
181
                    ],
182
                    'incorrectDataSetTypeMessage' => [
183
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
184
                        'parameters' => [
185
                            'targetValue' => 'YES',
186
                            'targetAttribute' => null,
187
                            'targetValueOrAttribute' => 'YES',
188
                        ],
189
                    ],
190
                    'message' => [
191
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
192
                        'parameters' => [
193
                            'targetValue' => 'YES',
194
                            'targetAttribute' => null,
195
                            'targetValueOrAttribute' => 'YES',
196
                        ],
197
                    ],
198
                    'type' => 'string',
199
                    'operator' => '==',
200
                    'skipOnEmpty' => true,
201
                    'skipOnError' => false,
202
                ],
203
            ],
204
            [
205
                new Equal(null, 'attribute'),
206
                [
207
                    'targetValue' => null,
208
                    'targetAttribute' => 'attribute',
209
                    'incorrectInputMessage' => [
210
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
211
                        'parameters' => [
212
                            'targetValue' => null,
213
                            'targetAttribute' => 'attribute',
214
                            'targetValueOrAttribute' => 'attribute',
215
                        ],
216
                    ],
217
                    'incorrectDataSetTypeMessage' => [
218
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
219
                        'parameters' => [
220
                            'targetValue' => null,
221
                            'targetAttribute' => 'attribute',
222
                            'targetValueOrAttribute' => 'attribute',
223
                        ],
224
                    ],
225
                    'message' => [
226
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
227
                        'parameters' => [
228
                            'targetValue' => null,
229
                            'targetAttribute' => 'attribute',
230
                            'targetValueOrAttribute' => 'attribute',
231
                        ],
232
                    ],
233
                    'type' => 'string',
234
                    'operator' => '==',
235
                    'skipOnEmpty' => false,
236
                    'skipOnError' => false,
237
                ],
238
            ],
239
            [
240
                new Equal(
241
                    targetAttribute: 'test',
242
                    incorrectInputMessage: 'Custom message 1.',
243
                    incorrectDataSetTypeMessage: 'Custom message 2.',
244
                    message: 'Custom message 3.',
245
                ),
246
                [
247
                    'targetValue' => null,
248
                    'targetAttribute' => 'test',
249
                    'incorrectInputMessage' => [
250
                        'template' => 'Custom message 1.',
251
                        'parameters' => [
252
                            'targetValue' => null,
253
                            'targetAttribute' => 'test',
254
                            'targetValueOrAttribute' => 'test',
255
                        ],
256
                    ],
257
                    'incorrectDataSetTypeMessage' => [
258
                        'template' => 'Custom message 2.',
259
                        'parameters' => [
260
                            'targetValue' => null,
261
                            'targetAttribute' => 'test',
262
                            'targetValueOrAttribute' => 'test',
263
                        ],
264
                    ],
265
                    'message' => [
266
                        'template' => 'Custom message 3.',
267
                        'parameters' => [
268
                            'targetValue' => null,
269
                            'targetAttribute' => 'test',
270
                            'targetValueOrAttribute' => 'test',
271
                        ],
272
                    ],
273
                    'type' => 'string',
274
                    'operator' => '==',
275
                    'skipOnEmpty' => false,
276
                    'skipOnError' => false,
277
                ],
278
            ],
279
        ];
280
    }
281
282
    public function dataValidationPassed(): array
283
    {
284
        return [
285
            [100, [new Equal(100)]],
286
            ['100', [new Equal(100)]],
287
        ];
288
    }
289
290
    public function dataValidationFailed(): array
291
    {
292
        $message = 'Value must be equal to "100".';
293
294
        return [
295
            [101, [new Equal(100)], ['' => [$message]]],
296
            [101, [new Equal(100, strict: true)], ['' => [$message]]],
297
            'custom error' => [101, [new Equal(100, message: 'Custom error')], ['' => ['Custom error']]],
298
        ];
299
    }
300
301
    public function testSkipOnError(): void
302
    {
303
        $this->testskipOnErrorInternal(new Equal(1), new Equal(1, skipOnError: true));
304
    }
305
306
    public function testWhen(): void
307
    {
308
        $when = static fn (mixed $value): bool => $value !== null;
309
        $this->testWhenInternal(new Equal(1), new Equal(1, when: $when));
310
    }
311
}
312