Issues (3627)

CoreBundle/Tests/Unit/Helper/ColorHelperTest.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2015 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Tests\Unit\Helper;
13
14
use Mautic\CoreBundle\Helper\ColorHelper;
15
16
class ColorHelperTest extends \PHPUnit\Framework\TestCase
17
{
18
    /**
19
     * @testdox The helper is instantiated correctly
20
     *
21
     * @covers \Mautic\CoreBundle\Helper\ColorHelper::__construct
22
     */
23
    public function testTheHelperIsInstantiatedWithoutAttributeCorrectly()
24
    {
25
        $helper = new ColorHelper();
26
        $this->assertEquals(0, $helper->getRed());
27
        $this->assertEquals(0, $helper->getGreen());
28
        $this->assertEquals(0, $helper->getBlue());
29
    }
30
31
    /**
32
     * @testdox A color hex hash can be set and the correct RGB representations filled
33
     *
34
     * @covers \Mautic\CoreBundle\Helper\ColorHelper::__construct
35
     */
36
    public function testThatColorHexAreSetCorrectly()
37
    {
38
        $colors = [
39
            '#ccc'    => [204, 204, 204],
40
            '#fff'    => [255, 255, 255],
41
            '#000'    => [0, 0, 0],
42
            '#333333' => [51, 51, 51],
43
            '#369'    => [51, 102, 153],
44
            '#f8Ac30' => [248, 172, 48],
45
        ];
46
47
        foreach ($colors as $hex => $rgb) {
48
            $helper = new ColorHelper($hex);
49
            $this->assertEquals($rgb[0], $helper->getRed());
50
            $this->assertEquals($rgb[1], $helper->getGreen());
51
            $this->assertEquals($rgb[2], $helper->getBlue());
52
        }
53
    }
54
55
    /**
56
     * @testdox A color hex hash can be set and received in the correct and valid hex format
57
     *
58
     * @covers \Mautic\CoreBundle\Helper\ColorHelper::setHex
59
     * @covers \Mautic\CoreBundle\Helper\ColorHelper::toHex
60
     */
61
    public function testThatColorHexAreConvertedBackToHexCorrectly()
62
    {
63
        $colors = [
64
            '#ccc'    => '#cccccc',
65
            '#fff'    => '#ffffff',
66
            '#000'    => '#000000',
67
            '#333333' => '#333333',
68
            '#369'    => '#336699',
69
            '#f8Ac30' => '#f8ac30',
70
        ];
71
72
        foreach ($colors as $hex1 => $hex2) {
73
            $helper = new ColorHelper();
74
            $helper->setHex($hex1);
75
            $this->assertEquals($hex2, $helper->toHex());
76
        }
77
    }
78
79
    /**
80
     * @testdox A color hex hash can be set and received in the correct and valid rgb format
81
     *
82
     * @covers \Mautic\CoreBundle\Helper\ColorHelper::toRgb
83
     */
84
    public function testThatColorHexAreConvertedToRgbCorrectly()
85
    {
86
        $colors = [
87
            '#ccc'    => 'rgb(204,204,204)',
88
            '#fff'    => 'rgb(255,255,255)',
89
            '#000'    => 'rgb(0,0,0)',
90
            '#333333' => 'rgb(51,51,51)',
91
            '#369'    => 'rgb(51,102,153)',
92
            '#f8Ac30' => 'rgb(248,172,48)',
93
        ];
94
95
        foreach ($colors as $hex => $rgb) {
96
            $helper = new ColorHelper($hex);
97
            $this->assertEquals($rgb, $helper->toRgb());
98
        }
99
    }
100
101
    public function testThatRgbColorsAreReturnedCorrectly()
102
    {
103
        $colors = [
104
            '#ccc'    => [
105
                'red'   => 204,
106
                'green' => 204,
107
                'blue'  => 204,
108
            ],
109
            '#369'    => [
110
                'red'   => 51,
111
                'green' => 102,
112
                'blue'  => 153,
113
            ],
114
            '#f8Ac30' => [
115
                'red'   => 248,
116
                'green' => 172,
117
                'blue'  => 48,
118
            ],
119
        ];
120
121
        foreach ($colors as $hex => $colors) {
122
            $helper = new ColorHelper($hex);
123
            $this->assertEquals($colors['red'], $helper->getRed());
124
            $this->assertEquals($colors['green'], $helper->getGreen());
125
            $this->assertEquals($colors['blue'], $helper->getBlue());
126
        }
127
    }
128
129
    /**
130
     * @testdox A color hex hash can be set and received in the correct and valid rgba format
131
     *
132
     * @covers \Mautic\CoreBundle\Helper\ColorHelper::toRgba
133
     */
134
    public function testThatColorHexAreConvertedToRgbaCorrectly()
135
    {
136
        $colors = [
137
            '#ccc'    => 'rgba(204,204,204,%g)',
138
            '#fff'    => 'rgba(255,255,255,%g)',
139
            '#000'    => 'rgba(0,0,0,%g)',
140
            '#333333' => 'rgba(51,51,51,%g)',
141
            '#369'    => 'rgba(51,102,153,%g)',
142
            '#f8Ac30' => 'rgba(248,172,48,%g)',
143
        ];
144
145
        foreach ($colors as $hex => $rgba) {
146
            $helper = new ColorHelper($hex);
147
            $randA  = round((mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax()), 2);
148
            $this->assertEquals(sprintf($rgba, $randA), $helper->toRgba($randA, $randA));
0 ignored issues
show
The call to Mautic\CoreBundle\Helper\ColorHelper::toRgba() has too many arguments starting with $randA. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
            $this->assertEquals(sprintf($rgba, $randA), $helper->/** @scrutinizer ignore-call */ toRgba($randA, $randA));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
149
        }
150
    }
151
152
    /**
153
     * @testdox The random color is within borders and not 0, 0, 0
154
     *
155
     * @covers \Mautic\CoreBundle\Helper\ColorHelper::buildRandomColor
156
     * @covers \Mautic\CoreBundle\Helper\ColorHelper::getColorArray
157
     */
158
    public function testThatRandomColorIsWithinBorders()
159
    {
160
        $helper = new ColorHelper();
161
        $helper->buildRandomColor();
162
        $rgb = $helper->getColorArray();
163
164
        $this->assertGreaterThan(0, $rgb[0]);
165
        $this->assertGreaterThan(0, $rgb[1]);
166
        $this->assertGreaterThan(0, $rgb[2]);
167
168
        $this->assertLessThanOrEqual(256, $rgb[0]);
169
        $this->assertLessThanOrEqual(256, $rgb[1]);
170
        $this->assertLessThanOrEqual(256, $rgb[2]);
171
    }
172
}
173