Skip to content

Commit

Permalink
Refactor & fix bug in BaseTestCase::assertColor()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed May 5, 2024
1 parent ab403d9 commit 41f2c96
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
51 changes: 30 additions & 21 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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;
Expand Down Expand Up @@ -49,40 +48,50 @@ public function getTestResourcePointer($filename = 'test.jpg')
*/
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)
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Drivers/Gd/Modifiers/PlaceModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public function testColorChangeOpacityJpeg(): void
$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);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Drivers/Imagick/Modifiers/PlaceModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public function testColorChangeOpacityJpeg(): void
$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);
}
}

0 comments on commit 41f2c96

Please sign in to comment.