1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-17 12:18:14 +01:00

Refactor tests to use data providers

This commit is contained in:
Oliver Vogel 2024-04-13 16:01:54 +02:00
parent 2691d92ec5
commit 5ee087e711
No known key found for this signature in database
GPG Key ID: 1B19D214C02D69BB
5 changed files with 218 additions and 132 deletions

View File

@ -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],
]
];
}
}

View File

@ -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],
],
];
}
}

View File

@ -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],
],
];
}
}

View File

@ -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],
]
];
}
}

View File

@ -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],
],
];
}
}