Passed
Pull Request — master (#1)
by Aydin
02:24
created

UnixTerminalTest::testMoveCursorToRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace PhpSchool\TerminalTest;
4
5
use PhpSchool\Terminal\Exception\NotInteractiveTerminal;
6
use PhpSchool\Terminal\IO\BufferedOutput;
7
use PhpSchool\Terminal\IO\InputStream;
8
use PhpSchool\Terminal\IO\OutputStream;
9
use PhpSchool\Terminal\IO\ResourceInputStream;
10
use PhpSchool\Terminal\IO\ResourceOutputStream;
11
use PhpSchool\Terminal\UnixTerminal;
12
use PHPUnit\Framework\TestCase;
13
14
class UnixTerminalTest extends TestCase
15
{
16
    public function testIsInteractiveReturnsTrueIfInputAndOutputAreTTYs() : void
17
    {
18
        $input  = $this->createMock(InputStream::class);
19
        $output = $this->createMock(OutputStream::class);
20
21
        $input
22
            ->expects($this->once())
23
            ->method('isInteractive')
24
            ->willReturn(true);
25
        $output
26
            ->expects($this->once())
27
            ->method('isInteractive')
28
            ->willReturn(true);
29
30
        $terminal = new UnixTerminal($input, $output);
31
32
        self::assertTrue($terminal->isInteractive());
33
    }
34
35
    public function testIsInteractiveReturnsFalseIfInputNotTTY() : void
36
    {
37
        $input  = $this->createMock(InputStream::class);
38
        $output = $this->createMock(OutputStream::class);
39
40
        $input
41
            ->expects($this->once())
42
            ->method('isInteractive')
43
            ->willReturn(false);
44
        $output
45
            ->expects($this->any())
46
            ->method('isInteractive')
47
            ->willReturn(true);
48
49
        $terminal = new UnixTerminal($input, $output);
50
51
        self::assertFalse($terminal->isInteractive());
52
    }
53
54
    public function testIsInteractiveReturnsFalseIfOutputNotTTY() : void
55
    {
56
        $input  = $this->createMock(InputStream::class);
57
        $output = $this->createMock(OutputStream::class);
58
59
        $input
60
            ->expects($this->once())
61
            ->method('isInteractive')
62
            ->willReturn(true);
63
        $output
64
            ->expects($this->once())
65
            ->method('isInteractive')
66
            ->willReturn(false);
67
68
        $terminal = new UnixTerminal($input, $output);
69
70
        self::assertFalse($terminal->isInteractive());
71
    }
72
73
    public function testIsInteractiveReturnsFalseIfInputAndOutputNotTTYs() : void
74
    {
75
        $input  = $this->createMock(InputStream::class);
76
        $output = $this->createMock(OutputStream::class);
77
78
        $input
79
            ->expects($this->once())
80
            ->method('isInteractive')
81
            ->willReturn(false);
82
        $output
83
            ->expects($this->any())
84
            ->method('isInteractive')
85
            ->willReturn(false);
86
87
        $terminal = new UnixTerminal($input, $output);
88
89
        self::assertFalse($terminal->isInteractive());
90
    }
91
92
    public function testMustBeInteractiveThrowsExceptionIfInputNotTTY() : void
93
    {
94
        self::expectException(NotInteractiveTerminal::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

94
        self::/** @scrutinizer ignore-call */ 
95
              expectException(NotInteractiveTerminal::class);
Loading history...
95
        self::expectExceptionMessage('Input stream is not interactive (non TTY)');
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

95
        self::/** @scrutinizer ignore-call */ 
96
              expectExceptionMessage('Input stream is not interactive (non TTY)');
Loading history...
96
97
        $input  = $this->createMock(InputStream::class);
98
        $output = $this->createMock(OutputStream::class);
99
100
        $input
101
            ->expects($this->once())
102
            ->method('isInteractive')
103
            ->willReturn(false);
104
105
        $terminal = new UnixTerminal($input, $output);
106
        $terminal->mustBeInteractive();
107
    }
108
109
    public function testMustBeInteractiveThrowsExceptionIfOutputNotTTY() : void
110
    {
111
        self::expectException(NotInteractiveTerminal::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

111
        self::/** @scrutinizer ignore-call */ 
112
              expectException(NotInteractiveTerminal::class);
Loading history...
112
        self::expectExceptionMessage('Output stream is not interactive (non TTY)');
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

112
        self::/** @scrutinizer ignore-call */ 
113
              expectExceptionMessage('Output stream is not interactive (non TTY)');
Loading history...
113
114
        $input  = $this->createMock(InputStream::class);
115
        $output = $this->createMock(OutputStream::class);
116
117
        $input
118
            ->expects($this->once())
119
            ->method('isInteractive')
120
            ->willReturn(true);
121
122
        $output
123
            ->expects($this->once())
124
            ->method('isInteractive')
125
            ->willReturn(false);
126
127
        $terminal = new UnixTerminal($input, $output);
128
        $terminal->mustBeInteractive();
129
    }
130
131
    public function testClear() : void
132
    {
133
        $input  = $this->createMock(InputStream::class);
134
        $output = new BufferedOutput;
135
136
        $terminal = new UnixTerminal($input, $output);
137
        $terminal->clear();
138
139
        self::assertEquals("\033[2J", $output->fetch());
140
    }
141
142
    public function testClearLine() : void
143
    {
144
        $input  = $this->createMock(InputStream::class);
145
        $output = new BufferedOutput;
146
147
        $terminal = new UnixTerminal($input, $output);
148
        $rf = new \ReflectionObject($terminal);
149
        $rp = $rf->getProperty('width');
150
        $rp->setAccessible(true);
151
        $rp->setValue($terminal, 23);
152
153
        $terminal->clearLine();
154
155
        self::assertEquals("\033[23D\033[K", $output->fetch());
156
    }
157
158
    public function testClean() : void
159
    {
160
        $input  = $this->createMock(InputStream::class);
161
        $output = new BufferedOutput;
162
163
        $terminal = new UnixTerminal($input, $output);
164
        $rf = new \ReflectionObject($terminal);
165
        $rp = $rf->getProperty('width');
166
        $rp->setAccessible(true);
167
        $rp->setValue($terminal, 23);
168
        $rp = $rf->getProperty('height');
169
        $rp->setAccessible(true);
170
        $rp->setValue($terminal, 2);
171
172
        $terminal->clean();
173
174
        self::assertEquals("\033[0;0H\033[23D\033[K\033[1;0H\033[23D\033[K\033[2;0H\033[23D\033[K", $output->fetch());
175
    }
176
177
    public function testEnableCursor() : void
178
    {
179
        $input  = $this->createMock(InputStream::class);
180
        $output = new BufferedOutput;
181
182
        $terminal = new UnixTerminal($input, $output);
183
        $terminal->enableCursor();
184
185
        self::assertEquals("\033[?25h", $output->fetch());
186
    }
187
188
    public function testDisableCursor() : void
189
    {
190
        $input  = $this->createMock(InputStream::class);
191
        $output = new BufferedOutput;
192
193
        $terminal = new UnixTerminal($input, $output);
194
        $terminal->disableCursor();
195
196
        self::assertEquals("\033[?25l", $output->fetch());
197
    }
198
199
    public function testMoveCursorToTop() : void
200
    {
201
        $input  = $this->createMock(InputStream::class);
202
        $output = new BufferedOutput;
203
204
        $terminal = new UnixTerminal($input, $output);
205
        $terminal->moveCursorToTop();
206
207
        self::assertEquals("\033[H", $output->fetch());
208
    }
209
210
    public function testMoveCursorToRow() : void
211
    {
212
        $input  = $this->createMock(InputStream::class);
213
        $output = new BufferedOutput;
214
215
        $terminal = new UnixTerminal($input, $output);
216
        $terminal->moveCursorToRow(2);
217
218
        self::assertEquals("\033[2;0H", $output->fetch());
219
    }
220
221
    public function testMoveCursorToColumn() : void
222
    {
223
        $input  = $this->createMock(InputStream::class);
224
        $output = new BufferedOutput;
225
226
        $terminal = new UnixTerminal($input, $output);
227
        $terminal->moveCursorToColumn(10);
228
229
        self::assertEquals("\033[10C", $output->fetch());
230
    }
231
232
    public function testRead() : void
233
    {
234
        $tempStream = fopen('php://temp', 'r+');
235
        fwrite($tempStream, 'mystring');
0 ignored issues
show
Bug introduced by
It seems like $tempStream can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

235
        fwrite(/** @scrutinizer ignore-type */ $tempStream, 'mystring');
Loading history...
236
        rewind($tempStream);
0 ignored issues
show
Bug introduced by
It seems like $tempStream can also be of type false; however, parameter $handle of rewind() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

236
        rewind(/** @scrutinizer ignore-type */ $tempStream);
Loading history...
237
238
        $input  = new ResourceInputStream($tempStream);
239
        $output = $this->createMock(OutputStream::class);
240
241
        $terminal = new UnixTerminal($input, $output);
242
243
        self::assertEquals('myst', $terminal->read(4));
244
        self::assertEquals('ring', $terminal->read(4));
245
246
        fclose($tempStream);
0 ignored issues
show
Bug introduced by
It seems like $tempStream can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

246
        fclose(/** @scrutinizer ignore-type */ $tempStream);
Loading history...
247
    }
248
249
    public function testWriteForwardsToOutput() : void
250
    {
251
        $input  = $this->createMock(InputStream::class);
252
        $output = new BufferedOutput;
253
254
        $terminal = new UnixTerminal($input, $output);
255
        $terminal->write('My awesome string');
256
257
        self::assertEquals('My awesome string', $output->fetch());
258
    }
259
}
260