2021-01-25 12:35:32 -08:00
|
|
|
<?php
|
|
|
|
|
2024-07-18 12:44:34 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020-2024 https://sikofitt.com sikofitt@gmail.com
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the
|
|
|
|
* terms of the Mozilla Public License, v. 2.0.
|
|
|
|
*
|
|
|
|
* If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2021-01-25 12:35:32 -08:00
|
|
|
namespace Sikofitt\Tests\Console\Getch;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Sikofitt\Console\Getch;
|
|
|
|
|
|
|
|
class GetchTest extends TestCase
|
|
|
|
{
|
|
|
|
private \FFI $ffi;
|
2021-02-15 11:44:39 -08:00
|
|
|
private const HOME_KEY = "\x1b[H";
|
2021-01-25 12:35:32 -08:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->ffi = \FFI::load(__DIR__.'/../test.h');
|
2021-02-15 11:44:39 -08:00
|
|
|
$file = $this->ffi->stdin;
|
|
|
|
$stdin = $this->ffi->fopen('/dev/stdin', 'a+');
|
2021-01-25 12:35:32 -08:00
|
|
|
|
|
|
|
foreach (range('D', 'A') as $character) {
|
2021-02-15 11:44:39 -08:00
|
|
|
$this->ffi->ungetc(ord($character), $file);
|
2021-01-25 12:35:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @preserveGlobalState disabled
|
2024-07-18 12:34:40 -07:00
|
|
|
*
|
2021-02-15 11:44:39 -08:00
|
|
|
* @backupStaticAttributes false
|
2024-07-18 12:34:40 -07:00
|
|
|
*
|
2021-02-15 11:44:39 -08:00
|
|
|
* @backupGlobals false
|
2021-01-25 12:35:32 -08:00
|
|
|
*/
|
|
|
|
public function testFailureOnInvalidLibrary()
|
|
|
|
{
|
2021-02-15 11:44:39 -08:00
|
|
|
Getch::resetFFI();
|
2021-01-25 12:35:32 -08:00
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
\getch(__DIR__.'/library.so');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetchClass()
|
|
|
|
{
|
|
|
|
$getch = new Getch();
|
|
|
|
foreach (range('A', 'D') as $character) {
|
|
|
|
self::assertSame(\ord($character), $getch->getch());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetchFunction()
|
|
|
|
{
|
|
|
|
foreach (range('A', 'D') as $character) {
|
|
|
|
self::assertSame(\ord($character), getch());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnsupportedOS()
|
|
|
|
{
|
|
|
|
if (PHP_OS_FAMILY !== 'Linux' || PHP_OS_FAMILY !== 'Windows') {
|
|
|
|
self::markTestSkipped('This test only applies to non Linux or Windows systems.');
|
|
|
|
}
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
\getch();
|
|
|
|
}
|
2021-02-15 11:44:39 -08:00
|
|
|
|
|
|
|
public function testHomeKey()
|
|
|
|
{
|
2021-02-22 17:35:17 -08:00
|
|
|
self::markTestSkipped('This seems impossible to test, since it relies on someone actually pressing keys on the keyboard.');
|
|
|
|
|
2021-02-15 11:44:39 -08:00
|
|
|
$stdin = $this->ffi->stdin;
|
|
|
|
|
|
|
|
foreach (str_split(strrev(self::HOME_KEY)) as $character) {
|
|
|
|
$this->ffi->ungetc(ord($character), $stdin);
|
|
|
|
}
|
|
|
|
$g = new Getch();
|
|
|
|
self::assertEquals(0, $g->getch());
|
2021-02-22 17:35:17 -08:00
|
|
|
self::assertEquals(71, $g->getch());
|
2021-02-15 11:44:39 -08:00
|
|
|
}
|
2021-02-27 19:10:44 -08:00
|
|
|
|
|
|
|
public function setPeek()
|
|
|
|
{
|
|
|
|
$g = new Getch();
|
|
|
|
$g->ungetch('q');
|
|
|
|
$peek = $g->peek();
|
|
|
|
$getch = $g->getch();
|
|
|
|
self::assertEquals($peek, $getch);
|
|
|
|
self::assertEquals(113, $peek);
|
|
|
|
}
|
2021-01-25 12:35:32 -08:00
|
|
|
}
|