mirror of
https://github.com/Intervention/image.git
synced 2025-08-24 14:32:52 +02:00
Refactor & fix bug in BaseTestCase::assertColor()
This commit is contained in:
@@ -9,7 +9,6 @@ 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\Colors\Rgb\Colorspace;
|
||||
use Intervention\Image\Interfaces\ColorInterface;
|
||||
use Mockery\Adapter\Phpunit\MockeryTestCase;
|
||||
use PHPUnit\Framework\ExpectationFailedException;
|
||||
@@ -49,40 +48,50 @@ abstract class BaseTestCase extends MockeryTestCase
|
||||
*/
|
||||
protected function assertColor(int $r, int $g, int $b, int $a, ColorInterface $color, int $tolerance = 0)
|
||||
{
|
||||
// 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(max($r - $tolerance, 0), min($r + $tolerance, 255)),
|
||||
'Failed asserting that color ' .
|
||||
$color->convertTo(Colorspace::class)->toString() .
|
||||
' equals '
|
||||
. $color->convertTo(Colorspace::class)->toString()
|
||||
$range($r, $tolerance),
|
||||
$errorMessage($r, $g, $b, $a, $color)
|
||||
);
|
||||
|
||||
$this->assertContains(
|
||||
$color->channel(Green::class)->value(),
|
||||
range(max($g - $tolerance, 0), min($g + $tolerance, 255)),
|
||||
'Failed asserting that color ' .
|
||||
$color->convertTo(Colorspace::class)->toString() .
|
||||
' equals '
|
||||
. $color->convertTo(Colorspace::class)->toString()
|
||||
$range($g, $tolerance),
|
||||
$errorMessage($r, $g, $b, $a, $color)
|
||||
);
|
||||
|
||||
$this->assertContains(
|
||||
$color->channel(Blue::class)->value(),
|
||||
range(max($b - $tolerance, 0), min($b + $tolerance, 255)),
|
||||
'Failed asserting that color ' .
|
||||
$color->convertTo(Colorspace::class)->toString() .
|
||||
' equals '
|
||||
. $color->convertTo(Colorspace::class)->toString()
|
||||
$range($b, $tolerance),
|
||||
$errorMessage($r, $g, $b, $a, $color)
|
||||
);
|
||||
|
||||
$this->assertContains(
|
||||
$color->channel(Alpha::class)->value(),
|
||||
range(max($a - $tolerance, 0), min($a + $tolerance, 255)),
|
||||
'Failed asserting that color ' .
|
||||
$color->convertTo(Colorspace::class)->toString() .
|
||||
' equals '
|
||||
. $color->convertTo(Colorspace::class)->toString()
|
||||
$range($a, $tolerance),
|
||||
$errorMessage($r, $g, $b, $a, $color)
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -35,6 +35,6 @@ final class PlaceModifierTest extends GdTestCase
|
||||
$image = $this->createTestImage(16, 16)->fill('0000ff');
|
||||
$this->assertEquals('0000ff', $image->pickColor(10, 10)->toHex());
|
||||
$image->modify(new PlaceModifier($this->getTestResourcePath('exif.jpg'), opacity: 50));
|
||||
$this->assertColor(127, 83, 127, 255, $image->pickColor(10, 10), tolerance: 1);
|
||||
$this->assertColor(127, 83, 127, 255, $image->pickColor(10, 10), tolerance: 0);
|
||||
}
|
||||
}
|
||||
|
@@ -35,6 +35,6 @@ final class PlaceModifierTest extends ImagickTestCase
|
||||
$image = $this->createTestImage(16, 16)->fill('0000ff');
|
||||
$this->assertEquals('0000ff', $image->pickColor(10, 10)->toHex());
|
||||
$image->modify(new PlaceModifier($this->getTestResourcePath('exif.jpg'), opacity: 50));
|
||||
$this->assertColor(127, 83, 127, 255, $image->pickColor(10, 10), tolerance: 1);
|
||||
$this->assertColor(127, 83, 127, 255, $image->pickColor(10, 10), tolerance: 0);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user