|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\TerminalTest; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSchool\Terminal\InputCharacter; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @author Aydin Hassan <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class InputCharacterTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
public function testWhenCharacterIsAControl() : void |
|
14
|
|
|
{ |
|
15
|
|
|
$char = new InputCharacter("\n"); |
|
16
|
|
|
|
|
17
|
|
|
self::assertTrue($char->isControl()); |
|
18
|
|
|
self::assertTrue($char->isHandledControl()); |
|
19
|
|
|
self::assertFalse($char->isNotControl()); |
|
20
|
|
|
self::assertEquals('ENTER', $char->getControl()); |
|
21
|
|
|
self::assertEquals("\n", $char->get()); |
|
22
|
|
|
self::assertEquals("\n", $char->__toString()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testWhenCharacterIsNotAControl() : void |
|
26
|
|
|
{ |
|
27
|
|
|
$char = new InputCharacter('p'); |
|
28
|
|
|
|
|
29
|
|
|
self::assertFalse($char->isControl()); |
|
30
|
|
|
self::assertFalse($char->isHandledControl()); |
|
31
|
|
|
self::assertTrue($char->isNotControl()); |
|
32
|
|
|
self::assertEquals('p', $char->get()); |
|
33
|
|
|
self::assertEquals('p', $char->__toString()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testExceptionIsThrownIfGetControlCalledWhenNotAControl() : void |
|
37
|
|
|
{ |
|
38
|
|
|
self::expectException(\RuntimeException::class); |
|
|
|
|
|
|
39
|
|
|
self::expectExceptionMessage('Character "p" is not a control'); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$char = new InputCharacter('p'); |
|
42
|
|
|
$char->getControl(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testGetControls() : void |
|
46
|
|
|
{ |
|
47
|
|
|
self::assertEquals( |
|
48
|
|
|
[ |
|
49
|
|
|
'UP', |
|
50
|
|
|
'DOWN', |
|
51
|
|
|
'RIGHT', |
|
52
|
|
|
'LEFT', |
|
53
|
|
|
'CTRLA', |
|
54
|
|
|
'CTRLB', |
|
55
|
|
|
'CTRLE', |
|
56
|
|
|
'CTRLF', |
|
57
|
|
|
'BACKSPACE', |
|
58
|
|
|
'CTRLW', |
|
59
|
|
|
'ENTER', |
|
60
|
|
|
'TAB', |
|
61
|
|
|
'ESC', |
|
62
|
|
|
], |
|
63
|
|
|
InputCharacter::getControls() |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testFromControlNameThrowsExceptionIfControlDoesNotExist() : void |
|
68
|
|
|
{ |
|
69
|
|
|
self::expectException(\InvalidArgumentException::class); |
|
|
|
|
|
|
70
|
|
|
self::expectExceptionMessage('Control "w" does not exist'); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
InputCharacter::fromControlName('w'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testFromControlName() : void |
|
76
|
|
|
{ |
|
77
|
|
|
$char = InputCharacter::fromControlName(InputCharacter::UP); |
|
78
|
|
|
|
|
79
|
|
|
self::assertTrue($char->isControl()); |
|
80
|
|
|
self::assertEquals('UP', $char->getControl()); |
|
81
|
|
|
self::assertEquals("\033[A", $char->get()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testControlExists() : void |
|
85
|
|
|
{ |
|
86
|
|
|
self::assertTrue(InputCharacter::controlExists(InputCharacter::UP)); |
|
87
|
|
|
self::assertFalse(InputCharacter::controlExists('w')); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testIsControlOnNotExplicitlyHandledControls() : void |
|
91
|
|
|
{ |
|
92
|
|
|
$char = new InputCharacter("\016"); //ctrl + p (I think) |
|
93
|
|
|
|
|
94
|
|
|
self::assertTrue($char->isControl()); |
|
95
|
|
|
self::assertFalse($char->isHandledControl()); |
|
96
|
|
|
|
|
97
|
|
|
$char = new InputCharacter("\021"); //ctrl + u (I think) |
|
98
|
|
|
|
|
99
|
|
|
self::assertTrue($char->isControl()); |
|
100
|
|
|
self::assertFalse($char->isHandledControl()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testUnicodeCharacter() : void |
|
104
|
|
|
{ |
|
105
|
|
|
$char = new InputCharacter('ß'); |
|
106
|
|
|
|
|
107
|
|
|
self::assertFalse($char->isControl()); |
|
108
|
|
|
self::assertFalse($char->isHandledControl()); |
|
109
|
|
|
self::assertTrue($char->isNotControl()); |
|
110
|
|
|
self::assertEquals('ß', $char->get()); |
|
111
|
|
|
self::assertEquals('ß', $char->__toString()); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|