Skip to content

Commit cba1e01

Browse files
authored
Merge pull request #43 from php-school/redraw
Add ability to redraw immediately
2 parents c695214 + 338f342 commit cba1e01

File tree

6 files changed

+169
-2
lines changed

6 files changed

+169
-2
lines changed

src/CliMenu.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\CliMenu\Exception\InvalidInstantiationException;
66
use PhpSchool\CliMenu\Exception\InvalidTerminalException;
7+
use PhpSchool\CliMenu\Exception\MenuNotOpenException;
78
use PhpSchool\CliMenu\MenuItem\LineBreakItem;
89
use PhpSchool\CliMenu\MenuItem\MenuItemInterface;
910
use PhpSchool\CliMenu\MenuItem\StaticItem;
@@ -59,7 +60,7 @@ class CliMenu
5960
* @param array $items
6061
* @param TerminalInterface|null $terminal
6162
* @param MenuStyle|null $style
62-
* @throws InvalidInstantiationException
63+
*
6364
* @throws InvalidTerminalException
6465
*/
6566
public function __construct(
@@ -68,7 +69,6 @@ public function __construct(
6869
TerminalInterface $terminal = null,
6970
MenuStyle $style = null
7071
) {
71-
7272
$this->title = $title;
7373
$this->items = $items;
7474
$this->terminal = $terminal ?: TerminalFactory::fromSystem();
@@ -234,6 +234,18 @@ protected function executeCurrentItem()
234234
}
235235
}
236236

237+
/**
238+
* Redraw the menu
239+
*/
240+
public function redraw()
241+
{
242+
if (!$this->isOpen()) {
243+
throw new MenuNotOpenException;
244+
}
245+
246+
$this->draw();
247+
}
248+
237249
/**
238250
* Draw the menu to STDOUT
239251
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace PhpSchool\CliMenu\Exception;
4+
5+
/**
6+
* @author Aydin Hassan <[email protected]>
7+
*/
8+
class MenuNotOpenException extends \RuntimeException
9+
{
10+
11+
}

test/CliMenuTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace PhpSchool\CliMenuTest;
44

55
use PhpSchool\CliMenu\CliMenu;
6+
use PhpSchool\CliMenu\Exception\MenuNotOpenException;
7+
use PhpSchool\CliMenu\MenuItem\SelectableItem;
68
use PhpSchool\CliMenu\MenuStyle;
9+
use PhpSchool\CliMenu\Terminal\TerminalInterface;
710
use PHPUnit_Framework_TestCase;
811

912
/**
@@ -20,4 +23,100 @@ public function testGetMenuStyle()
2023
$menu = new CliMenu('PHP School FTW', [], null, $style);
2124
static::assertSame($style, $menu->getStyle());
2225
}
26+
27+
public function testReDrawThrowsExceptionIfMenuNotOpen()
28+
{
29+
$menu = new CliMenu('PHP School FTW', []);
30+
31+
$this->expectException(MenuNotOpenException::class);
32+
33+
$menu->reDraw();
34+
}
35+
36+
public function testSimpleOpenClose()
37+
{
38+
$terminal = $this->createMock(TerminalInterface::class);
39+
40+
$terminal->expects($this->any())
41+
->method('isTTY')
42+
->willReturn(true);
43+
44+
$terminal->expects($this->once())
45+
->method('getKeyedInput')
46+
->willReturn('enter');
47+
48+
$terminal->expects($this->any())
49+
->method('getWidth')
50+
->willReturn(50);
51+
52+
$style = $this->getStyle($terminal);
53+
54+
$item = new SelectableItem('Item 1', function (CliMenu $menu) {
55+
$menu->close();
56+
});
57+
58+
$this->expectOutputString(file_get_contents($this->getTestFile()));
59+
60+
$menu = new CliMenu('PHP School FTW', [$item], $terminal, $style);
61+
$menu->open();
62+
}
63+
64+
public function testReDrawReDrawsImmediately()
65+
{
66+
$terminal = $this->createMock(TerminalInterface::class);
67+
68+
$terminal->expects($this->any())
69+
->method('isTTY')
70+
->willReturn(true);
71+
72+
$terminal->expects($this->once())
73+
->method('getKeyedInput')
74+
->willReturn('enter');
75+
76+
$terminal->expects($this->any())
77+
->method('getWidth')
78+
->willReturn(50);
79+
80+
$style = $this->getStyle($terminal);
81+
82+
$item = new SelectableItem('Item 1', function (CliMenu $menu) {
83+
$menu->getStyle()->setBg('red');
84+
$menu->redraw();
85+
$menu->close();
86+
});
87+
88+
$this->expectOutputString(file_get_contents($this->getTestFile()));
89+
90+
$menu = new CliMenu('PHP School FTW', [$item], $terminal, $style);
91+
$menu->open();
92+
}
93+
94+
/**
95+
* @return string
96+
*/
97+
private function getTestFile()
98+
{
99+
return sprintf('%s/res/%s.txt', __DIR__, $this->getName());
100+
}
101+
102+
/**
103+
* @param TerminalInterface $terminal
104+
* @return MenuStyle
105+
*/
106+
private function getStyle(TerminalInterface $terminal)
107+
{
108+
return new MenuStyle(
109+
'blue',
110+
'white',
111+
100,
112+
2,
113+
2,
114+
'',
115+
'',
116+
'',
117+
false,
118+
'=',
119+
$terminal
120+
);
121+
}
23122
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PhpSchool\CliMenuTest\Exception;
4+
5+
use PhpSchool\CliMenu\Exception\MenuNotOpenException;
6+
use PHPUnit_Framework_TestCase;
7+
8+
/**
9+
* @author Aydin Hassan <[email protected]>
10+
*/
11+
class MenuNotOpenExceptionTest extends PHPUnit_Framework_TestCase
12+
{
13+
public function testException()
14+
{
15+
$e = new MenuNotOpenException('error');
16+
$this->assertEquals('error', $e->getMessage());
17+
}
18+
}

test/res/testReDrawReDrawsImmediately.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
 
4+
 PHP School FTW 
5+
 ================================== 
6+
 ● Item 1 
7+
 
8+
9+
10+
11+
12+
 
13+
 PHP School FTW 
14+
 ================================== 
15+
 ● Item 1 
16+
 
17+
18+

test/res/testSimpleOpenClose.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
 
4+
 PHP School FTW 
5+
 ================================== 
6+
 ● Item 1 
7+
 
8+
9+

0 commit comments

Comments
 (0)