1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-05 13:27:29 +02:00
Files
intervention_image/tests/BaseTestCase.php
2025-06-01 07:57:46 +02:00

137 lines
4.4 KiB
PHP

<?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
{
public static function getTestResourcePath(string $filename = 'test.jpg'): string
{
return sprintf('%s/resources/%s', __DIR__, $filename);
}
public static function getTestResourceData(string $filename = 'test.jpg'): string
{
return file_get_contents(self::getTestResourcePath($filename));
}
public static function getTestResourcePointer(string $filename = 'test.jpg'): mixed
{
$pointer = fopen('php://temp', 'rw');
fwrite($pointer, self::getTestResourceData($filename));
rewind($pointer);
return $pointer;
}
/**
* Assert that given color equals the given color channel values in the given optional tolerance
*
* @throws ExpectationFailedException
*/
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, $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 . ')'
]);
};
// build color channel value range
$range = function (int $base, int $tolerance): array {
return range(max($base - $tolerance, 0), min($base + $tolerance, 255));
};
$this->assertContains(
$color->channel(Red::class)->value(),
$range($r, $tolerance),
$errorMessage($r, $g, $b, $a, $color)
);
$this->assertContains(
$color->channel(Green::class)->value(),
$range($g, $tolerance),
$errorMessage($r, $g, $b, $a, $color)
);
$this->assertContains(
$color->channel(Blue::class)->value(),
$range($b, $tolerance),
$errorMessage($r, $g, $b, $a, $color)
);
$this->assertContains(
$color->channel(Alpha::class)->value(),
$range($a, $tolerance),
$errorMessage($r, $g, $b, $a, $color)
);
}
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
{
$pointer = fopen('php://temp', 'rw');
fwrite($pointer, (string) $input);
rewind($pointer);
$detected = mime_content_type($pointer);
fclose($pointer);
$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);
}
}