Skip to content

Commit 761de53

Browse files
authored
Merge pull request #107 from php-school/colour-util-tests
ColourUtil tests and small refactors
2 parents 95d3a41 + 5a07d01 commit 761de53

File tree

6 files changed

+176
-46
lines changed

6 files changed

+176
-46
lines changed

src/CliMenuBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function setExitButtonText(string $exitButtonText) : self
199199
return $this;
200200
}
201201

202-
public function setBackgroundColour($colour, string $fallback = null) : self
202+
public function setBackgroundColour(string $colour, string $fallback = null) : self
203203
{
204204
$this->style['bg'] = ColourUtil::validateColour(
205205
$this->terminal,
@@ -210,7 +210,7 @@ public function setBackgroundColour($colour, string $fallback = null) : self
210210
return $this;
211211
}
212212

213-
public function setForegroundColour($colour, string $fallback = null) : self
213+
public function setForegroundColour(string $colour, string $fallback = null) : self
214214
{
215215
$this->style['fg'] = ColourUtil::validateColour(
216216
$this->terminal,

src/MenuStyle.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ class MenuStyle
2020
protected $terminal;
2121

2222
/**
23-
* @var int|string
23+
* @var string
2424
*/
2525
protected $fg;
2626

2727
/**
28-
* @var int|string
28+
* @var string
2929
*/
3030
protected $bg;
3131

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

175-
$this->setFg(static::$defaultStyleValues['fg']);
176-
$this->setBg(static::$defaultStyleValues['bg']);
175+
$this->fg = static::$defaultStyleValues['fg'];
176+
$this->bg = static::$defaultStyleValues['bg'];
177+
178+
$this->generateColoursSetCode();
179+
177180
$this->setWidth(static::$defaultStyleValues['width']);
178181
$this->setPadding(static::$defaultStyleValues['padding']);
179182
$this->setMargin(static::$defaultStyleValues['margin']);
@@ -199,13 +202,13 @@ public function getDisabledItemText(string $text) : string
199202
*/
200203
private function generateColoursSetCode() : void
201204
{
202-
if (is_string($this->fg)) {
205+
if (!ctype_digit($this->fg)) {
203206
$fgCode = self::$availableForegroundColors[$this->fg];
204207
} else {
205208
$fgCode = sprintf("38;5;%s", $this->fg);
206209
}
207210

208-
if (is_string($this->bg)) {
211+
if (!ctype_digit($this->bg)) {
209212
$bgCode = self::$availableBackgroundColors[$this->bg];
210213
} else {
211214
$bgCode = sprintf("48;5;%s", $this->bg);
@@ -259,7 +262,7 @@ public function getFg()
259262
return $this->fg;
260263
}
261264

262-
public function setFg($fg, string $fallback = null) : self
265+
public function setFg(string $fg, string $fallback = null) : self
263266
{
264267
$this->fg = ColourUtil::validateColour(
265268
$this->terminal,
@@ -276,7 +279,7 @@ public function getBg()
276279
return $this->bg;
277280
}
278281

279-
public function setBg($bg, string $fallback = null) : self
282+
public function setBg(string $bg, string $fallback = null) : self
280283
{
281284
$this->bg = ColourUtil::validateColour(
282285
$this->terminal,

src/Util/ColourUtil.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class ColourUtil
284284
255 => 'white',
285285
];
286286

287-
public static function getDefaultColoursNames() : array
287+
public static function getDefaultColourNames() : array
288288
{
289289
return static::$defaultColoursNames;
290290
}
@@ -296,7 +296,7 @@ public static function getDefaultColoursNames() : array
296296
public static function map256To8(int $colourCode) : string
297297
{
298298
if (!isset(static::$coloursMap[$colourCode])) {
299-
throw new \InvalidArgumentException("Invalid colour code");
299+
throw new \InvalidArgumentException('Invalid colour code');
300300
}
301301

302302
return static::$coloursMap[$colourCode];
@@ -306,22 +306,28 @@ public static function map256To8(int $colourCode) : string
306306
* Check if $colour exists
307307
* If it's a 256-colours code and $terminal doesn't support it, returns a fallback value
308308
*/
309-
public static function validateColour(Terminal $terminal, $colour, string $fallback = null)
309+
public static function validateColour(Terminal $terminal, string $colour, string $fallback = null) : string
310310
{
311-
if (is_int($colour)) {
312-
if ($colour < 0 || $colour > 255) {
313-
throw new \InvalidArgumentException("Invalid colour code");
314-
}
315-
if ($terminal->getColourSupport() < 256) {
316-
if ($fallback !== null) {
317-
Assertion::inArray($fallback, static::getDefaultColoursNames());
318-
return $fallback;
319-
}
320-
return static::map256To8($colour);
321-
}
322-
} else {
323-
Assertion::inArray($colour, static::getDefaultColoursNames());
311+
if (!ctype_digit($colour)) {
312+
return static::validateColourName($colour);
324313
}
325-
return $colour;
314+
315+
Assertion::between($colour, 0, 255, 'Invalid colour code');
316+
317+
if ($terminal->getColourSupport() >= 256) {
318+
return $colour;
319+
}
320+
321+
if ($fallback !== null) {
322+
return static::validateColourName($fallback);
323+
}
324+
325+
return static::map256To8((int) $colour);
326+
}
327+
328+
private static function validateColourName(string $colourName) : string
329+
{
330+
Assertion::inArray($colourName, static::getDefaultColourNames());
331+
return $colourName;
326332
}
327333
}

test/CliMenuBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function testSetBgThrowsExceptionWhenColourCodeIsNotInRange() : void
145145

146146
$builder = new CliMenuBuilder;
147147
$builder->setTerminal($terminal);
148-
$builder->setBackgroundColour(-5, 'white');
148+
$builder->setBackgroundColour(257, 'white');
149149
}
150150

151151
public function testDisableDefaultItems() : void

test/MenuStyleTest.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,6 @@ public function testMenuStyleCanBeInstantiatedByCliMenuBuilder() : void
7474
static::assertSame(MenuStyle::class, get_class($style));
7575
}
7676

77-
public function testAvailableColours() : void
78-
{
79-
static::assertSame([
80-
'black',
81-
'red',
82-
'green',
83-
'yellow',
84-
'blue',
85-
'magenta',
86-
'cyan',
87-
'white',
88-
'default'
89-
], ColourUtil::getDefaultColoursNames());
90-
}
91-
9277
public function testGetColoursSetCode() : void
9378
{
9479
static::assertSame("\e[37;44m", $this->getMenuStyle()->getColoursSetCode());
@@ -152,8 +137,8 @@ public function test256ColoursCodes() : void
152137
$style = $this->getMenuStyle(256);
153138
$style->setBg(16, 'white');
154139
$style->setFg(206, 'red');
155-
static::assertSame(16, $style->getBg());
156-
static::assertSame(206, $style->getFg());
140+
static::assertSame('16', $style->getBg());
141+
static::assertSame('206', $style->getFg());
157142
static::assertSame("\033[38;5;206;48;5;16m", $style->getColoursSetCode());
158143

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

181166
$style = $this->getMenuStyle(256);
182-
$style->setBg(-5, 'white');
167+
$style->setBg(257, 'white');
183168
}
184169

185170
public function testGetMarkerReturnsTheCorrectMarkers() : void

test/Util/ColourUtilTest.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace PhpSchool\CliMenuTest\Util;
4+
5+
use PhpSchool\CliMenu\Util\ColourUtil;
6+
use PhpSchool\Terminal\Terminal;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @author Aydin Hassan <[email protected]>
11+
*/
12+
class ColourUtilTest extends TestCase
13+
{
14+
public function testAvailableColours() : void
15+
{
16+
self::assertSame(
17+
[
18+
'black',
19+
'red',
20+
'green',
21+
'yellow',
22+
'blue',
23+
'magenta',
24+
'cyan',
25+
'white',
26+
'default'
27+
],
28+
ColourUtil::getDefaultColourNames()
29+
);
30+
}
31+
32+
public function testMap256To8ThrowsExceptionIfCodeNotValid() : void
33+
{
34+
self::expectException(\InvalidArgumentException::class);
35+
self::expectExceptionMessage('Invalid colour code');
36+
37+
ColourUtil::map256To8(512);
38+
}
39+
40+
public function testMap256To8() : void
41+
{
42+
self::assertEquals('white', ColourUtil::map256To8(255));
43+
self::assertEquals('magenta', ColourUtil::map256To8(213));
44+
self::assertEquals('yellow', ColourUtil::map256To8(143));
45+
self::assertEquals('blue', ColourUtil::map256To8(103));
46+
self::assertEquals('green', ColourUtil::map256To8(64));
47+
}
48+
49+
public function testValidateColourThrowsExceptionIfColourNot256AndNot8() : void
50+
{
51+
self::expectException(\Assert\InvalidArgumentException::class);
52+
53+
$terminal = $this->createMock(Terminal::class);
54+
55+
ColourUtil::validateColour($terminal, 'teal');
56+
}
57+
58+
public function testValidateColourThrowsExceptionIfFallbackNotValidWhenTerminalDoesNotSupport256Colours() : void
59+
{
60+
self::expectException(\Assert\InvalidArgumentException::class);
61+
62+
$terminal = $this->createMock(Terminal::class);
63+
$terminal->expects($this->once())
64+
->method('getColourSupport')
65+
->willReturn(8);
66+
67+
ColourUtil::validateColour($terminal, 255, 'teal');
68+
}
69+
70+
public function testValidateColourWithFallbackWhenTerminalDoesNotSupport256Colours() : void
71+
{
72+
$terminal = $this->createMock(Terminal::class);
73+
$terminal->expects($this->once())
74+
->method('getColourSupport')
75+
->willReturn(8);
76+
77+
self::assertEquals('red', ColourUtil::validateColour($terminal, 255, 'red'));
78+
}
79+
80+
public function testValidateColourPicksFallbackFromPreComputedListWhenTerminalDoesNotSupport256Colours() : void
81+
{
82+
$terminal = $this->createMock(Terminal::class);
83+
$terminal->expects($this->once())
84+
->method('getColourSupport')
85+
->willReturn(8);
86+
87+
self::assertEquals('yellow', ColourUtil::validateColour($terminal, 148));
88+
}
89+
90+
/**
91+
* @dataProvider invalidColourCodeProvider
92+
*/
93+
public function testValidateColourThrowsExceptionIfInvalid256ColourCodeUsed(int $colourCode) : void
94+
{
95+
self::expectException(\Assert\InvalidArgumentException::class);
96+
97+
ColourUtil::validateColour($this->createMock(Terminal::class), $colourCode);
98+
}
99+
100+
public function invalidColourCodeProvider() : array
101+
{
102+
return [
103+
[-1],
104+
[256],
105+
[1000],
106+
];
107+
}
108+
109+
/**
110+
* @dataProvider validColourCodeProvider
111+
*/
112+
public function testValidateColourWith256ColoursWhenTerminalSupports256Colours(int $colourCode) : void
113+
{
114+
$terminal = $this->createMock(Terminal::class);
115+
$terminal->expects($this->once())
116+
->method('getColourSupport')
117+
->willReturn(256);
118+
119+
self::assertEquals($colourCode, ColourUtil::validateColour($terminal, $colourCode));
120+
}
121+
122+
public function validColourCodeProvider() : array
123+
{
124+
return [
125+
[0],
126+
[255],
127+
[1],
128+
[100],
129+
];
130+
}
131+
132+
public function testValidateColourWithValid8ColourName() : void
133+
{
134+
self::assertEquals('red', ColourUtil::validateColour($this->createMock(Terminal::class), 'red'));
135+
}
136+
}

0 commit comments

Comments
 (0)