Completed
Pull Request — master (#7)
by Aydin
02:34
created

UnixTerminalTest::testClearEntireLine()   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 testClearEntireLine() : void
159
    {
160
        $input  = $this->createMock(InputStream::class);
161
        $output = new BufferedOutput;
162
163
        $terminal = new UnixTerminal($input, $output);
164
        $terminal->clearEntireLine();
165
166
        self::assertEquals("\033[2K", $output->fetch());
167
    }
168
169
    public function testClearDown() : void
170
    {
171
        $input  = $this->createMock(InputStream::class);
172
        $output = new BufferedOutput;
173
174
        $terminal = new UnixTerminal($input, $output);
175
        $terminal->clearDown();
176
177
        self::assertEquals("\033[J", $output->fetch());
178
    }
179
180
    public function testClean() : void
181
    {
182
        $input  = $this->createMock(InputStream::class);
183
        $output = new BufferedOutput;
184
185
        $terminal = new UnixTerminal($input, $output);
186
        $rf = new \ReflectionObject($terminal);
187
        $rp = $rf->getProperty('width');
188
        $rp->setAccessible(true);
189
        $rp->setValue($terminal, 23);
190
        $rp = $rf->getProperty('height');
191
        $rp->setAccessible(true);
192
        $rp->setValue($terminal, 2);
193
194
        $terminal->clean();
195
196
        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());
197
    }
198
199
    public function testEnableCursor() : void
200
    {
201
        $input  = $this->createMock(InputStream::class);
202
        $output = new BufferedOutput;
203
204
        $terminal = new UnixTerminal($input, $output);
205
        $terminal->enableCursor();
206
207
        self::assertEquals("\033[?25h", $output->fetch());
208
    }
209
210
    public function testDisableCursor() : void
211
    {
212
        $input  = $this->createMock(InputStream::class);
213
        $output = new BufferedOutput;
214
215
        $terminal = new UnixTerminal($input, $output);
216
        $terminal->disableCursor();
217
218
        self::assertEquals("\033[?25l", $output->fetch());
219
    }
220
221
    public function testMoveCursorToTop() : void
222
    {
223
        $input  = $this->createMock(InputStream::class);
224
        $output = new BufferedOutput;
225
226
        $terminal = new UnixTerminal($input, $output);
227
        $terminal->moveCursorToTop();
228
229
        self::assertEquals("\033[H", $output->fetch());
230
    }
231
232
    public function testMoveCursorToRow() : void
233
    {
234
        $input  = $this->createMock(InputStream::class);
235
        $output = new BufferedOutput;
236
237
        $terminal = new UnixTerminal($input, $output);
238
        $terminal->moveCursorToRow(2);
239
240
        self::assertEquals("\033[2;0H", $output->fetch());
241
    }
242
243
    public function testMoveCursorToColumn() : void
244
    {
245
        $input  = $this->createMock(InputStream::class);
246
        $output = new BufferedOutput;
247
248
        $terminal = new UnixTerminal($input, $output);
249
        $terminal->moveCursorToColumn(10);
250
251
        self::assertEquals("\033[10C", $output->fetch());
252
    }
253
254
    public function testShowAlternateScreen() : void
255
    {
256
        $input  = $this->createMock(InputStream::class);
257
        $output = new BufferedOutput;
258
259
        $terminal = new UnixTerminal($input, $output);
260
        $terminal->showSecondaryScreen();
261
262
        self::assertEquals("\033[?47h", $output->fetch());
263
    }
264
265
    public function testShowMainScreen() : void
266
    {
267
        $input  = $this->createMock(InputStream::class);
268
        $output = new BufferedOutput;
269
270
        $terminal = new UnixTerminal($input, $output);
271
        $terminal->showPrimaryScreen();
272
273
        self::assertEquals("\033[?47l", $output->fetch());
274
    }
275
276
    public function testRead() : void
277
    {
278
        $tempStream = fopen('php://temp', 'r+');
279
        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

279
        fwrite(/** @scrutinizer ignore-type */ $tempStream, 'mystring');
Loading history...
280
        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

280
        rewind(/** @scrutinizer ignore-type */ $tempStream);
Loading history...
281
282
        $input  = new ResourceInputStream($tempStream);
283
        $output = $this->createMock(OutputStream::class);
284
285
        $terminal = new UnixTerminal($input, $output);
286
287
        self::assertEquals('myst', $terminal->read(4));
288
        self::assertEquals('ring', $terminal->read(4));
289
290
        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

290
        fclose(/** @scrutinizer ignore-type */ $tempStream);
Loading history...
291
    }
292
293
    public function testWriteForwardsToOutput() : void
294
    {
295
        $input  = $this->createMock(InputStream::class);
296
        $output = new BufferedOutput;
297
298
        $terminal = new UnixTerminal($input, $output);
299
        $terminal->write('My awesome string');
300
301
        self::assertEquals('My awesome string', $output->fetch());
302
    }
303
}
304