Skip to content

Commit 06f04c9

Browse files
committed
Unit tests for inputs
1 parent 3652302 commit 06f04c9

File tree

10 files changed

+462
-18
lines changed

10 files changed

+462
-18
lines changed

src/Input/Input.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ public function setPlaceholderText(string $placeholderText) : Input;
2323

2424
public function getPlaceholderText() : string;
2525

26-
public function format(string $value) : string;
26+
public function filter(string $value) : string;
2727
}

src/Input/InputIO.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function collect(Input $input) : InputResult
7676

7777
if (!empty($this->callbacks[$userInput])) {
7878
foreach ($this->callbacks[$userInput] as $callback) {
79-
$inputValue = $callback($this, $inputValue);
79+
$inputValue = $callback($inputValue);
8080
$this->drawInput($input, $inputValue);
8181
}
8282
continue;
@@ -87,13 +87,10 @@ public function collect(Input $input) : InputResult
8787
}
8888
}
8989

90-
public function registerInputMap(string $input, string $mapTo) : void
91-
{
92-
$this->inputMap[$input] = $mapTo;
93-
}
94-
9590
public function registerControlCallback(string $control, callable $callback) : void
9691
{
92+
$this->inputMap[$control] = $control;
93+
9794
if (!isset($this->callbacks[$control])) {
9895
$this->callbacks[$control] = [];
9996
}
@@ -208,7 +205,7 @@ private function drawInput(Input $input, string $userInput) : void
208205
$this->drawEmptyLine($input, $userInput);
209206
$this->drawTitle($input, $userInput);
210207
$this->drawEmptyLine($input, $userInput);
211-
$this->drawInputField($input, $input->format($userInput));
208+
$this->drawInputField($input, $input->filter($userInput));
212209
$this->drawEmptyLine($input, $userInput);
213210
}
214211

