ColorTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Drupal\Tests\Component\Utility;
  3. use Drupal\Component\Utility\Color;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * Tests Color utility class conversions.
  7. *
  8. * @group Utility
  9. */
  10. class ColorTest extends TestCase {
  11. /**
  12. * @covers \Drupal\Component\Utility\Color::validateHex
  13. *
  14. * @param bool $expected
  15. * The expected result of validation.
  16. * @param string $value
  17. * The hex color value.
  18. *
  19. * @dataProvider providerTestValidateHex()
  20. */
  21. public function testValidateHex($expected, $value) {
  22. $this->assertSame($expected, Color::validateHex($value));
  23. }
  24. /**
  25. * Provides data for testValidateHex().
  26. */
  27. public function providerTestValidateHex() {
  28. return [
  29. // Tests length.
  30. [FALSE, ''],
  31. [FALSE, '#'],
  32. [FALSE, '1'],
  33. [FALSE, '#1'],
  34. [FALSE, '12'],
  35. [FALSE, '#12'],
  36. [TRUE, '123'],
  37. [TRUE, '#123'],
  38. [FALSE, '1234'],
  39. [FALSE, '#1234'],
  40. [FALSE, '12345'],
  41. [FALSE, '#12345'],
  42. [TRUE, '123456'],
  43. [TRUE, '#123456'],
  44. [FALSE, '1234567'],
  45. [FALSE, '#1234567'],
  46. // Tests valid hex value.
  47. [TRUE, 'abcdef'],
  48. [TRUE, 'ABCDEF'],
  49. [TRUE, 'A0F1B1'],
  50. [FALSE, 'WWW'],
  51. [FALSE, '#123##'],
  52. [FALSE, '@a0055'],
  53. // Tests the data type.
  54. [FALSE, 123456],
  55. // Tests multiple hash prefix.
  56. [FALSE, '###F00'],
  57. // Tests spaces.
  58. [FALSE, ' #123456'],
  59. [FALSE, '123456 '],
  60. [FALSE, '#12 3456'],
  61. ];
  62. }
  63. /**
  64. * Tests Color::hexToRgb().
  65. *
  66. * @param string $value
  67. * The hex color value.
  68. * @param string $expected
  69. * The expected rgb color value.
  70. * @param bool $invalid
  71. * Whether this value is invalid and exception should be expected.
  72. *
  73. * @dataProvider providerTestHexToRgb
  74. */
  75. public function testHexToRgb($value, $expected, $invalid = FALSE) {
  76. if ($invalid) {
  77. $this->expectException('InvalidArgumentException');
  78. }
  79. $this->assertSame($expected, Color::hexToRgb($value));
  80. }
  81. /**
  82. * Data provider for testHexToRgb().
  83. *
  84. * @see testHexToRgb()
  85. *
  86. * @return array
  87. * An array of arrays containing:
  88. * - The hex color value.
  89. * - The rgb color array value.
  90. * - (optional) Boolean indicating invalid status. Defaults to FALSE.
  91. */
  92. public function providerTestHexToRgb() {
  93. $invalid = [];
  94. // Any invalid arguments should throw an exception.
  95. foreach (['', '-1', '1', '12', '12345', '1234567', '123456789', '123456789a', 'foo'] as $value) {
  96. $invalid[] = [$value, '', TRUE];
  97. }
  98. // Duplicate all invalid value tests with additional '#' prefix.
  99. // The '#' prefix inherently turns the data type into a string.
  100. foreach ($invalid as $value) {
  101. $invalid[] = ['#' . $value[0], '', TRUE];
  102. }
  103. // Add invalid data types (hex value must be a string).
  104. foreach ([
  105. 1, 12, 1234, 12345, 123456, 1234567, 12345678, 123456789, 123456789,
  106. -1, PHP_INT_MAX, PHP_INT_MAX + 1, -PHP_INT_MAX, 0x0, 0x010,
  107. ] as $value) {
  108. $invalid[] = [$value, '', TRUE];
  109. }
  110. // And some valid values.
  111. $valid = [
  112. // Shorthands without alpha.
  113. ['hex' => '#000', 'rgb' => ['red' => 0, 'green' => 0, 'blue' => 0]],
  114. ['hex' => '#fff', 'rgb' => ['red' => 255, 'green' => 255, 'blue' => 255]],
  115. ['hex' => '#abc', 'rgb' => ['red' => 170, 'green' => 187, 'blue' => 204]],
  116. ['hex' => 'cba', 'rgb' => ['red' => 204, 'green' => 187, 'blue' => 170]],
  117. // Full without alpha.
  118. ['hex' => '#000000', 'rgb' => ['red' => 0, 'green' => 0, 'blue' => 0]],
  119. ['hex' => '#ffffff', 'rgb' => ['red' => 255, 'green' => 255, 'blue' => 255]],
  120. ['hex' => '#010203', 'rgb' => ['red' => 1, 'green' => 2, 'blue' => 3]],
  121. ];
  122. return array_merge($invalid, $valid);
  123. }
  124. /**
  125. * Tests Color::rgbToHex().
  126. *
  127. * @param string $value
  128. * The rgb color value.
  129. * @param string $expected
  130. * The expected hex color value.
  131. *
  132. * @dataProvider providerTestRbgToHex
  133. */
  134. public function testRgbToHex($value, $expected) {
  135. $this->assertSame($expected, Color::rgbToHex($value));
  136. }
  137. /**
  138. * Data provider for testRgbToHex().
  139. *
  140. * @see testRgbToHex()
  141. *
  142. * @return array
  143. * An array of arrays containing:
  144. * - The rgb color array value.
  145. * - The hex color value.
  146. */
  147. public function providerTestRbgToHex() {
  148. // Input using named RGB array (e.g., as returned by Color::hexToRgb()).
  149. $tests = [
  150. [['red' => 0, 'green' => 0, 'blue' => 0], '#000000'],
  151. [['red' => 255, 'green' => 255, 'blue' => 255], '#ffffff'],
  152. [['red' => 119, 'green' => 119, 'blue' => 119], '#777777'],
  153. [['red' => 1, 'green' => 2, 'blue' => 3], '#010203'],
  154. ];
  155. // Input using indexed RGB array (e.g.: array(10, 10, 10)).
  156. foreach ($tests as $test) {
  157. $tests[] = [array_values($test[0]), $test[1]];
  158. }
  159. // Input using CSS RGB string notation (e.g.: 10, 10, 10).
  160. foreach ($tests as $test) {
  161. $tests[] = [implode(', ', $test[0]), $test[1]];
  162. }
  163. return $tests;
  164. }
  165. /**
  166. * Data provider for testNormalizeHexLength().
  167. *
  168. * @see testNormalizeHexLength()
  169. *
  170. * @return array
  171. * An array of arrays containing:
  172. * - The hex color value.
  173. * - The 6 character length hex color value.
  174. */
  175. public function providerTestNormalizeHexLength() {
  176. $data = [
  177. ['#000', '#000000'],
  178. ['#FFF', '#FFFFFF'],
  179. ['#abc', '#aabbcc'],
  180. ['cba', '#ccbbaa'],
  181. ['#000000', '#000000'],
  182. ['ffffff', '#ffffff'],
  183. ['#010203', '#010203'],
  184. ];
  185. return $data;
  186. }
  187. /**
  188. * Tests Color::normalizeHexLength().
  189. *
  190. * @param string $value
  191. * The input hex color value.
  192. * @param string $expected
  193. * The expected normalized hex color value.
  194. *
  195. * @dataProvider providerTestNormalizeHexLength
  196. */
  197. public function testNormalizeHexLength($value, $expected) {
  198. $this->assertSame($expected, Color::normalizeHexLength($value));
  199. }
  200. }