Passed
Pull Request — master (#408)
by
unknown
02:42
created

IsTrueTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetName() 0 4 1
A testWhen() 0 4 1
A testSkipOnError() 0 3 1
A dataValidationPassed() 0 7 1
A getDifferentRuleInHandlerItems() 0 3 1
A dataOptions() 0 52 1
A dataValidationFailed() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use Yiisoft\Validator\Rule\IsTrue;
8
use Yiisoft\Validator\Rule\IsTrueHandler;
9
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
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 IsTrueTest extends RuleTestCase
16
{
17
    use DifferentRuleInHandlerTestTrait;
18
    use RuleWithOptionsTestTrait;
19
    use SkipOnErrorTestTrait;
20
    use WhenTestTrait;
21
22
    public function testGetName(): void
23
    {
24
        $rule = new IsTrue();
25
        $this->assertSame('isTrue', $rule->getName());
26
    }
27
28
    public function dataOptions(): array
29
    {
30
        return [
31
            [
32
                new IsTrue(),
33
                [
34
                    'trueValue' => '1',
35
                    'strict' => false,
36
                    'message' => [
37
                        'template' => 'The value must be "{true}".',
38
                        'parameters' => [
39
                            'true' => '1',
40
                        ],
41
                    ],
42
                    'skipOnEmpty' => false,
43
                    'skipOnError' => false,
44
                ],
45
            ],
46
            [
47
                new IsTrue(trueValue: true, strict: true),
48
                [
49
                    'trueValue' => true,
50
                    'strict' => true,
51
                    'message' => [
52
                        'template' => 'The value must be "{true}".',
53
                        'parameters' => [
54
                            'true' => 'true',
55
                        ],
56
                    ],
57
                    'skipOnEmpty' => false,
58
                    'skipOnError' => false,
59
                ],
60
            ],
61
            [
62
                new IsTrue(
63
                    trueValue: 'YES',
64
                    strict: true,
65
                    message: 'Custom message.',
66
                    skipOnEmpty: true,
67
                    skipOnError: true
68
                ),
69
                [
70
                    'trueValue' => 'YES',
71
                    'strict' => true,
72
                    'message' => [
73
                        'template' => 'Custom message.',
74
                        'parameters' => [
75
                            'true' => 'YES',
76
                        ],
77
                    ],
78
                    'skipOnEmpty' => true,
79
                    'skipOnError' => true,
80
                ],
81
            ],
82
        ];
83
    }
84
85
    public function dataValidationPassed(): array
86
    {
87
        return [
88
            [true, [new IsTrue()]],
89
            ['1', [new IsTrue()]],
90
            ['1', [new IsTrue(strict: true)]],
91
            [true, [new IsTrue(trueValue: true, strict: true)]],
92
        ];
93
    }
94
95
    public function dataValidationFailed(): array
96
    {
97
        return [
98
            ['5', [new IsTrue()], ['' => ['The value must be "1".']]],
99
            [null, [new IsTrue()], ['' => ['The value must be "1".']]],
100
            [[], [new IsTrue()], ['' => ['The value must be "1".']]],
101
            [true, [new IsTrue(strict: true)], ['' => ['The value must be "1".']]],
102
            ['1', [new IsTrue(trueValue: true, strict: true)], ['' => ['The value must be "true".']]],
103
            [[], [new IsTrue(trueValue: true, strict: true)], ['' => ['The value must be "true".']]],
104
105
            [false, [new IsTrue()], ['' => ['The value must be "1".']]],
106
            ['0', [new IsTrue()], ['' => ['The value must be "1".']]],
107
            ['0', [new IsTrue(strict: true)], ['' => ['The value must be "1".']]],
108
            [false, [new IsTrue(trueValue: true, strict: true)], ['' => ['The value must be "true".']]],
109
            'custom error' => [5, [new IsTrue(message: 'Custom error.')], ['' => ['Custom error.']]],
110
        ];
111
    }
112
113
    public function testSkipOnError(): void
114
    {
115
        $this->testSkipOnErrorInternal(new IsTrue(), new IsTrue(skipOnError: true));
116
    }
117
118
    public function testWhen(): void
119
    {
120
        $when = static fn (mixed $value): bool => $value !== null;
121
        $this->testWhenInternal(new IsTrue(), new IsTrue(when: $when));
122
    }
123
124
    protected function getDifferentRuleInHandlerItems(): array
125
    {
126
        return [IsTrue::class, IsTrueHandler::class];
127
    }
128
}
129