Passed
Pull Request — master (#646)
by
unknown
02:35
created

DateTest::testWhen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use InvalidArgumentException;
8
use Yiisoft\Validator\Rule\Date;
9
use Yiisoft\Validator\Rule\DateHandler;
10
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
11
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
12
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
13
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
14
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
15
16
final class DateTest extends RuleTestCase
17
{
18
    use DifferentRuleInHandlerTestTrait;
19
    use RuleWithOptionsTestTrait;
20
    use SkipOnErrorTestTrait;
21
    use WhenTestTrait;
22
23
    public function dataInvalidConfiguration(): array
24
    {
25
        return [
26
            [['pattern' => ''], 'Pattern can\'t be empty.'],
27
            [['format' => ''], 'Format can\'t be empty.'],
28
        ];
29
    }
30
31
    /**
32
     * @dataProvider dataInvalidConfiguration
33
     */
34
    public function testinvalidConfiguration(array $arguments, string $expectedExceptionMessage): void
35
    {
36
        $this->expectException(InvalidArgumentException::class);
37
        $this->expectExceptionMessage($expectedExceptionMessage);
38
        new Date(...$arguments);
0 ignored issues
show
Bug introduced by
$arguments is expanded, but the parameter $format of Yiisoft\Validator\Rule\Date::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        new Date(/** @scrutinizer ignore-type */ ...$arguments);
Loading history...
39
    }
40
41
    public function dataValidationPassed(): array
42
    {
43
        return [
44
            ['2020-01-01', [new Date(format: 'Y-m-d')]],
45
            ['10.02.2023', [new Date(format: 'd.m.Y')]],
46
            ['10/02/2023', [new Date(format: 'd/m/Y')]],
47
            ['', [new Date(format: 'd-m-Y', skipOnEmpty: true)]],
48
        ];
49
    }
50
51
    public function dataValidationFailed(): array
52
    {
53
        return [
54
            'incorrect input, is integer' => [
55
                1,
56
                [new Date(incorrectInputMessage: 'Custom incorrect input message.')],
57
                ['' => ['Custom incorrect input message.']],
58
            ],
59
            [
60
                '2023-02-20ee',
61
                [new Date(format: 'Y-m-dee', incorrectInputMessage: 'The must be a date.')],
62
                ['' => ['The must be a date.']],
63
            ],
64
            [
65
                '10-02-2023 00:00',
66
                [new Date(format: 'd-m-Y H:i', incorrectInputMessage: 'The must be a date.')],
67
                ['' => ['The must be a date.']],
68
            ],
69
            [
70
                '2023-02-2023',
71
                [new Date(format: 'Y-d-Y', incorrectInputMessage: 'The must be a date.')],
72
                ['' => ['The must be a date.']],
73
            ],
74
            [
75
                '2023-02-30',
76
                [new Date(format: 'Y-m-d', message: 'Attribute - {attribute}, value - {value}.')],
77
                ['' => ['Attribute - , value - 2023-02-30.']],
78
            ],
79
            'custom incorrect input message with parameters, attribute set' => [
80
                ['attribute' => 1],
81
                ['attribute' => [new Date(incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')]],
82
                ['attribute' => ['Attribute - attribute, type - int.']],
83
            ],
84
            'incorrect input, is not date' => [
85
                'asdadas',
86
                [new Date(message: 'Attribute - {attribute}, value - {value}.')],
87
                ['' => ['Attribute - , value - asdadas.']],
88
            ],
89
            'empty string and custom message' => [
90
                '',
91
                [new Date()],
92
                ['' => ['The  must be a date.']],
93
            ],
94
        ];
95
    }
96
97
    public function dataOptions(): array
98
    {
99
        return [
100
            [
101
                new Date(),
102
                [
103
                    'format' => 'Y-m-d',
104
                    'pattern' => '/^(?=.*Y)(?=.*[mM])(?=.*d).*[Ymd](-|\/|.)[Ymd]\1[Ymd]$/',
105
                    'incorrectInputMessage' => [
106
                        'template' => 'The {attribute} must be a date.',
107
                        'parameters' => [],
108
                    ],
109
                    'message' => [
110
                        'template' => 'The {attribute} is not a valid date.',
111
                        'parameters' => [],
112
                    ],
113
                    'skipOnEmpty' => false,
114
                    'skipOnError' => false,
115
                ],
116
            ],
117
        ];
118
    }
119
120
    public function testSkipOnError(): void
121
    {
122
        $this->testSkipOnErrorInternal(new Date(), new Date(skipOnError: true));
123
    }
124
125
    public function testWhen(): void
126
    {
127
        $when = static fn(mixed $value): bool => $value !== null;
128
        $this->testWhenInternal(new Date(), new Date(when: $when));
129
    }
130
131
    protected function getDifferentRuleInHandlerItems(): array
132
    {
133
        return [Date::class, DateHandler::class];
134
    }
135
136
    public function testGetName(): void
137
    {
138
        $rule = new Date();
139
        $this->assertSame(Date::class, $rule->getName());
140
    }
141
142
}
143