1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\TerminalTest; |
4
|
|
|
|
5
|
|
|
use PhpSchool\Terminal\InputCharacter; |
6
|
|
|
use PhpSchool\Terminal\Terminal; |
7
|
|
|
use PhpSchool\Terminal\NonCanonicalReader; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Aydin Hassan <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class NonCanonicalReaderTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testExceptionIsThrownIfMappingAddedForNonControlCharacter() : void |
16
|
|
|
{ |
17
|
|
|
self::expectException(\InvalidArgumentException::class); |
|
|
|
|
18
|
|
|
self::expectExceptionMessage('Control "w" does not exist'); |
|
|
|
|
19
|
|
|
|
20
|
|
|
$terminal = $this->createMock(Terminal::class); |
21
|
|
|
$terminalReader = new NonCanonicalReader($terminal); |
22
|
|
|
$terminalReader->addControlMapping('p', 'w'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testExceptionIsThrownIfMappingsAddedForNonControlCharacter() : void |
26
|
|
|
{ |
27
|
|
|
self::expectException(\InvalidArgumentException::class); |
|
|
|
|
28
|
|
|
self::expectExceptionMessage('Control "w" does not exist'); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$terminal = $this->createMock(Terminal::class); |
31
|
|
|
$terminalReader = new NonCanonicalReader($terminal); |
32
|
|
|
$terminalReader->addControlMappings(['p' => 'w']); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testCustomMappingToUpControl() : void |
36
|
|
|
{ |
37
|
|
|
$terminal = $this->createMock(Terminal::class); |
38
|
|
|
$terminal |
39
|
|
|
->expects($this->once()) |
40
|
|
|
->method('read') |
41
|
|
|
->with(4) |
42
|
|
|
->willReturn('w'); |
43
|
|
|
|
44
|
|
|
$terminalReader = new NonCanonicalReader($terminal); |
45
|
|
|
$terminalReader->addControlMapping('w', InputCharacter::UP); |
46
|
|
|
|
47
|
|
|
$char = $terminalReader->readCharacter(); |
48
|
|
|
|
49
|
|
|
self::assertTrue($char->isControl()); |
50
|
|
|
self::assertEquals('UP', $char->getControl()); |
51
|
|
|
self::assertEquals("\033[A", $char->get()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testReadNormalCharacter() : void |
55
|
|
|
{ |
56
|
|
|
$terminal = $this->createMock(Terminal::class); |
57
|
|
|
$terminal |
58
|
|
|
->expects($this->once()) |
59
|
|
|
->method('read') |
60
|
|
|
->with(4) |
61
|
|
|
->willReturn('w'); |
62
|
|
|
|
63
|
|
|
$terminalReader = new NonCanonicalReader($terminal); |
64
|
|
|
|
65
|
|
|
$char = $terminalReader->readCharacter(); |
66
|
|
|
|
67
|
|
|
self::assertFalse($char->isControl()); |
68
|
|
|
self::assertEquals('w', $char->get()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testReadControlCharacter() |
72
|
|
|
{ |
73
|
|
|
$terminal = $this->createMock(Terminal::class); |
74
|
|
|
$terminal |
75
|
|
|
->expects($this->once()) |
76
|
|
|
->method('read') |
77
|
|
|
->with(4) |
78
|
|
|
->willReturn("\n"); |
79
|
|
|
|
80
|
|
|
$terminalReader = new NonCanonicalReader($terminal); |
81
|
|
|
|
82
|
|
|
$char = $terminalReader->readCharacter(); |
83
|
|
|
|
84
|
|
|
self::assertTrue($char->isControl()); |
85
|
|
|
self::assertEquals('ENTER', $char->getControl()); |
86
|
|
|
self::assertEquals("\n", $char->get()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|