2021-10-21 14:32:05 +02:00
|
|
|
<?php
|
|
|
|
|
2024-01-16 12:01:29 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-10-21 14:32:05 +02:00
|
|
|
namespace Intervention\Image\Tests;
|
|
|
|
|
2023-10-22 09:12:15 +02:00
|
|
|
use Intervention\Image\Colors\Rgb\Channels\Alpha;
|
2024-05-05 11:28:01 +02:00
|
|
|
use Intervention\Image\Colors\Rgb\Channels\Blue;
|
|
|
|
use Intervention\Image\Colors\Rgb\Channels\Green;
|
|
|
|
use Intervention\Image\Colors\Rgb\Channels\Red;
|
2023-10-22 09:12:15 +02:00
|
|
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
2024-10-06 11:36:17 +02:00
|
|
|
use Intervention\Image\EncodedImage;
|
2021-10-21 14:32:05 +02:00
|
|
|
use Intervention\Image\Interfaces\ColorInterface;
|
2021-12-07 11:00:06 +00:00
|
|
|
use Mockery\Adapter\Phpunit\MockeryTestCase;
|
2024-05-05 11:28:01 +02:00
|
|
|
use PHPUnit\Framework\ExpectationFailedException;
|
2021-10-21 14:32:05 +02:00
|
|
|
|
2024-02-28 16:16:23 +01:00
|
|
|
abstract class BaseTestCase extends MockeryTestCase
|
2021-10-21 14:32:05 +02:00
|
|
|
{
|
2024-05-09 09:58:10 +02:00
|
|
|
public static function getTestResourcePath($filename = 'test.jpg'): string
|
2023-10-03 10:08:37 +02:00
|
|
|
{
|
2024-03-10 10:00:06 +01:00
|
|
|
return sprintf('%s/resources/%s', __DIR__, $filename);
|
2023-10-03 10:08:37 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 09:58:10 +02:00
|
|
|
public static function getTestResourceData($filename = 'test.jpg'): string
|
2023-10-03 10:08:37 +02:00
|
|
|
{
|
2024-05-09 09:58:10 +02:00
|
|
|
return file_get_contents(self::getTestResourcePath($filename));
|
2023-10-03 10:08:37 +02:00
|
|
|
}
|
|
|
|
|
2024-08-03 11:15:29 +02:00
|
|
|
public static function getTestResourcePointer($filename = 'test.jpg')
|
2024-03-23 08:08:41 +01:00
|
|
|
{
|
|
|
|
$pointer = fopen('php://temp', 'rw');
|
2024-05-09 09:58:10 +02:00
|
|
|
fputs($pointer, self::getTestResourceData($filename));
|
2024-03-23 08:08:41 +01:00
|
|
|
rewind($pointer);
|
|
|
|
|
|
|
|
return $pointer;
|
|
|
|
}
|
|
|
|
|
2024-05-05 11:28:01 +02:00
|
|
|
/**
|
|
|
|
* Assert that given color equals the given color channel values in the given optional tolerance
|
|
|
|
*
|
|
|
|
* @param int $r
|
|
|
|
* @param int $g
|
|
|
|
* @param int $b
|
|
|
|
* @param int $a
|
|
|
|
* @param ColorInterface $color
|
|
|
|
* @param int $tolerance
|
|
|
|
* @throws ExpectationFailedException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function assertColor(int $r, int $g, int $b, int $a, ColorInterface $color, int $tolerance = 0)
|
2021-10-21 14:32:05 +02:00
|
|
|
{
|
2024-05-05 11:48:13 +02:00
|
|
|
// 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));
|
|
|
|
};
|
|
|
|
|
2024-05-05 11:28:01 +02:00
|
|
|
$this->assertContains(
|
|
|
|
$color->channel(Red::class)->value(),
|
2024-05-05 11:48:13 +02:00
|
|
|
$range($r, $tolerance),
|
|
|
|
$errorMessage($r, $g, $b, $a, $color)
|
2024-05-05 11:28:01 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertContains(
|
|
|
|
$color->channel(Green::class)->value(),
|
2024-05-05 11:48:13 +02:00
|
|
|
$range($g, $tolerance),
|
|
|
|
$errorMessage($r, $g, $b, $a, $color)
|
2024-05-05 11:28:01 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertContains(
|
|
|
|
$color->channel(Blue::class)->value(),
|
2024-05-05 11:48:13 +02:00
|
|
|
$range($b, $tolerance),
|
|
|
|
$errorMessage($r, $g, $b, $a, $color)
|
2024-05-05 11:28:01 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertContains(
|
|
|
|
$color->channel(Alpha::class)->value(),
|
2024-05-05 11:48:13 +02:00
|
|
|
$range($a, $tolerance),
|
|
|
|
$errorMessage($r, $g, $b, $a, $color)
|
2024-05-05 11:28:01 +02:00
|
|
|
);
|
2021-12-07 11:00:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function assertTransparency(ColorInterface $color)
|
|
|
|
{
|
2023-10-22 09:12:15 +02:00
|
|
|
$this->assertInstanceOf(RgbColor::class, $color);
|
|
|
|
$channel = $color->channel(Alpha::class);
|
|
|
|
$this->assertEquals(0, $channel->value());
|
2021-10-21 14:32:05 +02:00
|
|
|
}
|
2023-11-12 11:32:08 +01:00
|
|
|
|
2024-10-06 11:36:17 +02:00
|
|
|
protected function assertMediaType(string|array $allowed, string|EncodedImage $input): void
|
2023-11-12 11:32:08 +01:00
|
|
|
{
|
2024-10-06 16:37:26 +02:00
|
|
|
$pointer = fopen('php://temp', 'rw');
|
|
|
|
fputs($pointer, (string) $input);
|
|
|
|
rewind($pointer);
|
|
|
|
$detected = mime_content_type($pointer);
|
|
|
|
fclose($pointer);
|
2023-11-12 11:32:08 +01:00
|
|
|
|
|
|
|
$allowed = is_string($allowed) ? [$allowed] : $allowed;
|
|
|
|
$this->assertTrue(in_array($detected, $allowed));
|
|
|
|
}
|
2024-01-16 16:49:24 +01:00
|
|
|
|
2024-10-06 11:36:17 +02:00
|
|
|
protected function assertMediaTypeBitmap(string|EncodedImage $input): void
|
2024-01-16 16:49:24 +01:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
2021-10-21 14:32:05 +02:00
|
|
|
}
|