-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathColorTest.php
More file actions
114 lines (101 loc) · 2.74 KB
/
ColorTest.php
File metadata and controls
114 lines (101 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Unit tests for Color value object.
*
* @author M. Yilmaz SUSLU <yilmazsuslu@gmail.com>
* @license MIT
*
* @since Oct 2016
*/
namespace Test\Embeddable;
use DDD\Embeddable\Color;
use InvalidArgumentException;
use JsonSerializable;
use PHPUnit\Framework\TestCase;
class ColorTest extends TestCase
{
/**
* @dataProvider badHexCodes
*/
public function testInvalidHexNotAccepted(mixed $hex) : void
{
$this->expectException(InvalidArgumentException::class);
new Color($hex);
}
/**
* @dataProvider goodHexCodes
*/
public function testAlwaysNormalizeHexValues(string $val, string $normalized) : void
{
$color = new Color($val);
$this->assertSame($normalized, (string)$color);
}
public function testRGBConversionWorks() : void
{
// Given
$underTest = new Color('FFF');
// When then
$this->assertSame([255, 255, 255], $underTest->toRGB());
$this->assertSame('rgb(255,255,255)', $underTest->toRGBString());
$this->assertInstanceOf(JsonSerializable::class, $underTest);
$this->assertSame($underTest->jsonSerialize(), $underTest->toArray());
}
public function testFromRgb() : void
{
$actual = Color::fromRGB(255, 255, 255)->toHex();
$this->assertSame('#FFFFFF', $actual);
}
public function testEmptyState() : void
{
// Given
$underTest = new Color();
// When then
$this->assertSame([], $underTest->toArray());
$this->assertSame([], $underTest->toRGB());
$this->assertSame('', (string)$underTest);
}
/**
* @dataProvider sampleColorNames
*/
public function testSerializedColorHasAName(string $hex, string $expectedName) : void
{
$arr = (new Color($hex))->toArray();
$this->assertSame($expectedName, $arr['name']);
}
public function goodHexCodes() : array
{
return [
['#CCA', '#CCCCAA'],
['#cca', '#CCCCAA'],
['#ccccaa', '#CCCCAA'],
['CCA', '#CCCCAA'],
['c9a', '#CC99AA'],
['aba', '#AABBAA'],
['000', '#000000'],
['9A4', '#99AA44'],
];
}
public function badHexCodes() : array
{
return [
['#FFFA'],
['#CACAC'],
['TXDFA8'],
['#TXDFA8'],
[-1],
['rgb(1,2,3)'],
['Hello'],
];
}
public function sampleColorNames() : array
{
return [
['CCCCCC', 'Pastel Gray'],
['FF0000', 'Red'],
['0F0', 'Electric Green'],
['00F', 'Blue'],
['FF00FF', 'Fuchsia'],
['FE00FE', 'Fuchsia'],
];
}
}