Passed
Push — master ( c3376f...0ee5e8 )
by
unknown
02:53
created

CountableLimitTestTrait::dataUseExactlyInstead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like expectException() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

33
        $this->/** @scrutinizer ignore-call */ 
34
               expectException(InvalidArgumentException::class);
Loading history...
34
        $this->expectExceptionMessage('$exactly is mutually exclusive with $min and $max.');
0 ignored issues
show
Bug introduced by
It seems like expectExceptionMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

34
        $this->/** @scrutinizer ignore-call */ 
35
               expectExceptionMessage('$exactly is mutually exclusive with $min and $max.');
Loading history...
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