-
Notifications
You must be signed in to change notification settings - Fork 17
/
NormalizeTest.php
executable file
·266 lines (236 loc) · 7.76 KB
/
NormalizeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
namespace Elboletaire\Watimage\Test\TestCase;
use Elboletaire\Watimage\Normalize;
class NormalizeTest extends \PHPUnit_Framework_TestCase
{
public function testColor()
{
$expected = ['r' => 0, 'g' => 0, 'b' => 0, 'a' => 127];
$this->assertArraySubset($expected, Normalize::color(-1));
$this->assertArraySubset($expected, Normalize::color([0, 0, 0, 127]));
$this->assertArraySubset($expected, Normalize::color([
'red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 127
]));
$this->assertArraySubset($expected, Normalize::color([
'r' => 0, 'g' => 0, 'b' => 0, 'a' => 127
]));
$this->assertArraySubset($expected, Normalize::color('#0000007F'));
$expected['a'] = 119;
$this->assertArraySubset($expected, Normalize::color('#0007'));
}
/**
* @expectedException Elboletaire\Watimage\Exception\InvalidArgumentException
*/
public function testColorFail()
{
Normalize::color('#33333');
}
public function testCrop()
{
$expected = [23, 32, 200, 150];
// Passing multiple arguments
$this->assertArraySubset(
$expected,
// x, y, width, height
Normalize::crop(23, 32, 200, 150)
);
// Passing an array
$this->assertArraySubset(
$expected,
Normalize::crop([23, 32, 200, 150])
);
// Passing an associative array
$this->assertArraySubset(
$expected,
Normalize::crop([
'x' => 23,
'y' => 32,
'width' => 200,
'height' => 150
])
);
// Passing a simplified associative array
$this->assertArraySubset(
$expected,
Normalize::crop([
'x' => 23,
'y' => 32,
'w' => 200,
'h' => 150
])
);
}
/**
* @expectedException Elboletaire\Watimage\Exception\InvalidArgumentException
*/
public function testCropFail()
{
Normalize::crop(23);
}
public function testFitInRange()
{
// Value between range
$this->assertEquals(5, Normalize::fitInRange(5, 0, 23));
$this->assertEquals(23, Normalize::fitInRange(23, 0, 23));
// Value out of range
$this->assertEquals(23, Normalize::fitInRange(121, 0, 23));
$this->assertEquals(0, Normalize::fitInRange(-121, 0, 23));
// Just force min value
$this->assertEquals(121, Normalize::fitInRange(121, 0));
$this->assertEquals(0, Normalize::fitInRange(-121, 0));
// Just force max value
$this->assertEquals(0, Normalize::fitInRange(121, false, 0));
$this->assertEquals(-121, Normalize::fitInRange(-121, false, 0));
}
public function testFlip()
{
$this->assertEquals(IMG_FLIP_HORIZONTAL, Normalize::flip('x'));
$this->assertEquals(IMG_FLIP_HORIZONTAL, Normalize::flip('h'));
$this->assertEquals(IMG_FLIP_HORIZONTAL, Normalize::flip('horizontal'));
$this->assertEquals(IMG_FLIP_HORIZONTAL, Normalize::flip(IMG_FLIP_HORIZONTAL));
$this->assertEquals(IMG_FLIP_VERTICAL, Normalize::flip('y'));
$this->assertEquals(IMG_FLIP_VERTICAL, Normalize::flip('v'));
$this->assertEquals(IMG_FLIP_VERTICAL, Normalize::flip('vertical'));
$this->assertEquals(IMG_FLIP_VERTICAL, Normalize::flip(IMG_FLIP_VERTICAL));
$this->assertEquals(IMG_FLIP_BOTH, Normalize::flip('b'));
$this->assertEquals(IMG_FLIP_BOTH, Normalize::flip('both'));
$this->assertEquals(IMG_FLIP_BOTH, Normalize::flip(IMG_FLIP_BOTH));
}
/**
* @expectedException Elboletaire\Watimage\Exception\InvalidArgumentException
*/
public function testFlipFail()
{
Normalize::flip('fail');
}
public function testPosition()
{
$expected = [23, 23];
$this->assertArraySubset($expected, Normalize::cssPosition(23));
$this->assertArraySubset($expected, Normalize::cssPosition(['x' => 23]));
$expected = [23, 32];
$this->assertArraySubset($expected, Normalize::cssPosition($expected));
$this->assertArraySubset($expected, Normalize::cssPosition(['x' => 23, 'y' => 32]));
}
public function testMargin()
{
try {
Normalize::margin('fail');
} catch (\Exception $e) {
$this->assertEquals('Invalid margin {"x":"fail","y":null}.', $e->getMessage());
}
}
/**
* @expectedException Elboletaire\Watimage\Exception\InvalidArgumentException
*/
public function testPositionFail()
{
Normalize::position(23, 'fail');
}
public function testSize()
{
$expected = [250, 320];
// Passing multiple arguments
$this->assertArraySubset(
$expected,
Normalize::size(
// width & height
250,
320
)
);
// Passing an array
$this->assertArraySubset(
$expected,
Normalize::size([
// width & height
250,
320
])
);
// Passing an associative array
$this->assertArraySubset(
$expected,
Normalize::size([
'width' => 250,
'height' => 320
])
);
// Passing simplified associative arrays
$this->assertArraySubset(
$expected,
Normalize::size([
'w' => 250,
'h' => 320
])
);
$this->assertArraySubset(
$expected,
Normalize::size([
'x' => 250,
'y' => 320
])
);
// Passing just width (should return same height)
$this->assertArraySubset(
[250, 250],
Normalize::size(250)
);
$this->assertArraySubset(
[250, 250],
Normalize::size(['width' => 250])
);
// Passing negative values should return zero
$this->assertArraySubset(
[0, 0],
Normalize::size(-200)
);
}
/**
* @expectedException Elboletaire\Watimage\Exception\InvalidArgumentException
*/
public function testSizeFail()
{
Normalize::size(null);
}
/**
* @covers Elboletaire\Watimage\Normalize::cssPosition
*/
public function testCssPosition()
{
// Test string
$expected = 'center center';
$this->assertEquals($expected, Normalize::cssPosition('center'));
$this->assertEquals($expected, Normalize::cssPosition('centered'));
try {
Normalize::cssPosition('fail');
} catch (\Exception $e) {
$this->assertEquals('Invalid watermark position fail.', $e->getMessage());
}
}
/**
* @expectedException Elboletaire\Watimage\Exception\InvalidArgumentException
*/
public function testCssPositionFail()
{
Normalize::cssPosition('not valid');
}
public function testWatermarkSize()
{
$this->assertEquals('50%', Normalize::watermarkSize('50%'));
$this->assertEquals('full', Normalize::watermarkSize('full'));
$this->assertArraySubset([23, 42], Normalize::watermarkSize(23, 42));
try {
Normalize::watermarkSize('fail');
} catch (\Exception $e) {
$this->assertEquals('Invalid size arguments {"width":"fail","height":null}', $e->getMessage());
}
}
/**
* @expectedException Elboletaire\Watimage\Exception\InvalidArgumentException
*/
public function testWatermarkSizeFail()
{
Normalize::watermarkSize('not valid');
}
}