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
|
|
|
], |
62
|
|
|
InputCharacter::getControls() |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testFromControlNameThrowsExceptionIfControlDoesNotExist() : void |
67
|
|
|
{ |
68
|
|
|
self::expectException(\InvalidArgumentException::class); |
|
|
|
|
69
|
|
|
self::expectExceptionMessage('Control "w" does not exist'); |
|
|
|
|
70
|
|
|
|
71
|
|
|
InputCharacter::fromControlName('w'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testFromControlName() : void |
75
|
|
|
{ |
76
|
|
|
$char = InputCharacter::fromControlName(InputCharacter::UP); |
77
|
|
|
|
78
|
|
|
self::assertTrue($char->isControl()); |
79
|
|
|
self::assertEquals('UP', $char->getControl()); |
80
|
|
|
self::assertEquals("\033[A", $char->get()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testControlExists() : void |
84
|
|
|
{ |
85
|
|
|
self::assertTrue(InputCharacter::controlExists(InputCharacter::UP)); |
86
|
|
|
self::assertFalse(InputCharacter::controlExists('w')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testIsControlOnNotExplicitlyHandledControls() : void |
90
|
|
|
{ |
91
|
|
|
$char = new InputCharacter("\016"); //ctrl + p (I think) |
92
|
|
|
|
93
|
|
|
self::assertTrue($char->isControl()); |
94
|
|
|
self::assertFalse($char->isHandledControl()); |
95
|
|
|
|
96
|
|
|
$char = new InputCharacter("\021"); //ctrl + u (I think) |
97
|
|
|
|
98
|
|
|
self::assertTrue($char->isControl()); |
99
|
|
|
self::assertFalse($char->isHandledControl()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testUnicodeCharacter() : void |
103
|
|
|
{ |
104
|
|
|
$char = new InputCharacter('ß'); |
105
|
|
|
|
106
|
|
|
self::assertFalse($char->isControl()); |
107
|
|
|
self::assertFalse($char->isHandledControl()); |
108
|
|
|
self::assertTrue($char->isNotControl()); |
109
|
|
|
self::assertEquals('ß', $char->get()); |
110
|
|
|
self::assertEquals('ß', $char->__toString()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|