-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathBaseTestCase.php
More file actions
106 lines (95 loc) · 3.66 KB
/
BaseTestCase.php
File metadata and controls
106 lines (95 loc) · 3.66 KB
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
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests;
use Intervention\Image\Colors\Rgb\Channels\Alpha;
use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\EncodedImage;
use Intervention\Image\Interfaces\ColorInterface;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use PHPUnit\Framework\ExpectationFailedException;
abstract class BaseTestCase extends MockeryTestCase
{
/**
* Assert that given color equals the given color channel values in the given optional tolerance.
*/
protected function assertColor(int $r, int $g, int $b, int $a, ColorInterface $color, int $tolerance = 0): void
{
// build errorMessage
$errorMessage = function (int $r, int $g, int $b, int $a, ColorInterface $color): string {
$color = 'rgba(' . implode(', ', [
$color->channel(Red::class)->value(),
$color->channel(Green::class)->value(),
$color->channel(Blue::class)->value(),
$color->channel(Alpha::class)->value(),
]) . ')';
return implode(' ', [
'Failed asserting that color',
$color,
'equals',
'rgba(' . $r . ', ' . $g . ', ' . $b . ', ' . $a . ')',
]);
};
foreach ([Red::class => $r, Green::class => $g, Blue::class => $b, Alpha::class => $a] as $channel => $value) {
$this->assertThat(
$color->channel($channel)->value(),
$this->logicalAnd(
$this->greaterThanOrEqual(max($channel::min(), $value - $tolerance)),
$this->lessThanOrEqual(min($channel::max(), $value + $tolerance)),
),
message: $errorMessage($r, $g, $b, $a, $color),
);
}
}
protected function assertBetween(int|float $min, int|float $max, int|float $value): void
{
if ($value < $min || $value > $max) {
throw new ExpectationFailedException(
'Failed asserting that value ' . $value . ' is between ' . $min . ' and ' . $max,
);
}
}
protected function assertTransparency(ColorInterface $color): void
{
$this->assertInstanceOf(RgbColor::class, $color);
$channel = $color->channel(Alpha::class);
$this->assertEquals(0, $channel->value(), 'Detected color ' . $color . ' is not completely transparent.');
}
protected function assertMediaType(string|array $allowed, string|EncodedImage $input): void
{
$stream = fopen('php://temp', 'rw');
fwrite($stream, (string) $input);
rewind($stream);
$detected = mime_content_type($stream);
fclose($stream);
$allowed = is_string($allowed) ? [$allowed] : $allowed;
$this->assertTrue(
in_array($detected, $allowed),
'Detected media type "' . $detected . '" is not: ' . implode(', ', $allowed),
);
}
protected function assertMediaTypeBitmap(string|EncodedImage $input): void
{
$this->assertMediaType([
'image/x-ms-bmp',
'image/bmp',
'bmp',
'ms-bmp',
'x-bitmap',
'x-bmp',
'x-ms-bmp',
'x-win-bitmap',
'x-windows-bmp',
'x-xbitmap',
'image/ms-bmp',
'image/x-bitmap',
'image/x-bmp',
'image/x-ms-bmp',
'image/x-win-bitmap',
'image/x-windows-bmp',
'image/x-xbitmap',
], $input);
}
}