2021-01-25 12:35:32 -08:00
|
|
|
<?php
|
|
|
|
|
2022-05-17 10:29:20 -07:00
|
|
|
namespace Olive\Tests\Console\Ungetch;
|
2021-01-25 12:35:32 -08:00
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-05-17 10:29:20 -07:00
|
|
|
use Olive\Console\Getch;
|
2021-01-25 12:35:32 -08:00
|
|
|
|
|
|
|
class UngetchTest extends TestCase
|
|
|
|
{
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUngetch()
|
|
|
|
{
|
|
|
|
$g = new Getch();
|
|
|
|
$res = $g->ungetch('A');
|
|
|
|
self::assertSame(65, $res);
|
|
|
|
$res = $g->getch();
|
|
|
|
self::assertSame(65, $res);
|
|
|
|
$res = ungetch(65);
|
|
|
|
self::assertSame(65, $res);
|
|
|
|
$res = $g->getch();
|
|
|
|
self::assertSame(65, $res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTypeError()
|
|
|
|
{
|
|
|
|
$g = new Getch();
|
|
|
|
$this->expectException(\TypeError::class);
|
|
|
|
$g->ungetch(new \stdClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testForFun()
|
|
|
|
{
|
2021-03-01 11:10:51 -08:00
|
|
|
foreach (\str_split(\strrev('Hello World!')) as $char) {
|
2021-01-25 12:35:32 -08:00
|
|
|
ungetch($char);
|
|
|
|
}
|
|
|
|
$result = '';
|
|
|
|
do {
|
|
|
|
$ord = getch();
|
|
|
|
$result .= \chr($ord);
|
2021-03-01 11:10:51 -08:00
|
|
|
} while ($ord !== ord('!'));
|
2021-01-25 12:35:32 -08:00
|
|
|
self::assertSame('Hello World!', $result);
|
|
|
|
}
|
|
|
|
}
|