@@ -219,7 +216,7 @@ private function drawInputWithError(Input $input, string $userInput) : void
219216
$this->drawEmptyLine($input, $userInput);
220217
$this->drawTitle($input, $userInput);
221218
$this->drawEmptyLine($input, $userInput);
222-
$this->drawInputField($input, $input->format($userInput));
219+
$this->drawInputField($input, $input->filter($userInput));
223220
$this->drawEmptyLine($input, $userInput);
224221
$this->drawCenteredLine(
225222
$input,

src/Input/Number.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,11 @@ public function getPlaceholderText() : string
7070

7171
public function ask() : InputResult
7272
{
73-
$this->inputIO->registerInputMap("\033[A", 'up');
74-
$this->inputIO->registerInputMap("\033[B", 'down');
75-
76-
$this->inputIO->registerControlCallback('up', function (InputIO $inputIO, string $input) {
73+
$this->inputIO->registerControlCallback("\033[A", function (string $input) {
7774
return $this->validate($input) ? $input + 1 : $input;
7875
});
7976

80-
$this->inputIO->registerControlCallback('down', function (InputIO $inputIO, string $input) {
77+
$this->inputIO->registerControlCallback("\033[B", function (string $input) {
8178
return $this->validate($input) ? $input - 1 : $input;
8279
});
8380

@@ -89,7 +86,7 @@ public function validate(string $input) : bool
8986
return (bool) preg_match('/^\d+$/', $input);
9087
}
9188

92-
public function format(string $value) : string
89+
public function filter(string $value) : string
9390
{
9491
return $value;
9592
}

src/Input/Password.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ public function validate(string $input) : bool
9090
return $validator($input);
9191
}
9292

93-
return mb_strlen($input) > 16;
93+
return mb_strlen($input) >= 16;
9494
}
9595

96-
public function format(string $value) : string
96+
public function filter(string $value) : string
9797
{
9898
return str_repeat('*', mb_strlen($value));
9999
}

src/Input/Text.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function validate(string $input) : bool
7878
return !empty($input);
7979
}
8080

81-
public function format(string $value) : string
81+
public function filter(string $value) : string
8282
{
8383
return $value;
8484
}

test/Input/InputIOTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace PhpSchool\CliMenuTest\Input;
4+
5+
use PhpSchool\CliMenu\CliMenu;
6+
use PhpSchool\CliMenu\Input\InputIO;
7+
use PhpSchool\CliMenu\Input\Text;
8+
use PhpSchool\CliMenu\MenuStyle;
9+
use PhpSchool\CliMenu\Terminal\TerminalInterface;
10+
use PHPUnit\Framework\TestCase;
11+
12+
/**
13+
* @author Aydin Hassan <[email protected]>
14+
*/
15+
class InputIOTest extends TestCase
16+
{
17+
/**
18+
* @var TerminalInterface
19+
*/
20+
private $terminal;
21+
22+
/**
23+
* @var CliMenu
24+
*/
25+
private $menu;
26+
27+
/**
28+
* @var MenuStyle
29+
*/
30+
private $style;
31+
32+
/**
33+
* @var InputIO
34+
*/
35+
private $inputIO;
36+
37+
public function setUp()
38+
{
39+
$this->terminal = $this->createMock(TerminalInterface::class);
40+
$this->menu = $this->createMock(CliMenu::class);
41+
$this->style = $this->createMock(MenuStyle::class);
42+
$this->inputIO = new InputIO($this->menu, $this->style, $this->terminal);
43+
}
44+
45+
public function testEnterReturnsOutputIfValid() : void
46+
{
47+
$this->terminal
48+
->expects($this->exactly(2))
49+
->method('getKeyedInput')
50+
->willReturn('1', 'enter');
51+
52+
$result = $this->inputIO->collect(new Text($this->inputIO));
53+
54+
self::assertEquals('1', $result->fetch());
55+
}
56+
57+
public function testCustomControlFunctions() : void
58+
{
59+
$this->inputIO->registerControlCallback('u', function ($input) {
60+
return ++$input;
61+
});
62+
63+
$this->terminal
64+
->expects($this->exactly(4))
65+
->method('getKeyedInput')
66+
->with(["\n" => 'enter', "\r" => 'enter', "\177" => 'backspace', 'u' => 'u'])
67+
->willReturn('1', '0', 'u', 'enter');
68+
69+
$result = $this->inputIO->collect(new Text($this->inputIO));
70+
71+
self::assertEquals('11', $result->fetch());
72+
}
73+
74+
public function testBackspaceDeletesPreviousCharacter() : void
75+
{
76+
$this->terminal
77+
->expects($this->exactly(6))
78+
->method('getKeyedInput')
79+
->willReturn('1', '6', '7', 'backspace', 'backspace', 'enter');
80+
81+
$result = $this->inputIO->collect(new Text($this->inputIO));
82+
83+
self::assertEquals('1', $result->fetch());
84+
}
85+
86+
public function testValidationErrorCausesErrorMessageToBeDisplayed() : void
87+
{
88+
$input = new class ($this->inputIO) extends Text {
89+
public function validate(string $input) : bool
90+
{
91+
return $input[-1] === 'p';
92+
}
93+
};
94+
95+
$this->terminal
96+
->expects($this->exactly(6))
97+
->method('getKeyedInput')
98+
->willReturn('1', 't', 'enter', 'backspace', 'p', 'enter');
99+
100+
$result = $this->inputIO->collect($input);
101+
102+
self::assertEquals('1p', $result->fetch());
103+
}
104+
}

test/Input/InputResultTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace PhpSchool\CliMenuTest\Input;
4+
5+
use PhpSchool\CliMenu\Input\InputResult;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* @author Aydin Hassan <[email protected]>
10+
*/
11+
class InputResultTest extends TestCase
12+
{
13+
public function testFetch() : void
14+
{
15+
static::assertEquals('my-password', (new InputResult('my-password'))->fetch());
16+
}
17+
}

test/Input/NumberTest.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace PhpSchool\CliMenuTest\Input;
4+
5+
use PhpSchool\CliMenu\CliMenu;
6+
use PhpSchool\CliMenu\Input\InputIO;
7+
use PhpSchool\CliMenu\Input\Number;
8+
use PhpSchool\CliMenu\MenuStyle;
9+
use PhpSchool\CliMenu\Terminal\TerminalInterface;
10+
use PHPUnit\Framework\TestCase;
11+
12+
/**
13+
* @author Aydin Hassan <[email protected]>
14+
*/
15+
class NumberTest extends TestCase
16+
{
17+
/**
18+
* @var TerminalInterface
19+
*/
20+
private $terminal;
21+
22+
/**
23+
* @var InputIO
24+
*/
25+
private $inputIO;
26+
27+
/**
28+
* @var Number
29+
*/
30+
private $input;
31+
32+
public function setUp()
33+
{
34+
$this->terminal = $this->createMock(TerminalInterface::class);
35+
$menu = $this->createMock(CliMenu::class);
36+
$style = $this->createMock(MenuStyle::class);
37+
38+
$this->inputIO = new InputIO($menu, $style, $this->terminal);
39+
$this->input = new Number($this->inputIO);
40+
}
41+
42+
public function testGetSetPromptText() : void
43+
{
44+
static::assertEquals('Enter a number:', $this->input->getPromptText());
45+
46+
$this->input->setPromptText('Number please:');
47+
static::assertEquals('Number please:', $this->input->getPromptText());
48+
}
49+
50+
public function testGetSetValidationFailedText() : void
51+
{
52+
static::assertEquals('Not a valid number, try again', $this->input->getValidationFailedText());
53+
54+
$this->input->setValidationFailedText('Failed!');
55+
static::assertEquals('Failed!', $this->input->getValidationFailedText());
56+
}
57+
58+
public function testGetSetPlaceholderText() : void
59+
{
60+
static::assertEquals('', $this->input->getPlaceholderText());
61+
62+
$this->input->setPlaceholderText('some placeholder text');
63+
static::assertEquals('some placeholder text', $this->input->getPlaceholderText());
64+
}
65+
66+
/**
67+
* @dataProvider validateProvider
68+
*/
69+
public function testValidate(string $value, bool $result) : void
70+
{
71+
static::assertEquals($this->input->validate($value), $result);
72+
}
73+
74+
public function validateProvider() : array
75+
{
76+
return [
77+
['10', true],
78+
['10t', false],
79+
['t10', false],
80+
['0', true],
81+
['0000000000', true],
82+
['9999999999', true],
83+
];
84+
}
85+
86+
public function testFilterReturnsInputAsIs() : void
87+
{
88+
static::assertEquals('9999', $this->input->filter('9999'));
89+
}
90+
91+
public function testUpKeyIncrementsNumber() : void
92+
{
93+
$this->terminal
94+
->expects($this->exactly(4))
95+
->method('getKeyedInput')
96+
->willReturn('1', '0', "\033[A", 'enter');
97+
98+
self::assertEquals(11, $this->input->ask()->fetch());
99+
}
100+
101+
public function testDownKeyDecrementsNumber() : void
102+
{
103+
$this->terminal
104+
->expects($this->exactly(4))
105+
->method('getKeyedInput')
106+
->willReturn('1', '0', "\033[B", 'enter');
107+
108+
self::assertEquals(9, $this->input->ask()->fetch());
109+
}
110+
}

0 commit comments

Comments
 (0)