LengthTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 288
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 181
dl 0
loc 288
rs 10
c 2
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetName() 0 4 1
B dataOptions() 0 82 1
A testWhen() 0 4 1
A getDifferentRuleInHandlerItems() 0 3 1
A getRuleClass() 0 3 1
A testSkipOnError() 0 3 1
A dataValidationPassed() 0 36 1
B dataValidationFailed() 0 124 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use stdClass;
8
use Yiisoft\Validator\DataSet\SingleValueDataSet;
9
use Yiisoft\Validator\Rule\Length;
10
use Yiisoft\Validator\Rule\LengthHandler;
11
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
12
use Yiisoft\Validator\Tests\Rule\Base\CountableLimitTestTrait;
13
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
14
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
15
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
16
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
17
18
final class LengthTest extends RuleTestCase
19
{
20
    use CountableLimitTestTrait;
21
    use DifferentRuleInHandlerTestTrait;
22
    use RuleWithOptionsTestTrait;
23
    use SkipOnErrorTestTrait;
24
    use WhenTestTrait;
25
26
    public function testGetName(): void
27
    {
28
        $rule = new Length(min: 3);
29
        $this->assertSame(Length::class, $rule->getName());
30
    }
31
32
    public function dataOptions(): array
33
    {
34
        return [
35
            [
36
                new Length(min: 3),
37
                [
38
                    'min' => 3,
39
                    'max' => null,
40
                    'exactly' => null,
41
                    'lessThanMinMessage' => [
42
                        'template' => 'This value must contain at least {min, number} {min, plural, one{character} other{characters}}.',
43
                        'parameters' => ['min' => 3],
44
                    ],
45
                    'greaterThanMaxMessage' => [
46
                        'template' => 'This value must contain at most {max, number} {max, plural, one{character} other{characters}}.',
47
                        'parameters' => ['max' => null],
48
                    ],
49
                    'notExactlyMessage' => [
50
                        'template' => 'This value must contain exactly {exactly, number} {exactly, plural, one{character} other{characters}}.',
51
                        'parameters' => ['exactly' => null],
52
                    ],
53
                    'incorrectInputMessage' => [
54
                        'template' => 'The value must be a string.',
55
                        'parameters' => [],
56
                    ],
57
                    'encoding' => 'UTF-8',
58
                    'skipOnEmpty' => false,
59
                    'skipOnError' => false,
60
                ],
61
            ],
62
            [
63
                new Length(max: 3),
64
                [
65
                    'min' => null,
66
                    'max' => 3,
67
                    'exactly' => null,
68
                    'lessThanMinMessage' => [
69
                        'template' => 'This value must contain at least {min, number} {min, plural, one{character} other{characters}}.',
70
                        'parameters' => ['min' => null],
71
                    ],
72
                    'greaterThanMaxMessage' => [
73
                        'template' => 'This value must contain at most {max, number} {max, plural, one{character} other{characters}}.',
74
                        'parameters' => ['max' => 3],
75
                    ],
76
                    'notExactlyMessage' => [
77
                        'template' => 'This value must contain exactly {exactly, number} {exactly, plural, one{character} other{characters}}.',
78
                        'parameters' => ['exactly' => null],
79
                    ],
80
                    'incorrectInputMessage' => [
81
                        'template' => 'The value must be a string.',
82
                        'parameters' => [],
83
                    ],
84
                    'encoding' => 'UTF-8',
85
                    'skipOnEmpty' => false,
86
                    'skipOnError' => false,
87
                ],
88
            ],
89
            [
90
                new Length(min: 3, max: 4, encoding: 'windows-1251'),
91
                [
92
                    'min' => 3,
93
                    'max' => 4,
94
                    'exactly' => null,
95
                    'lessThanMinMessage' => [
96
                        'template' => 'This value must contain at least {min, number} {min, plural, one{character} other{characters}}.',
97
                        'parameters' => ['min' => 3],
98
                    ],
99
                    'greaterThanMaxMessage' => [
100
                        'template' => 'This value must contain at most {max, number} {max, plural, one{character} other{characters}}.',
101
                        'parameters' => ['max' => 4],
102
                    ],
103
                    'notExactlyMessage' => [
104
                        'template' => 'This value must contain exactly {exactly, number} {exactly, plural, one{character} other{characters}}.',
105
                        'parameters' => ['exactly' => null],
106
                    ],
107
                    'incorrectInputMessage' => [
108
                        'template' => 'The value must be a string.',
109
                        'parameters' => [],
110
                    ],
111
                    'encoding' => 'windows-1251',
112
                    'skipOnEmpty' => false,
113
                    'skipOnError' => false,
114
                ],
115
            ],
116
        ];
117
    }
118
119
    public function dataValidationPassed(): array
120
    {
121
        return [
122
            [str_repeat('x', 25), [new Length(exactly: 25)]],
123
            [str_repeat('€', 25), [new Length(exactly: 25)]],
124
125
            [str_repeat('x', 125), [new Length(min: 25)]],
126
            [str_repeat('€', 25), [new Length(min: 25)]],
127
128
            [str_repeat('x', 25), [new Length(max: 25)]],
129
            [str_repeat('Ä', 24), [new Length(max: 25)]],
130
            ['', [new Length(max: 25)]],
131
132
            [str_repeat('x', 15), [new Length(min: 10, max: 25)]],
133
            [str_repeat('x', 10), [new Length(min: 10, max: 25)]],
134
            [str_repeat('x', 20), [new Length(min: 10, max: 25)]],
135
            [str_repeat('x', 25), [new Length(min: 10, max: 25)]],
136
137
            [str_repeat('x', 5), [new Length(min: 1)]],
138
            [str_repeat('x', 5), [new Length(max: 100)]],
139
140
            'value: empty string, exactly: 0' => [
141
                '',
142
                [new Length(0)],
143
            ],
144
            'value: empty string, min: 0' => [
145
                '',
146
                [new Length(min: 0)],
147
            ],
148
            'value: empty string, max: 0' => [
149
                '',
150
                [new Length(max: 0)],
151
            ],
152
            'value: empty string, exactly: positive, skipOnEmpty: true' => [
153
                '',
154
                [new Length(1, skipOnEmpty: true)],
155
            ],
156
        ];
157
    }
158
159
    public function dataValidationFailed(): array
160
    {
161
        $incorrectInputMessage = 'The value must be a string.';
162
        $greaterThanMaxMessage = 'This value must contain at most 25 characters.';
163
        $notExactlyMessage = 'This value must contain exactly 25 characters.';
164
        $lessThanMinMessage = 'This value must contain at least 25 characters.';
165
166
        return [
167
            'incorrect input, array' => [['not a string'], [new Length(min: 25)], ['' => [$incorrectInputMessage]]],
168
            'incorrect input, boolean (true)' => [true, [new Length(min: 25)], ['' => [$incorrectInputMessage]]],
169
            'incorrect input, boolean (false)' => [false, [new Length(min: 25)], ['' => [$incorrectInputMessage]]],
170
            'custom incorrect input message' => [
171
                false,
172
                [new Length(min: 25, incorrectInputMessage: 'Custom incorrect input message.')],
173
                ['' => ['Custom incorrect input message.']],
174
            ],
175
            'custom incorrect input message with parameters' => [
176
                false,
177
                [new Length(min: 25, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')],
178
                ['' => ['Attribute - , type - bool.']],
179
            ],
180
            'custom incorrect input message with parameters, attribute set' => [
181
                ['data' => false],
182
                ['data' => new Length(min: 25, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')],
183
                ['data' => ['Attribute - data, type - bool.']],
184
            ],
185
186
            [new SingleValueDataSet(new stdClass()), [new Length(min: 25)], ['' => [$incorrectInputMessage]]],
187
188
            [str_repeat('x', 1250), [new Length(max: 25)], ['' => [$greaterThanMaxMessage]]],
189
            [str_repeat('x', 125), [new Length(exactly: 25)], ['' => [$notExactlyMessage]]],
190
191
            ['', [new Length(exactly: 25)], ['' => [$notExactlyMessage]]],
192
            ['', [new Length(25)], ['' => [$notExactlyMessage]]],
193
            [
194
                str_repeat('x', 5),
195
                [new Length(min: 10, max: 25)],
196
                ['' => ['This value must contain at least 10 characters.']],
197
            ],
198
            [str_repeat('x', 13), [new Length(min: 25)], ['' => [$lessThanMinMessage]]],
199
            ['', [new Length(min: 25)], ['' => [$lessThanMinMessage]]],
200
201
            'custom less than min message' => [
202
                'ab',
203
                [new Length(min: 3, lessThanMinMessage: 'Custom less than min message.')],
204
                ['' => ['Custom less than min message.']],
205
            ],
206
            'custom less than min message with parameters' => [
207
                'ab',
208
                [new Length(min: 3, lessThanMinMessage: 'Min - {min}, attribute - {attribute}, number - {number}.')],
209
                ['' => ['Min - 3, attribute - , number - 2.']],
210
            ],
211
            'custom less than min message with parameters, attribute set' => [
212
                ['data' => 'ab'],
213
                [
214
                    'data' => new Length(
215
                        min: 3,
216
                        lessThanMinMessage: 'Min - {min}, attribute - {attribute}, number - {number}.',
217
                    ),
218
                ],
219
                ['data' => ['Min - 3, attribute - data, number - 2.']],
220
            ],
221
222
            'custom greater than max message' => [
223
                'abcd',
224
                [new Length(max: 3, greaterThanMaxMessage: 'Custom greater than max message.')],
225
                ['' => ['Custom greater than max message.']],
226
            ],
227
            'custom greater than max message with parameters' => [
228
                'abcd',
229
                [
230
                    new Length(
231
                        max: 3,
232
                        greaterThanMaxMessage: 'Max - {max}, attribute - {attribute}, number - {number}.',
233
                    ),
234
                ],
235
                ['' => ['Max - 3, attribute - , number - 4.']],
236
            ],
237
            'custom greater than max message with parameters, attribute set' => [
238
                ['data' => 'abcd'],
239
                [
240
                    'data' => new Length(
241
                        max: 3,
242
                        greaterThanMaxMessage: 'Max - {max}, attribute - {attribute}, number - {number}.',
243
                    ),
244
                ],
245
                ['data' => ['Max - 3, attribute - data, number - 4.']],
246
            ],
247
248
            'custom not exactly message' => [
249
                'abcd',
250
                [new Length(exactly: 3, notExactlyMessage: 'Custom not exactly message.')],
251
                ['' => ['Custom not exactly message.']],
252
            ],
253
            'custom not exactly message with parameters' => [
254
                'abcd',
255
                [
256
                    new Length(
257
                        exactly: 3,
258
                        notExactlyMessage: 'Exactly - {exactly}, attribute - {attribute}, number - {number}.',
259
                    ),
260
                ],
261
                ['' => ['Exactly - 3, attribute - , number - 4.']],
262
            ],
263
            'custom not exactly message with parameters, attribute set' => [
264
                ['data' => 'abcd'],
265
                [
266
                    'data' => new Length(
267
                        exactly: 3,
268
                        notExactlyMessage: 'Exactly - {exactly}, attribute - {attribute}, number - {number}.',
269
                    ),
270
                ],
271
                ['data' => ['Exactly - 3, attribute - data, number - 4.']],
272
            ],
273
274
            'value: string with greater count, exactly: 0' => [
275
                'a',
276
                [new Length(0)],
277
                ['' => ['This value must contain exactly 0 characters.']],
278
            ],
279
            'value: empty string, exactly: positive' => [
280
                '',
281
                [new Length(1)],
282
                ['' => ['This value must contain exactly 1 character.']],
283
            ],
284
        ];
285
    }
286
287
    public function testSkipOnError(): void
288
    {
289
        $this->testSkipOnErrorInternal(new Length(min: 3), new Length(min: 3, skipOnError: true));
290
    }
291
292
    public function testWhen(): void
293
    {
294
        $when = static fn (mixed $value): bool => $value !== null;
295
        $this->testWhenInternal(new Length(min: 3), new Length(min: 3, when: $when));
296
    }
297
298
    protected function getDifferentRuleInHandlerItems(): array
299
    {
300
        return [Length::class, LengthHandler::class];
301
    }
302
303
    protected function getRuleClass(): string
304
    {
305
        return Length::class;
306
    }
307
}
308