Passed
Pull Request — master (#519)
by
unknown
03:01
created

CompareTest.php$0 ➔ getSource()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use InvalidArgumentException;
8
use stdClass;
9
use Yiisoft\Validator\DataWrapperInterface;
10
use Yiisoft\Validator\Rule\Compare;
11
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
12
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
13
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
14
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
15
16
final class CompareTest extends RuleTestCase
17
{
18
    use RuleWithOptionsTestTrait;
19
    use SkipOnErrorTestTrait;
20
    use WhenTestTrait;
21
22
    public function testInitWithWrongOperator(): void
23
    {
24
        $this->expectException(InvalidArgumentException::class);
25
        $message = 'Operator "=" is not supported. The valid operators are: "==", "===", "!=", "!==", ">", ">=", ' .
26
            '"<", "<=".';
27
        $this->expectExceptionMessage($message);
28
        new Compare(operator: '=');
29
    }
30
31
    public function testGetName(): void
32
    {
33
        $rule = new Compare();
34
        $this->assertSame('compare', $rule->getName());
35
    }
36
37
    public function dataOptions(): array
38
    {
39
        return [
40
            [
41
                new Compare(1),
42
                [
43
                    'targetValue' => 1,
44
                    'targetAttribute' => null,
45
                    'incorrectInputMessage' => [
46
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
47
                        'parameters' => [
48
                            'targetValue' => 1,
49
                            'targetAttribute' => null,
50
                            'targetValueOrAttribute' => 1,
51
                        ],
52
                    ],
53
                    'incorrectDataSetTypeMessage' => [
54
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
55
                        'parameters' => [
56
                            'targetValue' => 1,
57
                            'targetAttribute' => null,
58
                            'targetValueOrAttribute' => 1,
59
                        ],
60
                    ],
61
                    'message' => [
62
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
63
                        'parameters' => [
64
                            'targetValue' => 1,
65
                            'targetAttribute' => null,
66
                            'targetValueOrAttribute' => 1,
67
                        ],
68
                    ],
69
                    'type' => 'string',
70
                    'operator' => '==',
71
                    'skipOnEmpty' => false,
72
                    'skipOnError' => false,
73
                ],
74
            ],
75
            [
76
                new Compare(1, type: Compare::TYPE_NUMBER),
77
                [
78
                    'targetValue' => 1,
79
                    'targetAttribute' => null,
80
                    'incorrectInputMessage' => [
81
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
82
                        'parameters' => [
83
                            'targetValue' => 1,
84
                            'targetAttribute' => null,
85
                            'targetValueOrAttribute' => 1,
86
                        ],
87
                    ],
88
                    'incorrectDataSetTypeMessage' => [
89
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
90
                        'parameters' => [
91
                            'targetValue' => 1,
92
                            'targetAttribute' => null,
93
                            'targetValueOrAttribute' => 1,
94
                        ],
95
                    ],
96
                    'message' => [
97
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
98
                        'parameters' => [
99
                            'targetValue' => 1,
100
                            'targetAttribute' => null,
101
                            'targetValueOrAttribute' => 1,
102
                        ],
103
                    ],
104
                    'type' => 'number',
105
                    'operator' => '==',
106
                    'skipOnEmpty' => false,
107
                    'skipOnError' => false,
108
                ],
109
            ],
110
            [
111
                new Compare(1, type: Compare::TYPE_NUMBER, operator: '>='),
112
                [
113
                    'targetValue' => 1,
114
                    'targetAttribute' => null,
115
                    'incorrectInputMessage' => [
116
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
117
                        'parameters' => [
118
                            'targetValue' => 1,
119
                            'targetAttribute' => null,
120
                            'targetValueOrAttribute' => 1,
121
                        ],
122
                    ],
123
                    'incorrectDataSetTypeMessage' => [
124
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
125
                        'parameters' => [
126
                            'targetValue' => 1,
127
                            'targetAttribute' => null,
128
                            'targetValueOrAttribute' => 1,
129
                        ],
130
                    ],
131
                    'message' => [
132
                        'template' => 'Value must be greater than or equal to "{targetValueOrAttribute}".',
133
                        'parameters' => [
134
                            'targetValue' => 1,
135
                            'targetAttribute' => null,
136
                            'targetValueOrAttribute' => 1,
137
                        ],
138
                    ],
139
                    'type' => 'number',
140
                    'operator' => '>=',
141
                    'skipOnEmpty' => false,
142
                    'skipOnError' => false,
143
                ],
144
            ],
145
            [
146
                new Compare('YES'),
147
                [
148
                    'targetValue' => 'YES',
149
                    'targetAttribute' => null,
150
                    'incorrectInputMessage' => [
151
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
152
                        'parameters' => [
153
                            'targetValue' => 'YES',
154
                            'targetAttribute' => null,
155
                            'targetValueOrAttribute' => 'YES',
156
                        ],
157
                    ],
158
                    'incorrectDataSetTypeMessage' => [
159
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
160
                        'parameters' => [
161
                            'targetValue' => 'YES',
162
                            'targetAttribute' => null,
163
                            'targetValueOrAttribute' => 'YES',
164
                        ],
165
                    ],
166
                    'message' => [
167
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
168
                        'parameters' => [
169
                            'targetValue' => 'YES',
170
                            'targetAttribute' => null,
171
                            'targetValueOrAttribute' => 'YES',
172
                        ],
173
                    ],
174
                    'type' => 'string',
175
                    'operator' => '==',
176
                    'skipOnEmpty' => false,
177
                    'skipOnError' => false,
178
                ],
179
            ],
180
            [
181
                new Compare('YES', skipOnEmpty: true),
182
                [
183
                    'targetValue' => 'YES',
184
                    'targetAttribute' => null,
185
                    'incorrectInputMessage' => [
186
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
187
                        'parameters' => [
188
                            'targetValue' => 'YES',
189
                            'targetAttribute' => null,
190
                            'targetValueOrAttribute' => 'YES',
191
                        ],
192
                    ],
193
                    'incorrectDataSetTypeMessage' => [
194
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
195
                        'parameters' => [
196
                            'targetValue' => 'YES',
197
                            'targetAttribute' => null,
198
                            'targetValueOrAttribute' => 'YES',
199
                        ],
200
                    ],
201
                    'message' => [
202
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
203
                        'parameters' => [
204
                            'targetValue' => 'YES',
205
                            'targetAttribute' => null,
206
                            'targetValueOrAttribute' => 'YES',
207
                        ],
208
                    ],
209
                    'type' => 'string',
210
                    'operator' => '==',
211
                    'skipOnEmpty' => true,
212
                    'skipOnError' => false,
213
                ],
214
            ],
215
            [
216
                new Compare('YES', operator: '!=='),
217
                [
218
                    'targetValue' => 'YES',
219
                    'targetAttribute' => null,
220
                    'incorrectInputMessage' => [
221
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
222
                        'parameters' => [
223
                            'targetValue' => 'YES',
224
                            'targetAttribute' => null,
225
                            'targetValueOrAttribute' => 'YES',
226
                        ],
227
                    ],
228
                    'incorrectDataSetTypeMessage' => [
229
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
230
                        'parameters' => [
231
                            'targetValue' => 'YES',
232
                            'targetAttribute' => null,
233
                            'targetValueOrAttribute' => 'YES',
234
                        ],
235
                    ],
236
                    'message' => [
237
                        'template' => 'Value must not be equal to "{targetValueOrAttribute}".',
238
                        'parameters' => [
239
                            'targetValue' => 'YES',
240
                            'targetAttribute' => null,
241
                            'targetValueOrAttribute' => 'YES',
242
                        ],
243
                    ],
244
                    'type' => 'string',
245
                    'operator' => '!==',
246
                    'skipOnEmpty' => false,
247
                    'skipOnError' => false,
248
                ],
249
            ],
250
            [
251
                new Compare('YES', message: 'Custom message for {targetValueOrAttribute}.'),
252
                [
253
                    'targetValue' => 'YES',
254
                    'targetAttribute' => null,
255
                    'incorrectInputMessage' => [
256
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
257
                        'parameters' => [
258
                            'targetValue' => 'YES',
259
                            'targetAttribute' => null,
260
                            'targetValueOrAttribute' => 'YES',
261
                        ],
262
                    ],
263
                    'incorrectDataSetTypeMessage' => [
264
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
265
                        'parameters' => [
266
                            'targetValue' => 'YES',
267
                            'targetAttribute' => null,
268
                            'targetValueOrAttribute' => 'YES',
269
                        ],
270
                    ],
271
                    'message' => [
272
                        'template' => 'Custom message for {targetValueOrAttribute}.',
273
                        'parameters' => [
274
                            'targetValue' => 'YES',
275
                            'targetAttribute' => null,
276
                            'targetValueOrAttribute' => 'YES',
277
                        ],
278
                    ],
279
                    'type' => 'string',
280
                    'operator' => '==',
281
                    'skipOnEmpty' => false,
282
                    'skipOnError' => false,
283
                ],
284
            ],
285
            [
286
                new Compare(null, 'test'),
287
                [
288
                    'targetValue' => null,
289
                    'targetAttribute' => 'test',
290
                    'incorrectInputMessage' => [
291
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
292
                        'parameters' => [
293
                            'targetValue' => null,
294
                            'targetAttribute' => 'test',
295
                            'targetValueOrAttribute' => 'test',
296
                        ],
297
                    ],
298
                    'incorrectDataSetTypeMessage' => [
299
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
300
                        'parameters' => [
301
                            'targetValue' => null,
302
                            'targetAttribute' => 'test',
303
                            'targetValueOrAttribute' => 'test',
304
                        ],
305
                    ],
306
                    'message' => [
307
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
308
                        'parameters' => [
309
                            'targetValue' => null,
310
                            'targetAttribute' => 'test',
311
                            'targetValueOrAttribute' => 'test',
312
                        ],
313
                    ],
314
                    'type' => 'string',
315
                    'operator' => '==',
316
                    'skipOnEmpty' => false,
317
                    'skipOnError' => false,
318
                ],
319
            ],
320
            [
321
                new Compare(
322
                    null,
323
                    'test',
324
                    incorrectInputMessage: 'Custom message 1.',
325
                    incorrectDataSetTypeMessage: 'Custom message 2.',
326
                    message: 'Custom message 3.',
327
                ),
328
                [
329
                    'targetValue' => null,
330
                    'targetAttribute' => 'test',
331
                    'incorrectInputMessage' => [
332
                        'template' => 'Custom message 1.',
333
                        'parameters' => [
334
                            'targetValue' => null,
335
                            'targetAttribute' => 'test',
336
                            'targetValueOrAttribute' => 'test',
337
                        ],
338
                    ],
339
                    'incorrectDataSetTypeMessage' => [
340
                        'template' => 'Custom message 2.',
341
                        'parameters' => [
342
                            'targetValue' => null,
343
                            'targetAttribute' => 'test',
344
                            'targetValueOrAttribute' => 'test',
345
                        ],
346
                    ],
347
                    'message' => [
348
                        'template' => 'Custom message 3.',
349
                        'parameters' => [
350
                            'targetValue' => null,
351
                            'targetAttribute' => 'test',
352
                            'targetValueOrAttribute' => 'test',
353
                        ],
354
                    ],
355
                    'type' => 'string',
356
                    'operator' => '==',
357
                    'skipOnEmpty' => false,
358
                    'skipOnError' => false,
359
                ],
360
            ],
361
            [
362
                new Compare(1, 'test'),
363
                [
364
                    'targetValue' => 1,
365
                    'targetAttribute' => 'test',
366
                    'incorrectInputMessage' => [
367
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
368
                        'parameters' => [
369
                            'targetValue' => 1,
370
                            'targetAttribute' => 'test',
371
                            'targetValueOrAttribute' => 1,
372
                        ],
373
                    ],
374
                    'incorrectDataSetTypeMessage' => [
375
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
376
                        'parameters' => [
377
                            'targetValue' => 1,
378
                            'targetAttribute' => 'test',
379
                            'targetValueOrAttribute' => 1,
380
                        ],
381
                    ],
382
                    'message' => [
383
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
384
                        'parameters' => [
385
                            'targetValue' => 1,
386
                            'targetAttribute' => 'test',
387
                            'targetValueOrAttribute' => 1,
388
                        ],
389
                    ],
390
                    'type' => 'string',
391
                    'operator' => '==',
392
                    'skipOnEmpty' => false,
393
                    'skipOnError' => false,
394
                ],
395
            ],
396
        ];
397
    }
398
399
    public function dataValidationPassed(): array
400
    {
401
        return [
402
            [null, [new Compare('')]],
403
            [null, [new Compare('', operator: '===')]],
404
405
            [100, [new Compare(100)]],
406
            [['attribute' => 100, 'number' => 100], ['number' => new Compare(null, 'attribute')]],
407
            ['100', [new Compare(100)]],
408
409
            [100, [new Compare(100, operator: '===')]],
410
            ['100', [new Compare(100, operator: '===')]],
411
            [100.0, [new Compare(100, operator: '===')]],
412
413
            [100.00001, [new Compare(100, operator: '!=')]],
414
            [false, [new Compare(100, operator: '!=')]],
415
416
            [false, [new Compare(100, operator: '!==')]],
417
418
            [101, [new Compare(100, operator: '>')]],
419
420
            [100, [new Compare(100, operator: '>=')]],
421
            [101, [new Compare(100, operator: '>=')]],
422
            [99, [new Compare(100, operator: '<')]],
423
424
            [100, [new Compare(100, operator: '<=')]],
425
            [99, [new Compare(100, operator: '<=')]],
426
            [['attribute' => 100, 'number' => 99], ['number' => new Compare(null, 'attribute', operator: '<=')]],
427
428
            ['100.50', [new Compare('100.5', type: Compare::TYPE_NUMBER)]],
429
            ['100.50', [new Compare('100.5', type: Compare::TYPE_NUMBER, operator: '===')]],
430
        ];
431
    }
432
433
    public function dataValidationFailed(): array
434
    {
435
        $incorrectDataSet = new class () implements DataWrapperInterface {
436
            public function getAttributeValue(string $attribute): mixed
437
            {
438
                return new stdClass();
439
            }
440
441
            public function getData(): ?array
442
            {
443
                return null;
444
            }
445
446
            public function getSource(): mixed
447
            {
448
                return false;
449
            }
450
451
            public function hasAttribute(string $attribute): bool
452
            {
453
                return false;
454
            }
455
        };
456
        $messageEqual = 'Value must be equal to "100".';
457
        $messageNotEqual = 'Value must not be equal to "100".';
458
        $messageGreaterThan = 'Value must be greater than "100".';
459
        $messageGreaterOrEqualThan = 'Value must be greater than or equal to "100".';
460
        $messageLessThan = 'Value must be less than "100".';
461
        $messageLessOrEqualThan = 'Value must be less than or equal to "100".';
462
463
        return [
464
            'incorrect input' => [
465
                [],
466
                [new Compare(false)],
467
                ['' => ['The allowed types are integer, float, string, boolean and null.']],
468
            ],
469
            'custom incorrect input message' => [
470
                [],
471
                [new Compare(false, incorrectInputMessage: 'Custom incorrect input message.')],
472
                ['' => ['Custom incorrect input message.']],
473
            ],
474
            'custom incorrect input message with parameters' => [
475
                [],
476
                [new Compare(false, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')],
477
                ['' => ['Attribute - , type - array.']],
478
            ],
479
            'custom incorrect input message with parameters, attribute set' => [
480
                ['data' => []],
481
                ['data' => new Compare(false, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')],
482
                ['data' => ['Attribute - data, type - array.']],
483
            ],
484
485
            'incorrect data set type' => [
486
                $incorrectDataSet,
487
                [new Compare(targetAttribute: 'test')],
488
                ['' => ['The attribute value returned from a custom data set must have a scalar type.']],
489
            ],
490
            'custom incorrect data set type message' => [
491
                $incorrectDataSet,
492
                [
493
                    new Compare(
494
                        targetAttribute: 'test',
495
                        incorrectDataSetTypeMessage: 'Custom incorrect data set type message.',
496
                    ),
497
                ],
498
                ['' => ['Custom incorrect data set type message.']],
499
            ],
500
            'custom incorrect data set type message with parameters' => [
501
                $incorrectDataSet,
502
                [
503
                    new Compare(
504
                        targetAttribute: 'test',
505
                        incorrectDataSetTypeMessage: 'Type - {type}.',
506
                    ),
507
                ],
508
                ['' => ['Type - stdClass.']],
509
            ],
510
511
            [null, [new Compare(0)], ['' => ['Value must be equal to "0".']]],
512
513
            [101, [new Compare(100)], ['' => [$messageEqual]]],
514
515
            [101, [new Compare(100, operator: '===')], ['' => [$messageEqual]]],
516
            [
517
                ['attribute' => 100, 'number' => 101],
518
                ['number' => new Compare(null, 'attribute', operator: '===')],
519
                ['number' => [$messageEqual]],
520
            ],
521
522
            [100, [new Compare(100, operator: '!=')], ['' => [$messageNotEqual]]],
523
            ['100', [new Compare(100, operator: '!=')], ['' => [$messageNotEqual]]],
524
            [100.0, [new Compare(100, operator: '!=')], ['' => [$messageNotEqual]]],
525
526
            [100, [new Compare(100, operator: '!==')], ['' => [$messageNotEqual]]],
527
            ['100', [new Compare(100, operator: '!==')], ['' => [$messageNotEqual]]],
528
            [100.0, [new Compare(100, operator: '!==')], ['' => [$messageNotEqual]]],
529
530
            [100, [new Compare(100, operator: '>')], ['' => [$messageGreaterThan]]],
531
            [99, [new Compare(100, operator: '>')], ['' => [$messageGreaterThan]]],
532
533
            [99, [new Compare(100, operator: '>=')], ['' => [$messageGreaterOrEqualThan]]],
534
535
            [100, [new Compare(100, operator: '<')], ['' => [$messageLessThan]]],
536
            [101, [new Compare(100, operator: '<')], ['' => [$messageLessThan]]],
537
538
            [101, [new Compare(100, operator: '<=')], ['' => [$messageLessOrEqualThan]]],
539
            [
540
                ['attribute' => 100, 'number' => 101],
541
                ['number' => new Compare(null, 'attribute', operator: '<=')],
542
                ['number' => [$messageLessOrEqualThan]],
543
            ],
544
545
            'custom message' => [101, [new Compare(100, message: 'Custom message.')], ['' => ['Custom message.']]],
546
            'custom message with parameters, target value set' => [
547
                101,
548
                [
549
                    new Compare(
550
                        100,
551
                        message: 'Attribute - {attribute}, target value - {targetValue}, target attribute - ' .
552
                        '{targetAttribute}, target value or attribute - {targetValueOrAttribute}, value - {value}.',
553
                    ),
554
                ],
555
                [
556
                    '' => [
557
                        'Attribute - , target value - 100, target attribute - , target value or attribute - 100, ' .
558
                        'value - 101.',
559
                    ],
560
                ],
561
            ],
562
            'custom message with parameters, attribute and target attribute set' => [
563
                ['attribute' => 100, 'number' => 101],
564
                [
565
                    'number' => new Compare(
566
                        null,
567
                        'attribute',
568
                        message: 'Attribute - {attribute}, target value - {targetValue}, target attribute - ' .
569
                        '{targetAttribute}, target value or attribute - {targetValueOrAttribute}, value - {value}.',
570
                        operator: '===',
571
                    ),
572
                ],
573
                [
574
                    'number' => [
575
                        'Attribute - number, target value - , target attribute - attribute, target value or ' .
576
                        'attribute - 100, value - 101.',
577
                    ],
578
                ],
579
            ],
580
581
            ['100.50', [new Compare('100.5', operator: '===')], ['' => ['Value must be equal to "100.5".']]],
582
        ];
583
    }
584
585
    public function testSkipOnError(): void
586
    {
587
        $this->testSkipOnErrorInternal(new Compare(), new Compare(skipOnError: true));
588
    }
589
590
    public function testWhen(): void
591
    {
592
        $when = static fn (mixed $value): bool => $value !== null;
593
        $this->testWhenInternal(new Compare(), new Compare(when: $when));
594
    }
595
}
596