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

BoolValueTest::dataOptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 81
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 56
nc 1
nop 0
dl 0
loc 81
rs 8.9599
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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