From 5ee087e711a2bafd2b5ac61b4e94a4b307f92819 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 13 Apr 2024 16:01:54 +0200 Subject: [PATCH] Refactor tests to use data providers --- .../Hsl/Decoders/StringColorDecoderTest.php | 47 +++++--- .../Hsv/Decoders/StringColorDecoderTest.php | 84 +++++++++------ .../Rgb/Decoders/HexColorDecoderTest.php | 100 +++++++++++------- .../Rgb/Decoders/HtmlColornameDecoderTest.php | 37 +++++-- .../Rgb/Decoders/StringColorDecoderTest.php | 82 ++++++++------ 5 files changed, 218 insertions(+), 132 deletions(-) diff --git a/tests/Unit/Colors/Hsl/Decoders/StringColorDecoderTest.php b/tests/Unit/Colors/Hsl/Decoders/StringColorDecoderTest.php index bff8a093..9eda0ef2 100644 --- a/tests/Unit/Colors/Hsl/Decoders/StringColorDecoderTest.php +++ b/tests/Unit/Colors/Hsl/Decoders/StringColorDecoderTest.php @@ -9,29 +9,44 @@ use PHPUnit\Framework\Attributes\RequiresPhpExtension; use Intervention\Image\Colors\Hsl\Color; use Intervention\Image\Colors\Hsl\Decoders\StringColorDecoder; use Intervention\Image\Tests\BaseTestCase; +use PHPUnit\Framework\Attributes\DataProvider; #[RequiresPhpExtension('gd')] #[CoversClass(\Intervention\Image\Colors\Hsl\Decoders\StringColorDecoder::class)] final class StringColorDecoderTest extends BaseTestCase { - public function testDecode(): void + #[DataProvider('decodeDataProvier')] + public function testDecode(string $input, string $classname, array $channelValues): void { $decoder = new StringColorDecoder(); + $result = $decoder->decode($input); + $this->assertInstanceOf($classname, $result); + $this->assertEquals($channelValues, $result->toArray()); + } - $result = $decoder->decode('hsl(0,0,0)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([0, 0, 0], $result->toArray()); - - $result = $decoder->decode('hsl(0, 100, 50)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([0, 100, 50], $result->toArray()); - - $result = $decoder->decode('hsl(360, 100, 50)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([360, 100, 50], $result->toArray()); - - $result = $decoder->decode('hsl(180, 100%, 50%)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([180, 100, 50], $result->toArray()); + public static function decodeDataProvier(): array + { + return [ + [ + 'hsl(0,0,0)', + Color::class, + [0, 0, 0], + ], + [ + 'hsl(0, 100, 50)', + Color::class, + [0, 100, 50], + ], + [ + 'hsl(360, 100, 50)', + Color::class, + [360, 100, 50], + ], + [ + 'hsl(180, 100%, 50%)', + Color::class, + [180, 100, 50], + ] + ]; } } diff --git a/tests/Unit/Colors/Hsv/Decoders/StringColorDecoderTest.php b/tests/Unit/Colors/Hsv/Decoders/StringColorDecoderTest.php index fa63c6ce..8b146d28 100644 --- a/tests/Unit/Colors/Hsv/Decoders/StringColorDecoderTest.php +++ b/tests/Unit/Colors/Hsv/Decoders/StringColorDecoderTest.php @@ -9,50 +9,64 @@ use PHPUnit\Framework\Attributes\RequiresPhpExtension; use Intervention\Image\Colors\Hsv\Color; use Intervention\Image\Colors\Hsv\Decoders\StringColorDecoder; use Intervention\Image\Tests\BaseTestCase; +use PHPUnit\Framework\Attributes\DataProvider; #[RequiresPhpExtension('gd')] #[CoversClass(\Intervention\Image\Colors\Hsv\Decoders\StringColorDecoder::class)] final class StringColorDecoderTest extends BaseTestCase { - public function testDecodeHsv(): void + #[DataProvider('decodeDataProvier')] + public function testDecodeHsv(string $input, string $classname, array $channelValues): void { $decoder = new StringColorDecoder(); - $result = $decoder->decode('hsv(0,0,0)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([0, 0, 0], $result->toArray()); - - $result = $decoder->decode('hsv(0, 100, 100)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([0, 100, 100], $result->toArray()); - - $result = $decoder->decode('hsv(360, 100, 100)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([360, 100, 100], $result->toArray()); - - - $result = $decoder->decode('hsv(180, 100%, 100%)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([180, 100, 100], $result->toArray()); + $result = $decoder->decode($input); + $this->assertInstanceOf($classname, $result); + $this->assertEquals($channelValues, $result->toArray()); } - public function testDecodeHsb(): void + public static function decodeDataProvier(): array { - $decoder = new StringColorDecoder(); - $result = $decoder->decode('hsb(0,0,0)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([0, 0, 0], $result->toArray()); - - $result = $decoder->decode('hsb(0, 100, 100)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([0, 100, 100], $result->toArray()); - - $result = $decoder->decode('hsb(360, 100, 100)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([360, 100, 100], $result->toArray()); - - - $result = $decoder->decode('hsb(180, 100%, 100%)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([180, 100, 100], $result->toArray()); + return [ + [ + 'hsv(0,0,0)', + Color::class, + [0, 0, 0], + ], + [ + 'hsv(0, 100, 100)', + Color::class, + [0, 100, 100], + ], + [ + 'hsv(360, 100, 100)', + Color::class, + [360, 100, 100], + ], + [ + 'hsv(180, 100%, 100%)', + Color::class, + [180, 100, 100], + ], + [ + 'hsb(0,0,0)', + Color::class, + [0, 0, 0], + ], + [ + 'hsb(0, 100, 100)', + Color::class, + [0, 100, 100], + ], + [ + 'hsb(360, 100, 100)', + Color::class, + [360, 100, 100], + ], + [ + 'hsb(180, 100%, 100%)', + Color::class, + [180, 100, 100], + ], + ]; } } diff --git a/tests/Unit/Colors/Rgb/Decoders/HexColorDecoderTest.php b/tests/Unit/Colors/Rgb/Decoders/HexColorDecoderTest.php index 1396b688..fbe13779 100644 --- a/tests/Unit/Colors/Rgb/Decoders/HexColorDecoderTest.php +++ b/tests/Unit/Colors/Rgb/Decoders/HexColorDecoderTest.php @@ -9,52 +9,74 @@ use PHPUnit\Framework\Attributes\RequiresPhpExtension; use Intervention\Image\Colors\Rgb\Color; use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder; use Intervention\Image\Tests\BaseTestCase; +use PHPUnit\Framework\Attributes\DataProvider; #[RequiresPhpExtension('gd')] #[CoversClass(\Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder::class)] final class HexColorDecoderTest extends BaseTestCase { - public function testDecode(): void + #[DataProvider('decodeDataProvier')] + public function testDecode(string $input, string $classname, array $channelValues): void { $decoder = new HexColorDecoder(); - $result = $decoder->decode('ccc'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 255], $result->toArray()); + $result = $decoder->decode($input); + $this->assertInstanceOf($classname, $result); + $this->assertEquals($channelValues, $result->toArray()); + } - $result = $decoder->decode('ccff33'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 255, 51, 255], $result->toArray()); - - $result = $decoder->decode('#ccc'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 255], $result->toArray()); - - $result = $decoder->decode('cccccc'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 255], $result->toArray()); - - $result = $decoder->decode('#cccccc'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 255], $result->toArray()); - - $result = $decoder->decode('#ccccccff'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 255], $result->toArray()); - - $result = $decoder->decode('#cccf'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 255], $result->toArray()); - - $result = $decoder->decode('ccccccff'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 255], $result->toArray()); - - $result = $decoder->decode('cccf'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 255], $result->toArray()); - - $result = $decoder->decode('#b53717aa'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([181, 55, 23, 170], $result->toArray()); + public static function decodeDataProvier(): array + { + return [ + [ + 'ccc', + Color::class, + [204, 204, 204, 255] + ], + [ + 'ccff33', + Color::class, + [204, 255, 51, 255], + ], + [ + '#ccc', + Color::class, + [204, 204, 204, 255], + ], + [ + 'cccccc', + Color::class, + [204, 204, 204, 255], + ], + [ + '#cccccc', + Color::class, + [204, 204, 204, 255], + ], + [ + '#ccccccff', + Color::class, + [204, 204, 204, 255], + ], + [ + '#cccf', + Color::class, + [204, 204, 204, 255], + ], + [ + 'ccccccff', + Color::class, + [204, 204, 204, 255], + ], + [ + 'cccf', + Color::class, + [204, 204, 204, 255], + ], + [ + '#b53717aa', + Color::class, + [181, 55, 23, 170], + ], + ]; } } diff --git a/tests/Unit/Colors/Rgb/Decoders/HtmlColornameDecoderTest.php b/tests/Unit/Colors/Rgb/Decoders/HtmlColornameDecoderTest.php index 0ef15b37..afe418e0 100644 --- a/tests/Unit/Colors/Rgb/Decoders/HtmlColornameDecoderTest.php +++ b/tests/Unit/Colors/Rgb/Decoders/HtmlColornameDecoderTest.php @@ -9,24 +9,39 @@ use PHPUnit\Framework\Attributes\RequiresPhpExtension; use Intervention\Image\Colors\Rgb\Color; use Intervention\Image\Colors\Rgb\Decoders\HtmlColornameDecoder; use Intervention\Image\Tests\BaseTestCase; +use PHPUnit\Framework\Attributes\DataProvider; #[RequiresPhpExtension('gd')] #[CoversClass(\Intervention\Image\Colors\Rgb\Decoders\HtmlColorNameDecoder::class)] final class HtmlColornameDecoderTest extends BaseTestCase { - public function testDecode(): void + #[DataProvider('decodeDataProvier')] + public function testDecode(string $input, string $classname, array $channelValues): void { $decoder = new HtmlColornameDecoder(); - $result = $decoder->decode('salmon'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([250, 128, 114, 255], $result->toArray()); + $result = $decoder->decode($input); + $this->assertInstanceOf($classname, $result); + $this->assertEquals($channelValues, $result->toArray()); + } - $result = $decoder->decode('khaki'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([240, 230, 140, 255], $result->toArray()); - - $result = $decoder->decode('peachpuff'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([255, 218, 185, 255], $result->toArray()); + public static function decodeDataProvier(): array + { + return [ + [ + 'salmon', + Color::class, + [250, 128, 114, 255], + ], + [ + 'khaki', + Color::class, + [240, 230, 140, 255], + ], + [ + 'peachpuff', + Color::class, + [255, 218, 185, 255], + ] + ]; } } diff --git a/tests/Unit/Colors/Rgb/Decoders/StringColorDecoderTest.php b/tests/Unit/Colors/Rgb/Decoders/StringColorDecoderTest.php index 75ba06ce..bf135325 100644 --- a/tests/Unit/Colors/Rgb/Decoders/StringColorDecoderTest.php +++ b/tests/Unit/Colors/Rgb/Decoders/StringColorDecoderTest.php @@ -9,44 +9,64 @@ use PHPUnit\Framework\Attributes\RequiresPhpExtension; use Intervention\Image\Colors\Rgb\Color; use Intervention\Image\Colors\Rgb\Decoders\StringColorDecoder; use Intervention\Image\Tests\BaseTestCase; +use PHPUnit\Framework\Attributes\DataProvider; #[RequiresPhpExtension('gd')] #[CoversClass(\Intervention\Image\Colors\Rgb\Decoders\StringColorDecoder::class)] final class StringColorDecoderTest extends BaseTestCase { - public function testDecode(): void + #[DataProvider('decodeDataProvier')] + public function testDecode(string $input, string $classname, array $channelValues): void { $decoder = new StringColorDecoder(); - $result = $decoder->decode('rgb(204, 204, 204)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 255], $result->toArray()); + $result = $decoder->decode($input); + $this->assertInstanceOf($classname, $result); + $this->assertEquals($channelValues, $result->toArray()); + } - $result = $decoder->decode('rgb(204,204,204)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 255], $result->toArray()); - - $result = $decoder->decode('rgb(100%,20%,0%)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([255, 51, 0, 255], $result->toArray()); - - $result = $decoder->decode('rgb(100%,19.8064%,0.1239483%)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([255, 51, 0, 255], $result->toArray()); - - $result = $decoder->decode('rgba(204, 204, 204, 1)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 255], $result->toArray()); - - $result = $decoder->decode('rgba(204,204,204,.2)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 51], $result->toArray()); - - $result = $decoder->decode('rgba(204,204,204,0.2)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([204, 204, 204, 51], $result->toArray()); - - $result = $decoder->decode('srgb(255, 0, 0)'); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals([255, 0, 0, 255], $result->toArray()); + public static function decodeDataProvier(): array + { + return [ + [ + 'rgb(204, 204, 204)', + Color::class, + [204, 204, 204, 255], + ], + [ + 'rgb(204,204,204)', + Color::class, + [204, 204, 204, 255], + ], + [ + 'rgb(100%,20%,0%)', + Color::class, + [255, 51, 0, 255], + ], + [ + 'rgb(100%,19.8064%,0.1239483%)', + Color::class, + [255, 51, 0, 255], + ], + [ + 'rgba(204, 204, 204, 1)', + Color::class, + [204, 204, 204, 255], + ], + [ + 'rgba(204,204,204,.2)', + Color::class, + [204, 204, 204, 51], + ], + [ + 'rgba(204,204,204,0.2)', + Color::class, + [204, 204, 204, 51], + ], + [ + 'srgb(255, 0, 0)', + Color::class, + [255, 0, 0, 255], + ], + ]; } }