11<?php
2+
23namespace PackageFactory \ColorHelper \Tests \Unit ;
34
4- use PHPUnit \Framework \TestCase ;
55use PackageFactory \ColorHelper \Domain \ValueObject \ColorInterface ;
66use PackageFactory \ColorHelper \Domain \ValueObject \HslaColor ;
77use PackageFactory \ColorHelper \Domain \ValueObject \RgbaColor ;
8+ use PHPUnit \Framework \TestCase ;
89use Symfony \Component \Yaml \Yaml ;
910
1011abstract class AbstractColorTest extends TestCase
1112{
1213 /**
13- * Return a set of colors as hls rgb and hex
14+ * Return a set of colors as hls rgb and hex.
15+ *
1416 * @return array
1517 */
1618 public function getColorFixtures (): array
@@ -34,62 +36,65 @@ function ($item) {
3436 }
3537
3638 /**
37- * Create a full range of rgb values to test the full color spectrum
39+ * Create a full range of rgb values to test the full color spectrum.
40+ *
3841 * @return array
3942 */
40- public function rgbSpectrumDataProvider ():array
43+ public function rgbSpectrumDataProvider (): array
4144 {
4245 $ interval = 20 ;
4346 $ testArgumentSets = [];
44- for ($ r = 0 ; $ r < 256 ; $ r+= $ interval ) {
45- for ($ g = 0 ; $ g < 256 ; $ g+= $ interval ) {
46- for ($ b = 0 ; $ b < 256 ; $ b+= $ interval ) {
47- $ testArgumentSets [] = [$ r ,$ g ,$ b ];
47+ for ($ r = 0 ; $ r < 256 ; $ r += $ interval ) {
48+ for ($ g = 0 ; $ g < 256 ; $ g += $ interval ) {
49+ for ($ b = 0 ; $ b < 256 ; $ b += $ interval ) {
50+ $ testArgumentSets [] = [$ r , $ g , $ b ];
4851 }
4952 }
5053 }
54+
5155 return $ testArgumentSets ;
5256 }
5357
5458 /**
55- * Create a full range of hsl values to test the full color spectrum
59+ * Create a full range of hsl values to test the full color spectrum.
60+ *
5661 * @return array
5762 */
58- public function hlsSpectrumDataProvider ():array
63+ public function hlsSpectrumDataProvider (): array
5964 {
6065 $ interval = 20 ;
6166 $ start = 5 ;
6267
6368 $ testArgumentSets = [];
64- for ($ h = $ start ; $ h < 360 ; $ h+= $ interval ) {
65- for ($ l =$ start ; $ l < 100 ; $ l+= $ interval ) {
66- for ($ s = $ start ; $ s < 100 ; $ s+= $ interval ) {
69+ for ($ h = $ start ; $ h < 360 ; $ h += $ interval ) {
70+ for ($ l = $ start ; $ l < 100 ; $ l += $ interval ) {
71+ for ($ s = $ start ; $ s < 100 ; $ s += $ interval ) {
6772 $ testArgumentSets [] = [$ h , $ l , $ s ];
6873 }
6974 }
7075 }
7176
7277 // test the extremes
73- $ testArgumentSets [] = [0 ,0 , 0 ];
74- $ testArgumentSets [] = [0 ,0 , 100 ];
78+ $ testArgumentSets [] = [0 , 0 , 0 ];
79+ $ testArgumentSets [] = [0 , 0 , 100 ];
7580
7681 return $ testArgumentSets ;
7782 }
7883
79-
8084 /**
8185 * @param ColorInterface $expected
8286 * @param ColorInterface $color
8387 */
84- protected function assertSimilarColor (ColorInterface $ expected , ColorInterface $ color , string $ message = null ) {
88+ protected function assertSimilarColor (ColorInterface $ expected , ColorInterface $ color , string $ message = null )
89+ {
8590 $ this ->addToAssertionCount (1 );
8691 if ($ expected instanceof RgbaColor) {
8792 if (!$ this ->isSimilarRgba ($ expected , $ color )) {
88- $ this ->fail ( $ message ?? $ color ->getRgbaString () . ' is not similar to ' . $ expected ->getRgbaString ());
93+ $ this ->fail ($ message ?? $ color ->getRgbaString (). ' is not similar to ' . $ expected ->getRgbaString ());
8994 }
9095 } elseif ($ expected instanceof HslaColor) {
9196 if (!$ this ->isSimilarHsla ($ expected , $ color )) {
92- $ this ->fail ( $ message ?? $ color ->getHslaString () . ' is not similar to ' . $ expected ->getHslaString ());
97+ $ this ->fail ($ message ?? $ color ->getHslaString (). ' is not similar to ' . $ expected ->getHslaString ());
9398 }
9499 }
95100 }
@@ -98,18 +103,19 @@ protected function assertSimilarColor(ColorInterface $expected, ColorInterface $
98103 * @param ColorInterface $expected
99104 * @param ColorInterface $color
100105 */
101- protected function assertSameColor (ColorInterface $ expected , ColorInterface $ color , string $ message = null ) {
106+ protected function assertSameColor (ColorInterface $ expected , ColorInterface $ color , string $ message = null )
107+ {
102108 $ this ->addToAssertionCount (1 );
103109 if (get_class ($ expected ) != get_class ($ color )) {
104- $ this ->fail ( $ message ?? get_class ($ expected ) . ' is not the same as ' . get_class ($ color ));
110+ $ this ->fail ($ message ?? get_class ($ expected ). ' is not the same as ' . get_class ($ color ));
105111 } elseif ($ color ->equals ($ expected ) == false ) {
106112 if ($ expected instanceof RgbaColor) {
107113 if (!$ this ->isSimilarRgba ($ expected , $ color , 0 )) {
108- $ this ->fail ( $ message ?? $ color ->getRgbaString () . ' is not equal to ' . $ expected ->getRgbaString ());
114+ $ this ->fail ($ message ?? $ color ->getRgbaString (). ' is not equal to ' . $ expected ->getRgbaString ());
109115 }
110116 } elseif ($ expected instanceof HslaColor) {
111117 if (!$ this ->isSimilarHsla ($ expected , $ color , 0 )) {
112- $ this ->fail ( $ message ?? $ color ->getHslaString () . ' is not equal to ' . $ expected ->getHslaString ());
118+ $ this ->fail ($ message ?? $ color ->getHslaString (). ' is not equal to ' . $ expected ->getHslaString ());
113119 }
114120 }
115121 }
@@ -118,7 +124,8 @@ protected function assertSameColor(ColorInterface $expected, ColorInterface $col
118124 /**
119125 * @param ColorInterface $a
120126 * @param ColorInterface $a
121- * @param float $maxDist
127+ * @param float $maxDist
128+ *
122129 * @return bool
123130 */
124131 protected function isSimilarHsla (ColorInterface $ a , ColorInterface $ b , float $ maxDist = 5 ): bool
@@ -129,29 +136,29 @@ protected function isSimilarHsla(ColorInterface $a, ColorInterface $b, float $ma
129136 $ deltaH1 = abs ($ b ->getHue () - $ a ->getHue ());
130137 $ deltH12 = 360 - $ a ->getHue () + $ b ->getHue ();
131138
132- return (
139+ return
133140 min ($ deltaH1 , $ deltH12 ) < $ maxDist
134141 && abs ($ b ->getSaturation () - $ a ->getSaturation ()) < $ maxDist
135142 && abs ($ b ->getLightness () - $ a ->getLightness ()) < $ maxDist
136- && abs ($ b ->getAlpha () - $ a ->getAlpha ()) < ($ maxDist / 100 )
137- );
143+ && abs ($ b ->getAlpha () - $ a ->getAlpha ()) < ($ maxDist / 100 );
138144 }
139145
140146 /**
141147 * @param ColorInterface $a
142148 * @param ColorInterface $a
143- * @param float $maxDist
149+ * @param float $maxDist
150+ *
144151 * @return bool
145152 */
146153 public function isSimilarRgba (ColorInterface $ a , ColorInterface $ b , float $ maxDist = 5 ): bool
147154 {
148155 $ a = $ a ->asRgba ();
149156 $ b = $ b ->asRgba ();
150- return (
157+
158+ return
151159 abs ($ b ->getRed () - $ a ->getRed ()) < $ maxDist
152160 && abs ($ b ->getGreen () - $ a ->getGreen ()) < $ maxDist
153161 && abs ($ b ->getBlue () - $ a ->getBlue ()) < $ maxDist
154- && abs ($ b ->getAlpha () - $ a ->getAlpha ()) < $ maxDist
155- );
162+ && abs ($ b ->getAlpha () - $ a ->getAlpha ()) < $ maxDist ;
156163 }
157164}
0 commit comments