CountableLimitHandlerTraitTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 71
rs 10
c 1
b 0
f 0
wmc 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ validateLimitsWithWrongRuleData() 0 38 1
validateLimitsWithWrongRuleData() 0 38 ?
testValidateLimitsWithWrongRule() 0 24 ?
A hp$1 ➔ testValidateLimitsWithWrongRule() 0 24 1
A hp$0 ➔ getGreaterThanMaxMessage() 0 3 1
A hp$0 ➔ getLessThanMinMessage() 0 3 1
A hp$0 ➔ getMax() 0 3 1
A hp$0 ➔ getMin() 0 3 1
A hp$1 ➔ validate() 0 3 1
A hp$0 ➔ getExactly() 0 3 1
A hp$0 ➔ getNotExactlyMessage() 0 3 1
A hp$1 ➔ checkValidateLimits() 0 7 1
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\CountableLimitInterface;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\Rule\Trait\CountableLimitHandlerTrait;
12
use Yiisoft\Validator\RuleHandlerInterface;
13
use Yiisoft\Validator\RuleInterface;
14
use Yiisoft\Validator\Tests\Support\Rule\RuleWithoutOptions;
15
use Yiisoft\Validator\ValidationContext;
16
17
final class CountableLimitHandlerTraitTest extends TestCase
18
{
19
    public function validateLimitsWithWrongRuleData(): array
20
    {
21
        return [
22
            [
23
                new class () implements CountableLimitInterface {
24
                    public function getMin(): int|null
25
                    {
26
                        return null;
27
                    }
28
29
                    public function getMax(): int|null
30
                    {
31
                        return null;
32
                    }
33
34
                    public function getExactly(): int|null
35
                    {
36
                        return 1;
37
                    }
38
39
                    public function getLessThanMinMessage(): string
40
                    {
41
                        return 'less then min message';
42
                    }
43
44
                    public function getGreaterThanMaxMessage(): string
45
                    {
46
                        return 'greater then min mesage';
47
                    }
48
49
                    public function getNotExactlyMessage(): string
50
                    {
51
                        return 'not exactly message';
52
                    }
53
                },
54
            ],
55
            [
56
                new RuleWithoutOptions(),
57
            ],
58
        ];
59
    }
60
61
    /**
62
     * @dataProvider validateLimitsWithWrongRuleData
63
     */
64
    public function testValidateLimitsWithWrongRule(CountableLimitInterface|RuleInterface $rule): void
65
    {
66
        $handler = new class () implements RuleHandlerInterface {
67
            use CountableLimitHandlerTrait;
68
69
            public function validate(mixed $value, object $rule, ValidationContext $context): Result
70
            {
71
                return new Result();
72
            }
73
74
            public function checkValidateLimits(
75
                CountableLimitInterface|RuleInterface $rule,
76
                ValidationContext $context,
77
                int $number,
78
                Result $result
79
            ): void {
80
                $this->validateCountableLimits($rule, $context, $number, $result);
81
            }
82
        };
83
        $context = new ValidationContext();
84
85
        $this->expectException(InvalidArgumentException::class);
86
        $this->expectExceptionMessage('$rule must implement both LimitInterface and RuleInterface.');
87
        $handler->checkValidateLimits($rule, $context, 1, new Result());
88
    }
89
}
90