1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule\Base; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
trait CountableLimitTestTrait |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @return class-string |
|
|
|
|
13
|
|
|
*/ |
14
|
|
|
abstract protected function getRuleClass(): string; |
15
|
|
|
|
16
|
|
|
public function dataInitWithMinAndMaxAndExactly(): array |
17
|
|
|
{ |
18
|
|
|
return [ |
19
|
|
|
[['min' => 3, 'exactly' => 3]], |
20
|
|
|
[['max' => 3, 'exactly' => 3]], |
21
|
|
|
[['min' => 3, 'max' => 3, 'exactly' => 3]], |
22
|
|
|
[['min' => 0, 'max' => 0, 'exactly' => 0]], |
23
|
|
|
]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @dataProvider dataInitWithMinAndMaxAndExactly |
28
|
|
|
*/ |
29
|
|
|
public function testInitWithMinAndMaxAndExactly(array $arguments): void |
30
|
|
|
{ |
31
|
|
|
$ruleClass = $this->getRuleClass(); |
32
|
|
|
|
33
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
|
|
|
34
|
|
|
$this->expectExceptionMessage('$exactly is mutually exclusive with $min and $max.'); |
|
|
|
|
35
|
|
|
new $ruleClass(...$arguments); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function dataUseExactlyInstead(): array |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
|
|
[['min' => 3, 'max' => 3]], |
42
|
|
|
[['min' => 0, 'max' => 0]], |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @dataProvider dataUseExactlyInstead |
48
|
|
|
*/ |
49
|
|
|
public function testUseExactlyInstead(array $arguments): void |
50
|
|
|
{ |
51
|
|
|
$ruleClass = $this->getRuleClass(); |
52
|
|
|
|
53
|
|
|
$this->expectException(InvalidArgumentException::class); |
54
|
|
|
$this->expectExceptionMessage('Use $exactly instead.'); |
55
|
|
|
new $ruleClass(...$arguments); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testInitWithoutRequiredArguments(): void |
59
|
|
|
{ |
60
|
|
|
$ruleClass = $this->getRuleClass(); |
61
|
|
|
|
62
|
|
|
$this->expectException(InvalidArgumentException::class); |
63
|
|
|
$this->expectExceptionMessage('At least one of these attributes must be specified: $min, $max, $exactly.'); |
64
|
|
|
new $ruleClass(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function dataInitWithNonPositiveValues(): array |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
[['min' => -1, 'max' => 2]], |
71
|
|
|
[['min' => 2, 'max' => -1]], |
72
|
|
|
[['min' => -1, 'max' => 0]], |
73
|
|
|
[['min' => 0, 'max' => -1]], |
74
|
|
|
[['min' => -2, 'max' => -1]], |
75
|
|
|
[['exactly' => -1]], |
76
|
|
|
[['min' => -1]], |
77
|
|
|
[['max' => -1]], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @dataProvider dataInitWithNonPositiveValues |
83
|
|
|
*/ |
84
|
|
|
public function testInitWithNonPositiveValues(array $arguments): void |
85
|
|
|
{ |
86
|
|
|
$ruleClass = $this->getRuleClass(); |
87
|
|
|
|
88
|
|
|
$this->expectException(InvalidArgumentException::class); |
89
|
|
|
$this->expectExceptionMessage('Only positive or zero values are allowed.'); |
90
|
|
|
new $ruleClass(...$arguments); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testInitWithMinGreaterThanMax(): void |
94
|
|
|
{ |
95
|
|
|
$ruleClass = $this->getRuleClass(); |
96
|
|
|
|
97
|
|
|
$this->expectException(InvalidArgumentException::class); |
98
|
|
|
$this->expectExceptionMessage('$min must be lower than $max.'); |
99
|
|
|
new $ruleClass(min: 2, max: 1); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|