Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Utility / ColorTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Utility;
4
5 use Drupal\Component\Utility\Color;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Tests Color utility class conversions.
10  *
11  * @group Utility
12  */
13 class ColorTest extends UnitTestCase {
14
15   /**
16    * Tests Color::hexToRgb().
17    *
18    * @param string $value
19    *   The hex color value.
20    * @param string $expected
21    *   The expected rgb color value.
22    * @param bool $invalid
23    *   Whether this value is invalid and exception should be expected.
24    *
25    * @dataProvider providerTestHexToRgb
26    */
27   public function testHexToRgb($value, $expected, $invalid = FALSE) {
28     if ($invalid) {
29       $this->setExpectedException('InvalidArgumentException');
30     }
31     $this->assertSame($expected, Color::hexToRgb($value));
32   }
33
34   /**
35    * Data provider for testHexToRgb().
36    *
37    * @see testHexToRgb()
38    *
39    * @return array
40    *   An array of arrays containing:
41    *     - The hex color value.
42    *     - The rgb color array value.
43    *     - (optional) Boolean indicating invalid status. Defaults to FALSE.
44    */
45   public function providerTestHexToRgb() {
46     $invalid = [];
47     // Any invalid arguments should throw an exception.
48     foreach (['', '-1', '1', '12', '12345', '1234567', '123456789', '123456789a', 'foo'] as $value) {
49       $invalid[] = [$value, '', TRUE];
50     }
51     // Duplicate all invalid value tests with additional '#' prefix.
52     // The '#' prefix inherently turns the data type into a string.
53     foreach ($invalid as $value) {
54       $invalid[] = ['#' . $value[0], '', TRUE];
55     }
56     // Add invalid data types (hex value must be a string).
57     foreach ([
58       1, 12, 1234, 12345, 123456, 1234567, 12345678, 123456789, 123456789,
59       -1, PHP_INT_MAX, PHP_INT_MAX + 1, -PHP_INT_MAX, 0x0, 0x010
60     ] as $value) {
61       $invalid[] = [$value, '', TRUE];
62     }
63     // And some valid values.
64     $valid = [
65       // Shorthands without alpha.
66       ['hex' => '#000', 'rgb' => ['red' => 0, 'green' => 0, 'blue' => 0]],
67       ['hex' => '#fff', 'rgb' => ['red' => 255, 'green' => 255, 'blue' => 255]],
68       ['hex' => '#abc', 'rgb' => ['red' => 170, 'green' => 187, 'blue' => 204]],
69       ['hex' => 'cba', 'rgb' => ['red' => 204, 'green' => 187, 'blue' => 170]],
70       // Full without alpha.
71       ['hex' => '#000000', 'rgb' => ['red' => 0, 'green' => 0, 'blue' => 0]],
72       ['hex' => '#ffffff', 'rgb' => ['red' => 255, 'green' => 255, 'blue' => 255]],
73       ['hex' => '#010203', 'rgb' => ['red' => 1, 'green' => 2, 'blue' => 3]],
74     ];
75     return array_merge($invalid, $valid);
76   }
77
78   /**
79    * Tests Color::rgbToHex().
80    *
81    * @param string $value
82    *   The rgb color value.
83    * @param string $expected
84    *   The expected hex color value.
85    *
86    * @dataProvider providerTestRbgToHex
87    */
88   public function testRgbToHex($value, $expected) {
89     $this->assertSame($expected, Color::rgbToHex($value));
90   }
91
92   /**
93    * Data provider for testRgbToHex().
94    *
95    * @see testRgbToHex()
96    *
97    * @return array
98    *   An array of arrays containing:
99    *     - The rgb color array value.
100    *     - The hex color value.
101    */
102   public function providerTestRbgToHex() {
103     // Input using named RGB array (e.g., as returned by Color::hexToRgb()).
104     $tests = [
105       [['red' => 0, 'green' => 0, 'blue' => 0], '#000000'],
106       [['red' => 255, 'green' => 255, 'blue' => 255], '#ffffff'],
107       [['red' => 119, 'green' => 119, 'blue' => 119], '#777777'],
108       [['red' => 1, 'green' => 2, 'blue' => 3], '#010203'],
109     ];
110     // Input using indexed RGB array (e.g.: array(10, 10, 10)).
111     foreach ($tests as $test) {
112       $tests[] = [array_values($test[0]), $test[1]];
113     }
114     // Input using CSS RGB string notation (e.g.: 10, 10, 10).
115     foreach ($tests as $test) {
116       $tests[] = [implode(', ', $test[0]), $test[1]];
117     }
118     return $tests;
119   }
120
121 }