Skip to content

Commit a6989b8

Browse files
authored
Merge pull request #39 from php-school/tests
Fix and add tests
2 parents 69d34ce + cfc7b8f commit a6989b8

File tree

3 files changed

+199
-3
lines changed

3 files changed

+199
-3
lines changed

src/MenuStyle.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function __construct(
154154
$builder = debug_backtrace();
155155
if (count($builder) < 2 || !isset($builder[1]['class']) || $builder[1]['class'] !== $this->allowedConsumer) {
156156
throw new InvalidInstantiationException(
157-
sprintf('The CliMenu must be instantiated by "%s"', $this->allowedConsumer)
157+
sprintf('The MenuStyle must be instantiated by "%s"', $this->allowedConsumer)
158158
);
159159
}
160160

@@ -427,6 +427,14 @@ public function getMarker($selected)
427427
return $selected ? $this->selectedMarker : $this->unselectedMarker;
428428
}
429429

430+
/**
431+
* @param string $itemExtra
432+
*/
433+
public function setItemExtra($itemExtra)
434+
{
435+
$this->itemExtra = $itemExtra;
436+
}
437+
430438
/**
431439
* @return string
432440
*/

test/CliMenuBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public function testModifyStyles()
6363
$builder->setSelectedMarker('x');
6464
$builder->setItemExtra('*');
6565
$builder->setTitleSeparator('-');
66-
67-
$terminal = $this->createMock(TerminalInterface::class);
66+
67+
$terminal = static::createMock(TerminalInterface::class);
6868
$terminal
6969
->expects($this->once())
7070
->method('getWidth')

