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