php-school /
terminal
| 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
Loading history...
|
|||||||
| 95 | self::expectExceptionMessage('Input stream is not interactive (non TTY)'); |
||||||
|
0 ignored issues
–
show
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
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
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
Loading history...
|
|||||||
| 112 | self::expectExceptionMessage('Output stream is not interactive (non TTY)'); |
||||||
|
0 ignored issues
–
show
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
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 | $terminal->clearLine(); |
||||||
| 149 | |||||||
| 150 | self::assertEquals("\033[2K", $output->fetch()); |
||||||
| 151 | } |
||||||
| 152 | |||||||
| 153 | public function testClearDown() : void |
||||||
| 154 | { |
||||||
| 155 | $input = $this->createMock(InputStream::class); |
||||||
| 156 | $output = new BufferedOutput; |
||||||
| 157 | |||||||
| 158 | $terminal = new UnixTerminal($input, $output); |
||||||
| 159 | $terminal->clearDown(); |
||||||
| 160 | |||||||
| 161 | self::assertEquals("\033[J", $output->fetch()); |
||||||
| 162 | } |
||||||
| 163 | |||||||
| 164 | public function testClean() : void |
||||||
| 165 | { |
||||||
| 166 | $input = $this->createMock(InputStream::class); |
||||||
| 167 | $output = new BufferedOutput; |
||||||
| 168 | |||||||
| 169 | $terminal = new UnixTerminal($input, $output); |
||||||
| 170 | $rf = new \ReflectionObject($terminal); |
||||||
| 171 | $rp = $rf->getProperty('width'); |
||||||
| 172 | $rp->setAccessible(true); |
||||||
| 173 | $rp->setValue($terminal, 23); |
||||||
| 174 | $rp = $rf->getProperty('height'); |
||||||
| 175 | $rp->setAccessible(true); |
||||||
| 176 | $rp->setValue($terminal, 2); |
||||||
| 177 | |||||||
| 178 | $terminal->clean(); |
||||||
| 179 | |||||||
| 180 | self::assertEquals("\033[0;0H\033[2K\033[1;0H\033[2K\033[2;0H\033[2K", $output->fetch()); |
||||||
| 181 | } |
||||||
| 182 | |||||||
| 183 | public function testEnableCursor() : void |
||||||
| 184 | { |
||||||
| 185 | $input = $this->createMock(InputStream::class); |
||||||
| 186 | $output = new BufferedOutput; |
||||||
| 187 | |||||||
| 188 | $terminal = new UnixTerminal($input, $output); |
||||||
| 189 | $terminal->enableCursor(); |
||||||
| 190 | |||||||
| 191 | self::assertEquals("\033[?25h", $output->fetch()); |
||||||
| 192 | } |
||||||
| 193 | |||||||
| 194 | public function testDisableCursor() : void |
||||||
| 195 | { |
||||||
| 196 | $input = $this->createMock(InputStream::class); |
||||||
| 197 | $output = new BufferedOutput; |
||||||
| 198 | |||||||
| 199 | $terminal = new UnixTerminal($input, $output); |
||||||
| 200 | $terminal->disableCursor(); |
||||||
| 201 | |||||||
| 202 | self::assertEquals("\033[?25l", $output->fetch()); |
||||||
| 203 | } |
||||||
| 204 | |||||||
| 205 | public function testMoveCursorToTop() : void |
||||||
| 206 | { |
||||||
| 207 | $input = $this->createMock(InputStream::class); |
||||||
| 208 | $output = new BufferedOutput; |
||||||
| 209 | |||||||
| 210 | $terminal = new UnixTerminal($input, $output); |
||||||
| 211 | $terminal->moveCursorToTop(); |
||||||
| 212 | |||||||
| 213 | self::assertEquals("\033[H", $output->fetch()); |
||||||
| 214 | } |
||||||
| 215 | |||||||
| 216 | public function testMoveCursorToRow() : void |
||||||
| 217 | { |
||||||
| 218 | $input = $this->createMock(InputStream::class); |
||||||
| 219 | $output = new BufferedOutput; |
||||||
| 220 | |||||||
| 221 | $terminal = new UnixTerminal($input, $output); |
||||||
| 222 | $terminal->moveCursorToRow(2); |
||||||
| 223 | |||||||
| 224 | self::assertEquals("\033[2;0H", $output->fetch()); |
||||||
| 225 | } |
||||||
| 226 | |||||||
| 227 | public function testMoveCursorToColumn() : void |
||||||
| 228 | { |
||||||
| 229 | $input = $this->createMock(InputStream::class); |
||||||
| 230 | $output = new BufferedOutput; |
||||||
| 231 | |||||||
| 232 | $terminal = new UnixTerminal($input, $output); |
||||||
| 233 | $terminal->moveCursorToColumn(10); |
||||||
| 234 | |||||||
| 235 | self::assertEquals("\033[10C", $output->fetch()); |
||||||
| 236 | } |
||||||
| 237 | |||||||
| 238 | public function testShowAlternateScreen() : void |
||||||
| 239 | { |
||||||
| 240 | $input = $this->createMock(InputStream::class); |
||||||
| 241 | $output = new BufferedOutput; |
||||||
| 242 | |||||||
| 243 | $terminal = new UnixTerminal($input, $output); |
||||||
| 244 | $terminal->showSecondaryScreen(); |
||||||
| 245 | |||||||
| 246 | self::assertEquals("\033[?47h", $output->fetch()); |
||||||
| 247 | } |
||||||
| 248 | |||||||
| 249 | public function testShowMainScreen() : void |
||||||
| 250 | { |
||||||
| 251 | $input = $this->createMock(InputStream::class); |
||||||
| 252 | $output = new BufferedOutput; |
||||||
| 253 | |||||||
| 254 | $terminal = new UnixTerminal($input, $output); |
||||||
| 255 | $terminal->showPrimaryScreen(); |
||||||
| 256 | |||||||
| 257 | self::assertEquals("\033[?47l", $output->fetch()); |
||||||
| 258 | } |
||||||
| 259 | |||||||
| 260 | public function testRead() : void |
||||||
| 261 | { |
||||||
| 262 | $tempStream = fopen('php://temp', 'r+'); |
||||||
| 263 | fwrite($tempStream, 'mystring'); |
||||||
| 264 | rewind($tempStream); |
||||||
| 265 | |||||||
| 266 | $input = new ResourceInputStream($tempStream); |
||||||
| 267 | $output = $this->createMock(OutputStream::class); |
||||||
| 268 | |||||||
| 269 | $terminal = new UnixTerminal($input, $output); |
||||||
| 270 | |||||||
| 271 | self::assertEquals('myst', $terminal->read(4)); |
||||||
| 272 | self::assertEquals('ring', $terminal->read(4)); |
||||||
| 273 | |||||||
| 274 | fclose($tempStream); |
||||||
| 275 | } |
||||||
| 276 | |||||||
| 277 | public function testWriteForwardsToOutput() : void |
||||||
| 278 | { |
||||||
| 279 | $input = $this->createMock(InputStream::class); |
||||||
| 280 | $output = new BufferedOutput; |
||||||
| 281 | |||||||
| 282 | $terminal = new UnixTerminal($input, $output); |
||||||
| 283 | $terminal->write('My awesome string'); |
||||||
| 284 | |||||||
| 285 | self::assertEquals('My awesome string', $output->fetch()); |
||||||
| 286 | } |
||||||
| 287 | |||||||
| 288 | public function testGetColourSupport() : void |
||||||
| 289 | { |
||||||
| 290 | $input = $this->createMock(InputStream::class); |
||||||
| 291 | $output = new BufferedOutput; |
||||||
| 292 | |||||||
| 293 | $terminal = new UnixTerminal($input, $output); |
||||||
| 294 | |||||||
| 295 | // Travis terminal supports 8 colours, but just in case |
||||||
| 296 | // in ever changes I'll add the 256 colors possibility too |
||||||
| 297 | self::assertTrue($terminal->getColourSupport() === 8 || $terminal->getColourSupport() === 256); |
||||||
| 298 | } |
||||||
| 299 | } |
||||||
| 300 |