diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index cd499d25..7416909f 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -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) ); } diff --git a/tests/Unit/Drivers/Gd/Modifiers/PlaceModifierTest.php b/tests/Unit/Drivers/Gd/Modifiers/PlaceModifierTest.php index 7fcc1b88..c169d591 100644 --- a/tests/Unit/Drivers/Gd/Modifiers/PlaceModifierTest.php +++ b/tests/Unit/Drivers/Gd/Modifiers/PlaceModifierTest.php @@ -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); } } diff --git a/tests/Unit/Drivers/Imagick/Modifiers/PlaceModifierTest.php b/tests/Unit/Drivers/Imagick/Modifiers/PlaceModifierTest.php index d68f015a..80e9cffb 100644 --- a/tests/Unit/Drivers/Imagick/Modifiers/PlaceModifierTest.php +++ b/tests/Unit/Drivers/Imagick/Modifiers/PlaceModifierTest.php @@ -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); } }