Skip to content

Commit

Permalink
Merge pull request #817 from helsingborg-stad/test/helper-color
Browse files Browse the repository at this point in the history
test: helper color
  • Loading branch information
NiclasNorin authored Feb 12, 2024
2 parents 3c67a10 + f11d9ec commit f043b6c
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 19 deletions.
44 changes: 26 additions & 18 deletions library/Helper/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,32 @@ class Color
* Prepare the color and alpha value
*
* @param array $colorItem [
* ['value' => ['color', 'alpha'],
* ['default' => ['color' => '', 'alpha' => '']]
* 'value' => ['color', 'alpha'],
* 'default' => ['color' => '', 'alpha' => '']
* ]
*
* @return string Rgba css color value.
*/
public static function prepareColor($colorItem)
public static function prepareColor(array $colorItem)
{
$colorItem['alpha'] = "1";

$colorItem['alpha'] = "1"; //Set default alpha value
if (
isset($colorItem['value']) && is_array($colorItem['value']) ||
isset($colorItem['default']) && is_array($colorItem['default'])
) {
$defaultColor = !empty($colorItem['default']['color']) ? $colorItem['default']['color'] : "";
$defaultAlpha = !empty($colorItem['default']['alpha']) ? $colorItem['default']['alpha'] : "1";

if (is_array($colorItem['value']) || (empty($colorItem['value']) && is_array($colorItem['default']))) {
//Extra default values for group
$defaultColor = $colorItem['default']['color'] ?? "";
$defaultAlpha = $colorItem['default']['alpha'] . "%" ?? "1";

//Collect set values for group
if (is_array($colorItem['value'])) {
$setColor = array_values($colorItem['value'])[0];
$setAlpha = array_values($colorItem['value'])[1];
} else {
$setColor = $colorItem['value'];
$setAlpha = $colorItem['alpha'];
if (!empty($colorItem['value'])) {
$setColor = array_values($colorItem['value'])[0] ?? '';
$setAlpha = array_values($colorItem['value'])[1] ?? "1";
}

//Define set value else default values
$colorItem['value'] = !empty($setColor) ? $setColor : $defaultColor;
$colorItem['alpha'] = !empty($setAlpha) || $setAlpha == "0" ? $setAlpha . '%' : $defaultAlpha; //empty() returns true on "0"
$colorItem['alpha'] = isset($setAlpha) && $setAlpha == "0" || !empty($setAlpha) ? $setAlpha : $defaultAlpha;
} else {
return false;
}

return self::convertHexToRgba($colorItem['value'], $colorItem['alpha'], $colorItem['default']);
Expand All @@ -56,6 +54,16 @@ private static function convertHexToRgba($value, $alpha, $default)
return "rgba({$value[0]}, {$value[1]}, {$value[2]}, $alpha)";
}

/**
* Get color palettes based on specified options.
*
* @param array $options An array of option names to retrieve color palettes.
* Defaults to common color palette options.
*
* @return array An associative array of color palettes,
* where keys are option names and values are corresponding color palettes.
*
*/
public static function getPalettes(array $options = [
'color_palette_primary',
'color_palette_secondary',
Expand Down
3 changes: 2 additions & 1 deletion patchwork.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"redefinable-internals": [
"extension_loaded"
"extension_loaded",
"class_exists"
]
}
110 changes: 110 additions & 0 deletions tests/phpunit/tests/Helper/Color.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Municipio\Tests\Helper;

use Mockery;
use WP_Mock;
use WP_Mock\Tools\TestCase;
use Municipio\Helper\Color;
use tad\FunctionMocker\FunctionMocker;

/**
* Class ColorTest
*/
class ColorTest extends TestCase
{
/**
* @testdox prepareColor returns an rgba color when getting a correct value array.
*/
public function testPrepareColorReturnsRgbaColor()
{
// When
$result = Color::prepareColor(
[
'value' => ['#000000', '0.5'],
'default' => ['color' => '#ffffff', 'alpha' => '1'],

]
);

// Then
$this->assertEquals('rgba(0, 0, 0, 0.5)', $result);
}

/**
* @testdox prepareColor returns default values as rgba if missing value array data.
*/
public function testPrepareColorReturnsRgbaColorBasedOnDefaultValues()
{
// When
$result = Color::prepareColor(
[
'value' => [],
'default' => ['color' => '#ffffff', 'alpha' => '1'],

]
);

// Then
$this->assertEquals('rgba(255, 255, 255, 1)', $result);
}

/**
* @testdox prepareColor returns false if faulty values.
* @dataProvider prepareColorFalseProvider
*/
public function testPrepareColorReturnsFalseIfFaultyValues($colors)
{
// When
$result = Color::prepareColor(
$colors
);

// Then
$this->assertFalse($result);
}

/**
* @testdox getPalettes returns options if Kirki class is not found.
*/
public function testGetPalettesReturnsArrayWithOptionsIfKirkiDoesNotExist()
{
// Given
$classExistsMock = FunctionMocker::replace('class_exists', false);

// When
$result = Color::getPalettes([ 'option' ]);

// Then
$classExistsMock->wasCalledWithOnce(['Kirki']);
$this->assertEquals('option', $result[0]);
}
/**
* @testdox getPalettes returns array with options
*/
public function testGetPalettesReturnsArrayWithOptions()
{
// Given
$arr = ['key1' => 'value1', 'key2' => 'value2'];
$kirki = Mockery::mock('overload:Kirki');
$kirki->shouldReceive('get_option')->andReturn($arr);

// When
$result = Color::getPalettes([ 'option' ]);

// Then
$this->assertEquals($arr, $result['option']);
}

/**
* Provider
*/
public function prepareColorFalseProvider()
{
return [
[['value' => '']],
[[]],
[['default' => '']],
];
}
}

0 comments on commit f043b6c

Please sign in to comment.