Passed
Pull Request — master (#595)
by
unknown
04:04 queued 01:30
created

StringValueTest::dataValidationPassed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 1
b 0
f 0
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
            ['', [$rule]],
63
            [' ', [$rule]],
64
            ['test', [$rule]],
65
        ];
66
    }
67
68
    public function dataValidationFailed(): array
69
    {
70
        $rule = new StringValue();
71
        $message = 'The value must be a string.';
72
73
        return [
74
            [null, [$rule], ['' => [$message]]],
75
            [1, [$rule], ['' => [$message]]],
76
            [1.5, [$rule], ['' => [$message]]],
77
            [false, [$rule], ['' => [$message]]],
78
            [['test'], [$rule], ['' => [$message]]],
79
            [new stdClass(), [$rule], ['' => [$message]]],
80
        ];
81
    }
82
83
    public function testSkipOnError(): void
84
    {
85
        $this->testSkipOnErrorInternal(new StringValue(), new StringValue(skipOnError: true));
86
    }
87
88
    public function testWhen(): void
89
    {
90
        $when = static fn (mixed $value): bool => $value !== null;
91
        $this->testWhenInternal(new StringValue(), new StringValue(when: $when));
92
    }
93
94
    protected function getDifferentRuleInHandlerItems(): array
95
    {
96
        return [StringValue::class, StringValueHandler::class];
97
    }
98
99
    protected function getRuleClass(): string
100
    {
101
        return StringValue::class;
102
    }
103
}
104