test/MenuStyleTest.php

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
3+
namespace PhpSchool\CliMenuTest;
4+
5+
use PhpSchool\CliMenu\CliMenuBuilder;
6+
use PhpSchool\CliMenu\Exception\InvalidInstantiationException;
7+
use PhpSchool\CliMenu\MenuStyle;
8+
use PhpSchool\CliMenu\Terminal\UnixTerminal;
9+
use PHPUnit_Framework_TestCase;
10+
11+
/**
12+
* Class MenuStyleTest
13+
* @author Michael Woodward <mikeymike.mw@gmail.com>
14+
*/
15+
class MenuStyleTest extends PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @return MenuStyle
19+
*/
20+
private function getMenuStyle()
21+
{
22+
// Use the CliMenuBuilder & reflection to get the style Obj
23+
$builder = new CliMenuBuilder();
24+
$menu = $builder->build();
25+
26+
$reflectionMenu = new \ReflectionObject($menu);
27+
$styleProperty = $reflectionMenu->getProperty('style');
28+
$styleProperty->setAccessible(true);
29+
$style = $styleProperty->getValue($menu);
30+
31+
$reflectionStyle = new \ReflectionObject($style);
32+
$terminalProperty = $reflectionStyle->getProperty('terminal');
33+
$terminalProperty->setAccessible(true);
34+
$terminalProperty->setValue($style, $this->getMockTerminal());
35+
36+
// Force recalculate terminal widths now terminal is set
37+
$style->setWidth(100);
38+
39+
return $style;
40+
}
41+
42+
private function getMockTerminal()
43+
{
44+
$terminal = $this
45+
->getMockBuilder(UnixTerminal::class)
46+
->disableOriginalConstructor()
47+
->setMethods(['getWidth'])
48+
->getMock();
49+
50+
$terminal
51+
->expects(static::any())
52+
->method('getWidth')
53+
->will(static::returnValue(500));
54+
55+
return $terminal;
56+
}
57+
58+
public function testMenuStyleCannotBeInstantiatedByDisallowedConsumers()
59+
{
60+
$this->setExpectedException(
61+
InvalidInstantiationException::class,
62+
'The MenuStyle must be instantiated by "PhpSchool\CliMenu\CliMenuBuilder"'
63+
);
64+
65+
new MenuStyle();
66+
}
67+
68+
public function testMenuStyleCanBeInstantiatedByCliMenuBuilder()
69+
{
70+
$builder = new CliMenuBuilder();
71+
$menu = $builder->build();
72+
73+
$reflectionMenu = new \ReflectionObject($menu);
74+
static::assertTrue($reflectionMenu->hasProperty('style'));
75+
76+
$styleProperty = $reflectionMenu->getProperty('style');
77+
$styleProperty->setAccessible(true);
78+
$style = $styleProperty->getValue($menu);
79+
80+
static::assertSame(MenuStyle::class, get_class($style));
81+
}
82+
83+
public function testAvailableColours()
84+
{
85+
static::assertSame([
86+
'black',
87+
'red',
88+
'green',
89+
'yellow',
90+
'blue',
91+
'magenta',
92+
'cyan',
93+
'white',
94+
'default'
95+
], MenuStyle::getAvailableColours());
96+
}
97+
98+
public function testGetSelectedSetCode()
99+
{
100+
static::assertSame("\e[47;34m", $this->getMenuStyle()->getSelectedSetCode());
101+
}
102+
103+
public function testGetSelectedUnsetCode()
104+
{
105+
static::assertSame("\e[49;39m", $this->getMenuStyle()->getSelectedUnsetCode());
106+
}
107+
108+
public function testGetUnselectedSetCode()
109+
{
110+
static::assertSame("\e[44;37m", $this->getMenuStyle()->getUnselectedSetCode());
111+
}
112+
113+
public function testGetUnselectedUnsetCode()
114+
{
115+
static::assertSame("\e[49;39m", $this->getMenuStyle()->getUnselectedUnsetCode());
116+
}
117+
118+
public function testGetterAndSetters()
119+
{
120+
$style = $this->getMenuStyle();
121+
122+
static::assertSame('blue', $style->getBg());
123+
static::assertSame('white', $style->getFg());
124+
static::assertSame('', $style->getUnselectedMarker());
125+
static::assertSame('', $style->getSelectedMarker());
126+
static::assertSame('', $style->getItemExtra());
127+
static::assertFalse($style->getDisplaysExtra());
128+
static::assertSame('=', $style->getTitleSeparator());
129+
static::assertSame(100, $style->getWidth());
130+
static::assertSame(2, $style->getMargin());
131+
static::assertSame(2, $style->getPadding());
132+
133+
$style->setBg('red');
134+
$style->setFg('yellow');
135+
$style->setUnselectedMarker('-');
136+
$style->setSelectedMarker('>');
137+
$style->setItemExtra('EXTRA!');
138+
$style->setDisplaysExtra(true);
139+
$style->setTitleSeparator('+');
140+
$style->setWidth(200);
141+
$style->setMargin(10);
142+
$style->setPadding(10);
143+
144+
static::assertSame('red', $style->getBg());
145+
static::assertSame('yellow', $style->getFg());
146+
static::assertSame('-', $style->getUnselectedMarker());
147+
static::assertSame('>', $style->getSelectedMarker());
148+
static::assertSame('EXTRA!', $style->getItemExtra());
149+
static::assertTrue($style->getDisplaysExtra());
150+
static::assertSame('+', $style->getTitleSeparator());
151+
static::assertSame(200, $style->getWidth());
152+
static::assertSame(10, $style->getMargin());
153+
static::assertSame(10, $style->getPadding());
154+
}
155+
156+
public function testGetMarkerReturnsTheCorrectMarkers()
157+
{
158+
$style = $this->getMenuStyle();
159+
160+
$style->setSelectedMarker('>');
161+
$style->setUnselectedMarker('x');
162+
163+
static::assertSame('>', $style->getMarker(true));
164+
static::assertSame('x', $style->getMarker(false));
165+
}
166+
167+
public function testWidthCalculation()
168+
{
169+
$style = $this->getMenuStyle();
170+
171+
$style->setWidth(300);
172+
$style->setPadding(5);
173+
$style->setMargin(5);
174+
175+
static::assertSame(280, $style->getContentWidth());
176+
}
177+
178+
public function testRightHandPaddingCalculation()
179+
{
180+
$style = $this->getMenuStyle();
181+
182+
$style->setWidth(300);
183+
$style->setPadding(5);
184+
$style->setMargin(5);
185+
186+
static::assertSame(235, $style->getRightHandPadding(50));
187+
}
188+
}

0 commit comments

Comments
 (0)