Skip to content

ColourUtil tests and small refactors #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function setExitButtonText(string $exitButtonText) : self
return $this;
}

public function setBackgroundColour($colour, string $fallback = null) : self
public function setBackgroundColour(string $colour, string $fallback = null) : self
{
$this->style['bg'] = ColourUtil::validateColour(
$this->terminal,
Expand All @@ -210,7 +210,7 @@ public function setBackgroundColour($colour, string $fallback = null) : self
return $this;
}

public function setForegroundColour($colour, string $fallback = null) : self
public function setForegroundColour(string $colour, string $fallback = null) : self
{
$this->style['fg'] = ColourUtil::validateColour(
$this->terminal,
Expand Down
19 changes: 11 additions & 8 deletions src/MenuStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class MenuStyle
protected $terminal;

/**
* @var int|string
* @var string
*/
protected $fg;

/**
* @var int|string
* @var string
*/
protected $bg;

Expand Down Expand Up @@ -172,8 +172,11 @@ public function __construct(Terminal $terminal = null)
{
$this->terminal = $terminal ?: TerminalFactory::fromSystem();

$this->setFg(static::$defaultStyleValues['fg']);
$this->setBg(static::$defaultStyleValues['bg']);
$this->fg = static::$defaultStyleValues['fg'];
$this->bg = static::$defaultStyleValues['bg'];

$this->generateColoursSetCode();

$this->setWidth(static::$defaultStyleValues['width']);
$this->setPadding(static::$defaultStyleValues['padding']);
$this->setMargin(static::$defaultStyleValues['margin']);
Expand All @@ -199,13 +202,13 @@ public function getDisabledItemText(string $text) : string
*/
private function generateColoursSetCode() : void
{
if (is_string($this->fg)) {
if (!ctype_digit($this->fg)) {
$fgCode = self::$availableForegroundColors[$this->fg];
} else {
$fgCode = sprintf("38;5;%s", $this->fg);
}

if (is_string($this->bg)) {
if (!ctype_digit($this->bg)) {
$bgCode = self::$availableBackgroundColors[$this->bg];
} else {
$bgCode = sprintf("48;5;%s", $this->bg);
Expand Down Expand Up @@ -259,7 +262,7 @@ public function getFg()
return $this->fg;
}

public function setFg($fg, string $fallback = null) : self
public function setFg(string $fg, string $fallback = null) : self
{
$this->fg = ColourUtil::validateColour(
$this->terminal,
Expand All @@ -276,7 +279,7 @@ public function getBg()
return $this->bg;
}

public function setBg($bg, string $fallback = null) : self
public function setBg(string $bg, string $fallback = null) : self
{
$this->bg = ColourUtil::validateColour(
$this->terminal,
Expand Down
40 changes: 23 additions & 17 deletions src/Util/ColourUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class ColourUtil
255 => 'white',
];

public static function getDefaultColoursNames() : array
public static function getDefaultColourNames() : array
{
return static::$defaultColoursNames;
}
Expand All @@ -296,7 +296,7 @@ public static function getDefaultColoursNames() : array
public static function map256To8(int $colourCode) : string
{
if (!isset(static::$coloursMap[$colourCode])) {
throw new \InvalidArgumentException("Invalid colour code");
throw new \InvalidArgumentException('Invalid colour code');
}

return static::$coloursMap[$colourCode];
Expand All @@ -306,22 +306,28 @@ public static function map256To8(int $colourCode) : string
* Check if $colour exists
* If it's a 256-colours code and $terminal doesn't support it, returns a fallback value
*/
public static function validateColour(Terminal $terminal, $colour, string $fallback = null)
public static function validateColour(Terminal $terminal, string $colour, string $fallback = null) : string
{
if (is_int($colour)) {
if ($colour < 0 || $colour > 255) {
throw new \InvalidArgumentException("Invalid colour code");
}
if ($terminal->getColourSupport() < 256) {
if ($fallback !== null) {
Assertion::inArray($fallback, static::getDefaultColoursNames());
return $fallback;
}
return static::map256To8($colour);
}
} else {
Assertion::inArray($colour, static::getDefaultColoursNames());
if (!ctype_digit($colour)) {
return static::validateColourName($colour);
}
return $colour;

Assertion::between($colour, 0, 255, 'Invalid colour code');

if ($terminal->getColourSupport() >= 256) {
return $colour;
}

if ($fallback !== null) {
return static::validateColourName($fallback);
}

return static::map256To8((int) $colour);
}

private static function validateColourName(string $colourName) : string
{
Assertion::inArray($colourName, static::getDefaultColourNames());
return $colourName;
}
}
2 changes: 1 addition & 1 deletion test/CliMenuBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function testSetBgThrowsExceptionWhenColourCodeIsNotInRange() : void

$builder = new CliMenuBuilder;
$builder->setTerminal($terminal);
$builder->setBackgroundColour(-5, 'white');
$builder->setBackgroundColour(257, 'white');
}

public function testDisableDefaultItems() : void
Expand Down
21 changes: 3 additions & 18 deletions test/MenuStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,6 @@ public function testMenuStyleCanBeInstantiatedByCliMenuBuilder() : void
static::assertSame(MenuStyle::class, get_class($style));
}

public function testAvailableColours() : void
{
static::assertSame([
'black',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
'default'
], ColourUtil::getDefaultColoursNames());
}

public function testGetColoursSetCode() : void
{
static::assertSame("\e[37;44m", $this->getMenuStyle()->getColoursSetCode());
Expand Down Expand Up @@ -152,8 +137,8 @@ public function test256ColoursCodes() : void
$style = $this->getMenuStyle(256);
$style->setBg(16, 'white');
$style->setFg(206, 'red');
static::assertSame(16, $style->getBg());
static::assertSame(206, $style->getFg());
static::assertSame('16', $style->getBg());
static::assertSame('206', $style->getFg());
static::assertSame("\033[38;5;206;48;5;16m", $style->getColoursSetCode());

$style = $this->getMenuStyle(8);
Expand All @@ -179,7 +164,7 @@ public function testSetBgThrowsExceptionWhenColourCodeIsNotInRange() : void
$this->expectExceptionMessage('Invalid colour code');

$style = $this->getMenuStyle(256);
$style->setBg(-5, 'white');
$style->setBg(257, 'white');
}

public function testGetMarkerReturnsTheCorrectMarkers() : void
Expand Down
136 changes: 136 additions & 0 deletions test/Util/ColourUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace PhpSchool\CliMenuTest\Util;

use PhpSchool\CliMenu\Util\ColourUtil;
use PhpSchool\Terminal\Terminal;
use PHPUnit\Framework\TestCase;

/**
* @author Aydin Hassan <[email protected]>
*/
class ColourUtilTest extends TestCase
{
public function testAvailableColours() : void
{
self::assertSame(
[
'black',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
'default'
],
ColourUtil::getDefaultColourNames()
);
}

public function testMap256To8ThrowsExceptionIfCodeNotValid() : void
{
self::expectException(\InvalidArgumentException::class);
self::expectExceptionMessage('Invalid colour code');

ColourUtil::map256To8(512);
}

public function testMap256To8() : void
{
self::assertEquals('white', ColourUtil::map256To8(255));
self::assertEquals('magenta', ColourUtil::map256To8(213));
self::assertEquals('yellow', ColourUtil::map256To8(143));
self::assertEquals('blue', ColourUtil::map256To8(103));
self::assertEquals('green', ColourUtil::map256To8(64));
}

public function testValidateColourThrowsExceptionIfColourNot256AndNot8() : void
{
self::expectException(\Assert\InvalidArgumentException::class);

$terminal = $this->createMock(Terminal::class);

ColourUtil::validateColour($terminal, 'teal');
}

public function testValidateColourThrowsExceptionIfFallbackNotValidWhenTerminalDoesNotSupport256Colours() : void
{
self::expectException(\Assert\InvalidArgumentException::class);

$terminal = $this->createMock(Terminal::class);
$terminal->expects($this->once())
->method('getColourSupport')
->willReturn(8);

ColourUtil::validateColour($terminal, 255, 'teal');
}

public function testValidateColourWithFallbackWhenTerminalDoesNotSupport256Colours() : void
{
$terminal = $this->createMock(Terminal::class);
$terminal->expects($this->once())
->method('getColourSupport')
->willReturn(8);

self::assertEquals('red', ColourUtil::validateColour($terminal, 255, 'red'));
}

public function testValidateColourPicksFallbackFromPreComputedListWhenTerminalDoesNotSupport256Colours() : void
{
$terminal = $this->createMock(Terminal::class);
$terminal->expects($this->once())
->method('getColourSupport')
->willReturn(8);

self::assertEquals('yellow', ColourUtil::validateColour($terminal, 148));
}

/**
* @dataProvider invalidColourCodeProvider
*/
public function testValidateColourThrowsExceptionIfInvalid256ColourCodeUsed(int $colourCode) : void
{
self::expectException(\Assert\InvalidArgumentException::class);

ColourUtil::validateColour($this->createMock(Terminal::class), $colourCode);
}

public function invalidColourCodeProvider() : array
{
return [
[-1],
[256],
[1000],
];
}

/**
* @dataProvider validColourCodeProvider
*/
public function testValidateColourWith256ColoursWhenTerminalSupports256Colours(int $colourCode) : void
{
$terminal = $this->createMock(Terminal::class);
$terminal->expects($this->once())
->method('getColourSupport')
->willReturn(256);

self::assertEquals($colourCode, ColourUtil::validateColour($terminal, $colourCode));
}

public function validColourCodeProvider() : array
{
return [
[0],
[255],
[1],
[100],
];
}

public function testValidateColourWithValid8ColourName() : void
{
self::assertEquals('red', ColourUtil::validateColour($this->createMock(Terminal::class), 'red'));
}
}