testCustomMappingToUpControl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 17
rs 9.8666
cc 1
nc 1
nop 0
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);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
        self::/** @scrutinizer ignore-call */ 
18
              expectException(\InvalidArgumentException::class);
Loading history...
18
        self::expectExceptionMessage('Control "w" does not exist');
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCa...xpectExceptionMessage() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
        self::/** @scrutinizer ignore-call */ 
19
              expectExceptionMessage('Control "w" does not exist');
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        self::/** @scrutinizer ignore-call */ 
28
              expectException(\InvalidArgumentException::class);
Loading history...
28
        self::expectExceptionMessage('Control "w" does not exist');
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCa...xpectExceptionMessage() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        self::/** @scrutinizer ignore-call */ 
29
              expectExceptionMessage('Control "w" does not exist');
Loading history...
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