Passed
Pull Request — master (#595)
by
unknown
02:33
created

StringValueTest.php$1 ➔ testWhen()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
cc 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use stdClass;
8
use Yiisoft\Validator\Rule\StringValue;
9
use Yiisoft\Validator\Rule\StringValueHandler;
10
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
11
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
12
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
13
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
14
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
15
16
final class StringValueTest extends RuleTestCase
17
{
18
    use DifferentRuleInHandlerTestTrait;
19
    use RuleWithOptionsTestTrait;
20
    use SkipOnErrorTestTrait;
21
    use WhenTestTrait;
22
23
    public function testGetName(): void
24
    {
25
        $rule = new StringValue();
26
        $this->assertSame('string', $rule->getName());
27
    }
28
29
    public function dataOptions(): array
30
    {
31
        return [
32
            [
33
                new StringValue(),
34
                [
35
                    'message' => [
36
                        'template' => 'The value must be a string.',
37
                        'parameters' => [],
38
                    ],
39
                    'skipOnEmpty' => false,
40
                    'skipOnError' => false,
41
                ],
42
            ],
43
            [
44
                new StringValue(message: 'Custom message.', skipOnEmpty: true, skipOnError: true),
45
                [
46
                    'message' => [
47
                        'template' => 'Custom message.',
48
                        'parameters' => [],
49
                    ],
50
                    'skipOnEmpty' => true,
51
                    'skipOnError' => true,
52
                ],
53
            ],
54
        ];
55
    }
56
57
    public function dataValidationPassed(): array
58
    {
59
        $rule = new StringValue();
60
61
        return [
62
            'value: empty string' => ['', [$rule]],
63
            'value: empty string with whitespaces' => [' ', [$rule]],
64
            'value: non-empty string' => ['test', [$rule]],
65
            'value: null, skipOnEmpty: true' => [null, [new StringValue(skipOnEmpty: true)]],
66
            'value: null, when: custom callable allowing everything except null' => [
67
                null,
68
                [new StringValue(when: static fn (mixed $value): bool => $value !== null)],
69
            ],
70
            'value: object providing rules and valid data' => [
71
                new class () {
72
                    #[StringValue]
73
                    private string $name = 'test';
0 ignored issues
show
introduced by
The private property $name is not used, and could be removed.
Loading history...
74
                },
75
                null,
76
            ],
77
        ];
78
    }
79
80
    public function dataValidationFailed(): array
81
    {
82
        $rule = new StringValue();
83
        $message = 'The value must be a string.';
84
85
        return [
86
            'value: null' => [null, [$rule], ['' => [$message]]],
87
            'value: integer' => [1, [$rule], ['' => [$message]]],
88
            'value: float' => [1.5, [$rule], ['' => [$message]]],
89
            'value: boolean' => [false, [$rule], ['' => [$message]]],
90
            'value: array' => [['test'], [$rule], ['' => [$message]]],
91
            'value: object' => [new stdClass(), [$rule], ['' => [$message]]],
92
            'value: null, multiple rules' => [
93
                null,
94
                [
95
                    new StringValue(),
96
                    new StringValue(),
97
                ],
98
                ['' => [$message, $message]],
99
            ],
100
            'value: null, multiple rules, skipOnError: true' => [
101
                null,
102
                [
103
                    new StringValue(),
104
                    new StringValue(skipOnError: true),
105
                ],
106
                ['' => [$message]],
107
            ],
108
            'value: integer, when: custom callable allowing everything except null' => [
109
                1,
110
                [new StringValue(when: static fn (mixed $value): bool => $value !== null)],
111
                ['' => [$message]],
112
            ],
113
            'value: object providing rules and wrong data' => [
114
                new class () {
115
                    #[StringValue]
116
                    private ?string $name = null;
0 ignored issues
show
introduced by
The private property $name is not used, and could be removed.
Loading history...
117
                },
118
                null,
119
                ['name' => [$message]],
120
            ],
121
        ];
122
    }
123
124
    public function testSkipOnError(): void
125
    {
126
        $this->testSkipOnErrorInternal(new StringValue(), new StringValue(skipOnError: true));
127
    }
128
129
    public function testWhen(): void
130
    {
131
        $when = static fn (mixed $value): bool => $value !== null;
132
        $this->testWhenInternal(new StringValue(), new StringValue(when: $when));
133
    }
134
135
    protected function getDifferentRuleInHandlerItems(): array
136
    {
137
        return [StringValue::class, StringValueHandler::class];
138
    }
139
140
    protected function getRuleClass(): string
141
    {
142
        return StringValue::class;
143
    }
144
}
145