Passed
Pull Request — master (#464)
by Alexander
04:02 queued 01:28
created

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