1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use kalanis\kw_rules\Rules\File; |
4
|
|
|
use kalanis\kw_rules\Exceptions\RuleException; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class ImageRulesTest extends CommonTestClass |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @throws RuleException |
11
|
|
|
*/ |
12
|
|
|
public function testImageExists(): void |
13
|
|
|
{ |
14
|
|
|
$data = new File\ImageIs(); |
15
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
16
|
|
|
$data->validate($this->getMockImage()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @throws RuleException |
21
|
|
|
*/ |
22
|
|
|
public function testImageNotExists(): void |
23
|
|
|
{ |
24
|
|
|
$data = new File\ImageIs(); |
25
|
|
|
$this->expectException(RuleException::class); |
26
|
|
|
$data->validate($this->getMockFile()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $maxSizeX |
31
|
|
|
* @param string $maxSizeY |
32
|
|
|
* @param bool $matchEquals |
33
|
|
|
* @throws RuleException |
34
|
|
|
* @dataProvider sizeMatchProvider |
35
|
|
|
*/ |
36
|
|
|
public function testImageMatchSize(string $maxSizeX, string $maxSizeY, bool $matchEquals): void |
37
|
|
|
{ |
38
|
|
|
$data = new File\ImageSizeEquals(); |
39
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
40
|
|
|
$data->setAgainstValue([$maxSizeX, $maxSizeY]); |
41
|
|
|
if (!$matchEquals) $this->expectException(RuleException::class); |
42
|
|
|
$data->validate($this->getMockImage()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testImageMatchSizeFail(): void |
46
|
|
|
{ |
47
|
|
|
$data = new File\ImageSizeEquals(); |
48
|
|
|
$this->expectException(RuleException::class); |
49
|
|
|
$data->setAgainstValue('not array'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $maxSizeX |
54
|
|
|
* @param string $maxSizeY |
55
|
|
|
* @param bool $matchEquals |
56
|
|
|
* @param bool $matchMin |
57
|
|
|
* @param bool $matchMax |
58
|
|
|
* @throws RuleException |
59
|
|
|
* @dataProvider sizeMatchProvider |
60
|
|
|
*/ |
61
|
|
|
public function testImageMatchListSize(string $maxSizeX, string $maxSizeY, bool $matchEquals, bool $matchMin, bool $matchMax): void |
62
|
|
|
{ |
63
|
|
|
$data = new File\ImageSizeList(); |
64
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
65
|
|
|
$data->setAgainstValue([[$maxSizeX, $maxSizeY]]); |
66
|
|
|
if (!$matchEquals) $this->expectException(RuleException::class); |
67
|
|
|
$data->validate($this->getMockImage()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testImageMatchListSizeFailString(): void |
71
|
|
|
{ |
72
|
|
|
$data = new File\ImageSizeList(); |
73
|
|
|
$this->expectException(RuleException::class); |
74
|
|
|
$data->setAgainstValue('abcdef'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testImageMatchListSizeFailSimpleArray(): void |
78
|
|
|
{ |
79
|
|
|
$data = new File\ImageSizeList(); |
80
|
|
|
$this->expectException(RuleException::class); |
81
|
|
|
$data->setAgainstValue(['12', '34']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $maxSizeX |
86
|
|
|
* @param string $maxSizeY |
87
|
|
|
* @param bool $matchEquals |
88
|
|
|
* @param bool $matchMin |
89
|
|
|
* @param bool $matchMax |
90
|
|
|
* @throws RuleException |
91
|
|
|
* @dataProvider sizeMatchProvider |
92
|
|
|
*/ |
93
|
|
|
public function testImageMatchMinSize(string $maxSizeX, string $maxSizeY, bool $matchEquals, bool $matchMin, bool $matchMax): void |
94
|
|
|
{ |
95
|
|
|
$data = new File\ImageSizeMin(); |
96
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
97
|
|
|
$data->setAgainstValue([$maxSizeX, $maxSizeY]); |
98
|
|
|
if (!$matchMin) $this->expectException(RuleException::class); |
99
|
|
|
$data->validate($this->getMockImage()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testImageMatchMinSizeFail(): void |
103
|
|
|
{ |
104
|
|
|
$data = new File\ImageSizeMin(); |
105
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
106
|
|
|
$this->expectException(RuleException::class); |
107
|
|
|
$data->setAgainstValue('123456'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $maxSizeX |
112
|
|
|
* @param string $maxSizeY |
113
|
|
|
* @param bool $matchEquals |
114
|
|
|
* @param bool $matchMin |
115
|
|
|
* @param bool $matchMax |
116
|
|
|
* @throws RuleException |
117
|
|
|
* @dataProvider sizeMatchProvider |
118
|
|
|
*/ |
119
|
|
|
public function testImageMatchMaxSize(string $maxSizeX, string $maxSizeY, bool $matchEquals, bool $matchMin, bool $matchMax): void |
120
|
|
|
{ |
121
|
|
|
$data = new File\ImageSizeMax(); |
122
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
123
|
|
|
$data->setAgainstValue([$maxSizeX, $maxSizeY]); |
124
|
|
|
if (!$matchMax) $this->expectException(RuleException::class); |
125
|
|
|
$data->validate($this->getMockImage()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testImageMatchMaxSizeFail(): void |
129
|
|
|
{ |
130
|
|
|
$data = new File\ImageSizeMax(); |
131
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
132
|
|
|
$this->expectException(RuleException::class); |
133
|
|
|
$data->setAgainstValue('123456'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function sizeMatchProvider(): array |
137
|
|
|
{ |
138
|
|
|
return [ |
139
|
|
|
['6', '5', true, true, true ], |
140
|
|
|
['5', '6', false, false, false], |
141
|
|
|
['5', '5', false, true, false], |
142
|
|
|
['6', '6', false, false, true ], |
143
|
|
|
['4', '0', false, true, false], |
144
|
|
|
['0', '7', false, false, true ], |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|