Skip to content

Add ability to redraw immediately #43

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 2 commits into from
Oct 4, 2016
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
16 changes: 14 additions & 2 deletions src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpSchool\CliMenu\Exception\InvalidInstantiationException;
use PhpSchool\CliMenu\Exception\InvalidTerminalException;
use PhpSchool\CliMenu\Exception\MenuNotOpenException;
use PhpSchool\CliMenu\MenuItem\LineBreakItem;
use PhpSchool\CliMenu\MenuItem\MenuItemInterface;
use PhpSchool\CliMenu\MenuItem\StaticItem;
Expand Down Expand Up @@ -59,7 +60,7 @@ class CliMenu
* @param array $items
* @param TerminalInterface|null $terminal
* @param MenuStyle|null $style
* @throws InvalidInstantiationException
*
* @throws InvalidTerminalException
*/
public function __construct(
Expand All @@ -68,7 +69,6 @@ public function __construct(
TerminalInterface $terminal = null,
MenuStyle $style = null
) {

$this->title = $title;
$this->items = $items;
$this->terminal = $terminal ?: TerminalFactory::fromSystem();
Expand Down Expand Up @@ -234,6 +234,18 @@ protected function executeCurrentItem()
}
}

/**
* Redraw the menu
*/
public function redraw()
{
if (!$this->isOpen()) {
throw new MenuNotOpenException;
}

$this->draw();
}

/**
* Draw the menu to STDOUT
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/MenuNotOpenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace PhpSchool\CliMenu\Exception;

/**
* @author Aydin Hassan <[email protected]>
*/
class MenuNotOpenException extends \RuntimeException
{

}
99 changes: 99 additions & 0 deletions test/CliMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace PhpSchool\CliMenuTest;

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Exception\MenuNotOpenException;
use PhpSchool\CliMenu\MenuItem\SelectableItem;
use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\CliMenu\Terminal\TerminalInterface;
use PHPUnit_Framework_TestCase;

/**
Expand All @@ -20,4 +23,100 @@ public function testGetMenuStyle()
$menu = new CliMenu('PHP School FTW', [], null, $style);
static::assertSame($style, $menu->getStyle());
}

public function testReDrawThrowsExceptionIfMenuNotOpen()
{
$menu = new CliMenu('PHP School FTW', []);

$this->expectException(MenuNotOpenException::class);

$menu->reDraw();
}

public function testSimpleOpenClose()
{
$terminal = $this->createMock(TerminalInterface::class);

$terminal->expects($this->any())
->method('isTTY')
->willReturn(true);

$terminal->expects($this->once())
->method('getKeyedInput')
->willReturn('enter');

$terminal->expects($this->any())
->method('getWidth')
->willReturn(50);

$style = $this->getStyle($terminal);

$item = new SelectableItem('Item 1', function (CliMenu $menu) {
$menu->close();
});

$this->expectOutputString(file_get_contents($this->getTestFile()));

$menu = new CliMenu('PHP School FTW', [$item], $terminal, $style);
$menu->open();
}

public function testReDrawReDrawsImmediately()
{
$terminal = $this->createMock(TerminalInterface::class);

$terminal->expects($this->any())
->method('isTTY')
->willReturn(true);

$terminal->expects($this->once())
->method('getKeyedInput')
->willReturn('enter');

$terminal->expects($this->any())
->method('getWidth')
->willReturn(50);

$style = $this->getStyle($terminal);

$item = new SelectableItem('Item 1', function (CliMenu $menu) {
$menu->getStyle()->setBg('red');
$menu->redraw();
$menu->close();
});

$this->expectOutputString(file_get_contents($this->getTestFile()));

$menu = new CliMenu('PHP School FTW', [$item], $terminal, $style);
$menu->open();
}

/**
* @return string
*/
private function getTestFile()
{
return sprintf('%s/res/%s.txt', __DIR__, $this->getName());
}

/**
* @param TerminalInterface $terminal
* @return MenuStyle
*/
private function getStyle(TerminalInterface $terminal)
{
return new MenuStyle(
'blue',
'white',
100,
2,
2,
'○',
'●',
'✔',
false,
'=',
$terminal
);
}
}
18 changes: 18 additions & 0 deletions test/Exception/MenuNotOpenExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PhpSchool\CliMenuTest\Exception;

use PhpSchool\CliMenu\Exception\MenuNotOpenException;
use PHPUnit_Framework_TestCase;

/**
* @author Aydin Hassan <[email protected]>
*/
class MenuNotOpenExceptionTest extends PHPUnit_Framework_TestCase
{
public function testException()
{
$e = new MenuNotOpenException('error');
$this->assertEquals('error', $e->getMessage());
}
}
18 changes: 18 additions & 0 deletions test/res/testReDrawReDrawsImmediately.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


 
 PHP School FTW 
 ================================== 
 ● Item 1 
 




 
 PHP School FTW 
 ================================== 
 ● Item 1 
 


Expand Down
9 changes: 9 additions & 0 deletions test/res/testSimpleOpenClose.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


 
 PHP School FTW 
 ================================== 
 ● Item 1 
 


Expand Down