Completed
Pull Request — master (#5)
by Aydin
02:43 queued 14s
created

InputCharacterTest::testWhenCharacterIsAControl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
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);
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

38
        self::/** @scrutinizer ignore-call */ 
39
              expectException(\RuntimeException::class);
Loading history...
39
        self::expectExceptionMessage('Character "p" is not a control');
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

39
        self::/** @scrutinizer ignore-call */ 
40
              expectExceptionMessage('Character "p" is not a control');
Loading history...
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);
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

68
        self::/** @scrutinizer ignore-call */ 
69
              expectException(\InvalidArgumentException::class);
Loading history...
69
        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

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