mirror of
https://github.com/Intervention/image.git
synced 2025-08-25 06:40:48 +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\Green;
|
||||||
use Intervention\Image\Colors\Rgb\Channels\Red;
|
use Intervention\Image\Colors\Rgb\Channels\Red;
|
||||||
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
use Intervention\Image\Colors\Rgb\Colorspace;
|
|
||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Mockery\Adapter\Phpunit\MockeryTestCase;
|
use Mockery\Adapter\Phpunit\MockeryTestCase;
|
||||||
use PHPUnit\Framework\ExpectationFailedException;
|
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)
|
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(
|
$this->assertContains(
|
||||||
$color->channel(Red::class)->value(),
|
$color->channel(Red::class)->value(),
|
||||||
range(max($r - $tolerance, 0), min($r + $tolerance, 255)),
|
$range($r, $tolerance),
|
||||||
'Failed asserting that color ' .
|
$errorMessage($r, $g, $b, $a, $color)
|
||||||
$color->convertTo(Colorspace::class)->toString() .
|
|
||||||
' equals '
|
|
||||||
. $color->convertTo(Colorspace::class)->toString()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertContains(
|
$this->assertContains(
|
||||||
$color->channel(Green::class)->value(),
|
$color->channel(Green::class)->value(),
|
||||||
range(max($g - $tolerance, 0), min($g + $tolerance, 255)),
|
$range($g, $tolerance),
|
||||||
'Failed asserting that color ' .
|
$errorMessage($r, $g, $b, $a, $color)
|
||||||
$color->convertTo(Colorspace::class)->toString() .
|
|
||||||
' equals '
|
|
||||||
. $color->convertTo(Colorspace::class)->toString()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertContains(
|
$this->assertContains(
|
||||||
$color->channel(Blue::class)->value(),
|
$color->channel(Blue::class)->value(),
|
||||||
range(max($b - $tolerance, 0), min($b + $tolerance, 255)),
|
$range($b, $tolerance),
|
||||||
'Failed asserting that color ' .
|
$errorMessage($r, $g, $b, $a, $color)
|
||||||
$color->convertTo(Colorspace::class)->toString() .
|
|
||||||
' equals '
|
|
||||||
. $color->convertTo(Colorspace::class)->toString()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertContains(
|
$this->assertContains(
|
||||||
$color->channel(Alpha::class)->value(),
|
$color->channel(Alpha::class)->value(),
|
||||||
range(max($a - $tolerance, 0), min($a + $tolerance, 255)),
|
$range($a, $tolerance),
|
||||||
'Failed asserting that color ' .
|
$errorMessage($r, $g, $b, $a, $color)
|
||||||
$color->convertTo(Colorspace::class)->toString() .
|
|
||||||
' equals '
|
|
||||||
. $color->convertTo(Colorspace::class)->toString()
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -35,6 +35,6 @@ final class PlaceModifierTest extends GdTestCase
|
|||||||
$image = $this->createTestImage(16, 16)->fill('0000ff');
|
$image = $this->createTestImage(16, 16)->fill('0000ff');
|
||||||
$this->assertEquals('0000ff', $image->pickColor(10, 10)->toHex());
|
$this->assertEquals('0000ff', $image->pickColor(10, 10)->toHex());
|
||||||
$image->modify(new PlaceModifier($this->getTestResourcePath('exif.jpg'), opacity: 50));
|
$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');
|
$image = $this->createTestImage(16, 16)->fill('0000ff');
|
||||||
$this->assertEquals('0000ff', $image->pickColor(10, 10)->toHex());
|
$this->assertEquals('0000ff', $image->pickColor(10, 10)->toHex());
|
||||||
$image->modify(new PlaceModifier($this->getTestResourcePath('exif.jpg'), opacity: 50));
|
$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