Passed
Pull Request — master (#491)
by
unknown
21:15 queued 18:50
created

TrueValueTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 109
dl 0
loc 194
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testWhen() 0 4 1
A dataValidationPassed() 0 7 1
A dataValidationFailed() 0 77 1
A dataOptions() 0 71 1
A testGetName() 0 4 1
A testSkipOnError() 0 3 1
A getDifferentRuleInHandlerItems() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use Yiisoft\Validator\Rule\TrueValue;
8
use Yiisoft\Validator\Rule\TrueValueHandler;
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 TrueValueTest 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 TrueValue();
25
        $this->assertSame('isTrue', $rule->getName());
26
    }
27
28
    public function dataOptions(): array
29
    {
30
        return [
31
            [
32
                new TrueValue(),
33
                [
34
                    'trueValue' => '1',
35
                    'strict' => false,
36
                    'messageWithType' => [
37
                        'template' => 'The value must be "{true}".',
38
                        'parameters' => [
39
                            'true' => '1',
40
                        ],
41
                    ],
42
                    'messageWithValue' => [
43
                        'template' => 'The value must be "{true}".',
44
                        'parameters' => [
45
                            'true' => '1',
46
                        ],
47
                    ],
48
                    'skipOnEmpty' => false,
49
                    'skipOnError' => false,
50
                ],
51
            ],
52
            [
53
                new TrueValue(trueValue: true, strict: true),
54
                [
55
                    'trueValue' => true,
56
                    'strict' => true,
57
                    'messageWithType' => [
58
                        'template' => 'The value must be "{true}".',
59
                        'parameters' => [
60
                            'true' => 'true',
61
                        ],
62
                    ],
63
                    'messageWithValue' => [
64
                        'template' => 'The value must be "{true}".',
65
                        'parameters' => [
66
                            'true' => 'true',
67
                        ],
68
                    ],
69
                    'skipOnEmpty' => false,
70
                    'skipOnError' => false,
71
                ],
72
            ],
73
            [
74
                new TrueValue(
75
                    trueValue: 'YES',
76
                    strict: true,
77
                    incorrectInputMessage: 'Custom incorrect input message.',
78
                    message: 'Custom message.',
79
                    skipOnEmpty: true,
80
                    skipOnError: true
81
                ),
82
                [
83
                    'trueValue' => 'YES',
84
                    'strict' => true,
85
                    'messageWithType' => [
86
                        'template' => 'Custom incorrect input message.',
87
                        'parameters' => [
88
                            'true' => 'YES',
89
                        ],
90
                    ],
91
                    'messageWithValue' => [
92
                        'template' => 'Custom message.',
93
                        'parameters' => [
94
                            'true' => 'YES',
95
                        ],
96
                    ],
97
                    'skipOnEmpty' => true,
98
                    'skipOnError' => true,
99
                ],
100
            ],
101
        ];
102
    }
103
104
    public function dataValidationPassed(): array
105
    {
106
        return [
107
            [true, [new TrueValue()]],
108
            ['1', [new TrueValue()]],
109
            ['1', [new TrueValue(strict: true)]],
110
            [true, [new TrueValue(trueValue: true, strict: true)]],
111
        ];
112
    }
113
114
    public function dataValidationFailed(): array
115
    {
116
        return [
117
            ['5', [new TrueValue()], ['' => ['The value must be "1".']]],
118
            [null, [new TrueValue()], ['' => ['The value must be "1".']]],
119
            [[], [new TrueValue()], ['' => ['The value must be "1".']]],
120
            [true, [new TrueValue(strict: true)], ['' => ['The value must be "1".']]],
121
            ['1', [new TrueValue(trueValue: true, strict: true)], ['' => ['The value must be "true".']]],
122
            [[], [new TrueValue(trueValue: true, strict: true)], ['' => ['The value must be "true".']]],
123
124
            [false, [new TrueValue()], ['' => ['The value must be "1".']]],
125
            ['0', [new TrueValue()], ['' => ['The value must be "1".']]],
126
            ['0', [new TrueValue(strict: true)], ['' => ['The value must be "1".']]],
127
            [false, [new TrueValue(trueValue: true, strict: true)], ['' => ['The value must be "true".']]],
128
129
            'custom message' => [
130
                5,
131
                [new TrueValue(message: 'Custom error.')],
132
                ['' => ['Custom error.']],
133
            ],
134
            'custom message with parameters' => [
135
                5,
136
                [new TrueValue(message: 'Attribute - {attribute}, true - {true}, value - {value}.')],
137
                ['' => ['Attribute - , true - 1, value - 5.']],
138
            ],
139
            'custom message with parameters, custom true value, strict' => [
140
                5,
141
                [
142
                    new TrueValue(
143
                        trueValue: true,
144
                        strict: true,
145
                        message: 'Attribute - {attribute}, true - {true}, value - {value}.',
146
                    ),
147
                ],
148
                ['' => ['Attribute - , true - true, value - 5.']],
149
            ],
150
            'custom message with parameters, attribute set' => [
151
                ['data' => 5],
152
                [
153
                    'data' => new TrueValue(message: 'Attribute - {attribute}, true - {true}, value - {value}.'),
154
                ],
155
                ['data' => ['Attribute - data, true - 1, value - 5.']],
156
            ],
157
            'custom incorrect input message' => [
158
                [],
159
                [new TrueValue(incorrectInputMessage: 'Custom error.')],
160
                ['' => ['Custom error.']],
161
            ],
162
            'custom incorrect input message with parameters' => [
163
                [],
164
                [
165
                    new TrueValue(incorrectInputMessage: 'Attribute - {attribute}, true - {true}, type - {type}.'),
166
                ],
167
                ['' => ['Attribute - , true - 1, type - array.']],
168
            ],
169
            'custom incorrect input message with parameters, custom true and false values, strict' => [
170
                [],
171
                [
172
                    new TrueValue(
173
                        trueValue: true,
174
                        strict: true,
175
                        incorrectInputMessage: 'Attribute - {attribute}, true - {true}, type - {type}.',
176
                    ),
177
                ],
178
                ['' => ['Attribute - , true - true, type - array.']],
179
            ],
180
            'custom incorrect input message with parameters, attribute set' => [
181
                ['data' => []],
182
                [
183
                    'data' => new TrueValue(incorrectInputMessage: 'Attribute - {attribute}, true - {true}, type - {type}.'),
184
                ],
185
                ['data' => ['Attribute - data, true - 1, type - array.']],
186
            ],
187
            'custom incorrect input message, null' => [
188
                null,
189
                [new TrueValue(incorrectInputMessage: 'Attribute - {attribute}, true - {true}, type - {type}.'),],
190
                ['' => ['Attribute - , true - 1, type - null.']],
191
            ],
192
        ];
193
    }
194
195
    public function testSkipOnError(): void
196
    {
197
        $this->testSkipOnErrorInternal(new TrueValue(), new TrueValue(skipOnError: true));
198
    }
199
200
    public function testWhen(): void
201
    {
202
        $when = static fn (mixed $value): bool => $value !== null;
203
        $this->testWhenInternal(new TrueValue(), new TrueValue(when: $when));
204
    }
205
206
    protected function getDifferentRuleInHandlerItems(): array
207
    {
208
        return [TrueValue::class, TrueValueHandler::class];
209
    }
210
}
211