Passed
Push — master ( dbf0a8...b29633 )
by
unknown
02:35
created

ssage()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use InvalidArgumentException;
8
use PHPUnit\Framework\TestCase;
9
use Yiisoft\Validator\LimitInterface;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\Rule\Trait\LimitHandlerTrait;
12
use Yiisoft\Validator\RuleHandlerInterface;
13
use Yiisoft\Validator\RuleInterface;
14
use Yiisoft\Validator\Tests\Support\Rule\RuleWithoutOptions;
15
use Yiisoft\Validator\Tests\Support\ValidatorFactory;
16
use Yiisoft\Validator\ValidationContext;
17
18
final class LimitHandlerTraitTest extends TestCase
19
{
20
    public function validateLimitsWithWrongRuleData(): array
21
    {
22
        return [
23
            [
24
                new class () implements LimitInterface {
25
                    public function getMin(): ?int
26
                    {
27
                        return null;
28
                    }
29
30
                    public function getMax(): ?int
31
                    {
32
                        return null;
33
                    }
34
35
                    public function getExactly(): ?int
36
                    {
37
                        return 1;
38
                    }
39
40
                    public function getLessThanMinMessage(): string
41
                    {
42
                        return 'less then min message';
43
                    }
44
45
                    public function getGreaterThanMaxMessage(): string
46
                    {
47
                        return 'greater then min mesage';
48
                    }
49
50
                    public function getNotExactlyMessage(): string
51
                    {
52
                        return 'not exactly message';
53
                    }
54
                },
55
            ],
56
            [
57
                new RuleWithoutOptions(),
58
            ],
59
        ];
60
    }
61
62
    /**
63
     * @dataProvider validateLimitsWithWrongRuleData
64
     */
65
    public function testValidateLimitsWithWrongRule(LimitInterface|RuleInterface $rule): void
66
    {
67
        $handler = new class () implements RuleHandlerInterface {
68
            use LimitHandlerTrait;
69
70
            public function validate(mixed $value, object $rule, ValidationContext $context): Result
71
            {
72
                return new Result();
73
            }
74
75
            public function checkValidateLimits(
76
                LimitInterface|RuleInterface $rule,
77
                ValidationContext $context,
78
                int $number,
79
                Result $result
80
            ): void {
81
                $this->validateLimits($rule, $context, $number, $result);
82
            }
83
        };
84
        $context = new ValidationContext(ValidatorFactory::make(), dataSet: null);
85
86
        $this->expectException(InvalidArgumentException::class);
87
        $this->expectExceptionMessage('$rule must implement both LimitInterface and RuleInterface.');
88
        $handler->checkValidateLimits($rule, $context, 1, new Result());
89
    }
90
}
91