From a9b50e0bd1f59cf89288a828c3a4edff24ae1a47 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Mon, 15 Jan 2024 15:01:03 +0100 Subject: [PATCH] Add tests --- src/Colors/Rgb/Channels/Blue.php | 1 - src/Colors/Rgb/Channels/Green.php | 1 - tests/Colors/Cmyk/ChannelTest.php | 19 +++++++++++++ tests/Colors/Cmyk/ColorTest.php | 30 ++++++++++++++++++++ tests/Colors/Hsl/ChannelTest.php | 19 +++++++++++++ tests/Colors/Hsl/Channels/SaturationTest.php | 16 +++++++++++ tests/Colors/Hsl/ColorTest.php | 20 +++++++++++++ tests/Colors/Hsv/ChannelTest.php | 19 +++++++++++++ tests/Colors/Hsv/Channels/SaturationTest.php | 16 +++++++++++ tests/Colors/Hsv/Channels/ValueTest.php | 16 +++++++++++ tests/Colors/Hsv/ColorTest.php | 20 +++++++++++++ tests/Colors/Rgb/ChannelTest.php | 19 +++++++++++++ tests/Colors/Rgb/Channels/AlphaTest.php | 19 +++++++++++++ tests/Colors/Rgb/ColorTest.php | 20 +++++++++++++ 14 files changed, 233 insertions(+), 2 deletions(-) create mode 100644 tests/Colors/Hsl/Channels/SaturationTest.php create mode 100644 tests/Colors/Hsv/Channels/SaturationTest.php create mode 100644 tests/Colors/Hsv/Channels/ValueTest.php create mode 100644 tests/Colors/Rgb/Channels/AlphaTest.php diff --git a/src/Colors/Rgb/Channels/Blue.php b/src/Colors/Rgb/Channels/Blue.php index b83be821..df5d56b0 100644 --- a/src/Colors/Rgb/Channels/Blue.php +++ b/src/Colors/Rgb/Channels/Blue.php @@ -4,5 +4,4 @@ namespace Intervention\Image\Colors\Rgb\Channels; class Blue extends Red { - // } diff --git a/src/Colors/Rgb/Channels/Green.php b/src/Colors/Rgb/Channels/Green.php index 28abe607..93c85ceb 100644 --- a/src/Colors/Rgb/Channels/Green.php +++ b/src/Colors/Rgb/Channels/Green.php @@ -4,5 +4,4 @@ namespace Intervention\Image\Colors\Rgb\Channels; class Green extends Red { - // } diff --git a/tests/Colors/Cmyk/ChannelTest.php b/tests/Colors/Cmyk/ChannelTest.php index 64f03f33..a282712a 100644 --- a/tests/Colors/Cmyk/ChannelTest.php +++ b/tests/Colors/Cmyk/ChannelTest.php @@ -33,6 +33,25 @@ class ChannelTest extends TestCase $channel = new Channel(normalized: 2); } + public function testConstructorFail(): void + { + $this->expectException(ColorException::class); + new Channel(200); + } + + public function testToInt(): void + { + $channel = new Channel(10); + $this->assertEquals(10, $channel->toInt()); + } + + public function testToString(): void + { + $channel = new Channel(10); + $this->assertEquals("10", $channel->toString()); + $this->assertEquals("10", (string) $channel); + } + public function testValue(): void { $channel = new Channel(10); diff --git a/tests/Colors/Cmyk/ColorTest.php b/tests/Colors/Cmyk/ColorTest.php index 6a06634c..71da6093 100644 --- a/tests/Colors/Cmyk/ColorTest.php +++ b/tests/Colors/Cmyk/ColorTest.php @@ -8,6 +8,7 @@ use Intervention\Image\Colors\Cmyk\Channels\Magenta; use Intervention\Image\Colors\Cmyk\Channels\Yellow; use Intervention\Image\Colors\Cmyk\Color as Color; use Intervention\Image\Colors\Cmyk\Colorspace; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Tests\TestCase; /** @@ -22,6 +23,12 @@ class ColorTest extends TestCase $this->assertInstanceOf(Color::class, $color); } + public function testCreate(): void + { + $color = Color::create('cmyk(10, 20, 30, 40)'); + $this->assertInstanceOf(Color::class, $color); + } + public function testColorspace(): void { $color = new Color(0, 0, 0, 0); @@ -43,6 +50,13 @@ class ColorTest extends TestCase $this->assertEquals(10, $channel->value()); } + public function testChannelNotFound(): void + { + $color = new Color(10, 20, 30, 30); + $this->expectException(ColorException::class); + $color->channel('none'); + } + public function testCyanMagentaYellowKey(): void { $color = new Color(10, 20, 30, 40); @@ -62,6 +76,22 @@ class ColorTest extends TestCase $this->assertEquals([10, 20, 30, 40], $color->toArray()); } + public function testToHex(): void + { + $color = new Color(0, 73, 100, 0); + $this->assertEquals('ff4400', $color->toHex()); + $this->assertEquals('#ff4400', $color->toHex('#')); + } + + public function testIsGreyscale(): void + { + $color = new Color(0, 73, 100, 0); + $this->assertFalse($color->isGreyscale()); + + $color = new Color(0, 0, 0, 50); + $this->assertTrue($color->isGreyscale()); + } + public function testNormalize(): void { $color = new Color(100, 50, 20, 0); diff --git a/tests/Colors/Hsl/ChannelTest.php b/tests/Colors/Hsl/ChannelTest.php index 0b540efb..f2b3b9a8 100644 --- a/tests/Colors/Hsl/ChannelTest.php +++ b/tests/Colors/Hsl/ChannelTest.php @@ -33,6 +33,25 @@ class ChannelTest extends TestCase $channel = new Hue(normalized: 2); } + public function testConstructorFail(): void + { + $this->expectException(ColorException::class); + new Hue(400); + } + + public function testToInt(): void + { + $channel = new Hue(10); + $this->assertEquals(10, $channel->toInt()); + } + + public function testToString(): void + { + $channel = new Hue(10); + $this->assertEquals("10", $channel->toString()); + $this->assertEquals("10", (string) $channel); + } + public function testValue(): void { $channel = new Hue(10); diff --git a/tests/Colors/Hsl/Channels/SaturationTest.php b/tests/Colors/Hsl/Channels/SaturationTest.php new file mode 100644 index 00000000..a4a610d0 --- /dev/null +++ b/tests/Colors/Hsl/Channels/SaturationTest.php @@ -0,0 +1,16 @@ +assertEquals(0, $saturation->min()); + $this->assertEquals(100, $saturation->max()); + } +} diff --git a/tests/Colors/Hsl/ColorTest.php b/tests/Colors/Hsl/ColorTest.php index 9039309c..3ed2ed06 100644 --- a/tests/Colors/Hsl/ColorTest.php +++ b/tests/Colors/Hsl/ColorTest.php @@ -7,6 +7,7 @@ use Intervention\Image\Colors\Hsl\Channels\Luminance; use Intervention\Image\Colors\Hsl\Channels\Saturation; use Intervention\Image\Colors\Hsl\Color; use Intervention\Image\Colors\Hsl\Colorspace; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Tests\TestCase; /** @@ -20,6 +21,12 @@ class ColorTest extends TestCase $this->assertInstanceOf(Color::class, $color); } + public function testCreate(): void + { + $color = Color::create('hsl(10, 20, 30)'); + $this->assertInstanceOf(Color::class, $color); + } + public function testColorspace(): void { $color = new Color(0, 0, 0); @@ -41,6 +48,13 @@ class ColorTest extends TestCase $this->assertEquals(10, $channel->value()); } + public function testChannelNotFound(): void + { + $color = new Color(10, 20, 30); + $this->expectException(ColorException::class); + $color->channel('none'); + } + public function testHueSaturationLuminanceKey(): void { $color = new Color(10, 20, 30); @@ -58,6 +72,12 @@ class ColorTest extends TestCase $this->assertEquals([10, 20, 30], $color->toArray()); } + public function testToHex(): void + { + $color = new Color(16, 100, 50); + $this->assertEquals('ff4400', $color->toHex()); + } + public function testNormalize(): void { $color = new Color(180, 50, 25); diff --git a/tests/Colors/Hsv/ChannelTest.php b/tests/Colors/Hsv/ChannelTest.php index 2e463c53..003f783e 100644 --- a/tests/Colors/Hsv/ChannelTest.php +++ b/tests/Colors/Hsv/ChannelTest.php @@ -34,6 +34,25 @@ class ChannelTest extends TestCase $channel = new Hue(normalized: 2); } + public function testConstructorFail(): void + { + $this->expectException(ColorException::class); + new Hue(400); + } + + public function testToInt(): void + { + $channel = new Hue(10); + $this->assertEquals(10, $channel->toInt()); + } + + public function testToString(): void + { + $channel = new Hue(10); + $this->assertEquals("10", $channel->toString()); + $this->assertEquals("10", (string) $channel); + } + public function testValue(): void { $channel = new Hue(10); diff --git a/tests/Colors/Hsv/Channels/SaturationTest.php b/tests/Colors/Hsv/Channels/SaturationTest.php new file mode 100644 index 00000000..021c7e57 --- /dev/null +++ b/tests/Colors/Hsv/Channels/SaturationTest.php @@ -0,0 +1,16 @@ +assertEquals(0, $saturation->min()); + $this->assertEquals(100, $saturation->max()); + } +} diff --git a/tests/Colors/Hsv/Channels/ValueTest.php b/tests/Colors/Hsv/Channels/ValueTest.php new file mode 100644 index 00000000..7a098842 --- /dev/null +++ b/tests/Colors/Hsv/Channels/ValueTest.php @@ -0,0 +1,16 @@ +assertEquals(0, $saturation->min()); + $this->assertEquals(100, $saturation->max()); + } +} diff --git a/tests/Colors/Hsv/ColorTest.php b/tests/Colors/Hsv/ColorTest.php index 3fc70b09..64d015a7 100644 --- a/tests/Colors/Hsv/ColorTest.php +++ b/tests/Colors/Hsv/ColorTest.php @@ -7,6 +7,7 @@ use Intervention\Image\Colors\Hsv\Channels\Saturation; use Intervention\Image\Colors\Hsv\Channels\Value; use Intervention\Image\Colors\Hsv\Color as Color; use Intervention\Image\Colors\Hsv\Colorspace; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Tests\TestCase; /** @@ -20,6 +21,12 @@ class ColorTest extends TestCase $this->assertInstanceOf(Color::class, $color); } + public function testCreate(): void + { + $color = Color::create('hsv(10, 20, 30)'); + $this->assertInstanceOf(Color::class, $color); + } + public function testColorspace(): void { $color = new Color(0, 0, 0); @@ -41,6 +48,13 @@ class ColorTest extends TestCase $this->assertEquals(10, $channel->value()); } + public function testChannelNotFound(): void + { + $color = new Color(10, 20, 30); + $this->expectException(ColorException::class); + $color->channel('none'); + } + public function testHueSaturationValueKey(): void { $color = new Color(10, 20, 30); @@ -58,6 +72,12 @@ class ColorTest extends TestCase $this->assertEquals([10, 20, 30], $color->toArray()); } + public function testToHex(): void + { + $color = new Color(16, 100, 100); + $this->assertEquals('ff4400', $color->toHex()); + } + public function testNormalize(): void { $color = new Color(180, 50, 25); diff --git a/tests/Colors/Rgb/ChannelTest.php b/tests/Colors/Rgb/ChannelTest.php index 5f2c45d6..72168bb3 100644 --- a/tests/Colors/Rgb/ChannelTest.php +++ b/tests/Colors/Rgb/ChannelTest.php @@ -32,6 +32,25 @@ class ChannelTest extends TestCase $channel = new Channel(normalized: 2); } + public function testConstructorFail(): void + { + $this->expectException(ColorException::class); + new Channel(300); + } + + public function testToInt(): void + { + $channel = new Channel(255); + $this->assertEquals(255, $channel->toInt()); + } + + public function testToString(): void + { + $channel = new Channel(10); + $this->assertEquals("10", $channel->toString()); + $this->assertEquals("10", (string) $channel); + } + public function testValue(): void { $channel = new Channel(10); diff --git a/tests/Colors/Rgb/Channels/AlphaTest.php b/tests/Colors/Rgb/Channels/AlphaTest.php new file mode 100644 index 00000000..989b751f --- /dev/null +++ b/tests/Colors/Rgb/Channels/AlphaTest.php @@ -0,0 +1,19 @@ +assertEquals('0.333333', $alpha->toString()); + $this->assertEquals('0.333333', (string) $alpha); + } +} diff --git a/tests/Colors/Rgb/ColorTest.php b/tests/Colors/Rgb/ColorTest.php index 8f0d5890..bdd5a7cf 100644 --- a/tests/Colors/Rgb/ColorTest.php +++ b/tests/Colors/Rgb/ColorTest.php @@ -9,6 +9,7 @@ use Intervention\Image\Colors\Rgb\Channels\Green; use Intervention\Image\Colors\Rgb\Channels\Blue; use Intervention\Image\Colors\Rgb\Color as Color; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Tests\TestCase; /** @@ -58,6 +59,13 @@ class ColorTest extends TestCase $this->assertEquals(10, $channel->value()); } + public function testChannelNotFound(): void + { + $color = new Color(10, 20, 30); + $this->expectException(ColorException::class); + $color->channel('none'); + } + public function testRedGreenBlue(): void { $color = new Color(10, 20, 30); @@ -80,6 +88,9 @@ class ColorTest extends TestCase $color = new Color(181, 55, 23); $this->assertEquals('b53717', $color->toHex()); $this->assertEquals('#b53717', $color->toHex('#')); + + $color = new Color(181, 55, 23, 51); + $this->assertEquals('b5371733', $color->toHex()); } public function testNormalize(): void @@ -126,4 +137,13 @@ class ColorTest extends TestCase $this->assertInstanceOf(CmykColor::class, $converted); $this->assertEquals([0, 20, 20, 0], $converted->toArray()); } + + public function testIsGreyscale(): void + { + $color = new Color(255, 0, 100); + $this->assertFalse($color->isGreyscale()); + + $color = new Color(50, 50, 50); + $this->assertTrue($color->isGreyscale()); + } }