|
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
|
|
|
|