From e33c5f94d2dc42028e3c60cc7f092dd56a6263fc Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 3 Aug 2022 11:55:33 +0900 Subject: [PATCH] feat: make CLI::input() testable --- system/CLI/CLI.php | 9 ++++----- tests/system/CLI/CLITest.php | 28 +++++++++++++--------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/system/CLI/CLI.php b/system/CLI/CLI.php index 7a5030314948..dabd9c1091b1 100644 --- a/system/CLI/CLI.php +++ b/system/CLI/CLI.php @@ -175,18 +175,17 @@ public static function init() * php index.php user -v --v -name=John --name=John * * @param string $prefix - * - * @codeCoverageIgnore */ public static function input(?string $prefix = null): string { - if (static::$readline_support) { - return readline($prefix); + // readline() can't be tested. + if (static::$readline_support && ENVIRONMENT !== 'testing') { + return readline($prefix); // @codeCoverageIgnore } echo $prefix; - return fgets(STDIN); + return fgets(fopen('php://stdin', 'rb')); } /** diff --git a/tests/system/CLI/CLITest.php b/tests/system/CLI/CLITest.php index da4524ae47a8..d90e34433f1f 100644 --- a/tests/system/CLI/CLITest.php +++ b/tests/system/CLI/CLITest.php @@ -12,6 +12,7 @@ namespace CodeIgniter\CLI; use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\PhpStreamWrapper; use CodeIgniter\Test\StreamFilterTrait; use ReflectionProperty; use RuntimeException; @@ -59,22 +60,19 @@ public function testWait() $time = time(); CLI::wait(1); $this->assertCloseEnough(1, time() - $time); + } + + public function testWaitZero() + { + PhpStreamWrapper::register(); + PhpStreamWrapper::setContent(' '); + + // test the press any key to continue... + $time = time(); + CLI::wait(0); + $this->assertSame(0, time() - $time); - // Leaving the code fragment below in, to remind myself (or others) - // of what appears to be the most likely path to test this last - // bit of wait() functionality. - // The problem: if the block below is enabled, the phpunit tests - // go catatonic when it is executed, presumably because of - // the CLI::input() waiting for a key press - // - // // test the press any key to continue... - // stream_filter_register('CLITestKeyboardFilter', 'CodeIgniter\CLI\CLITestKeyboardFilter'); - // $spoofer = stream_filter_append(STDIN, 'CLITestKeyboardFilter'); - // $time = time(); - // CLITestKeyboardFilter::$spoofed = ' '; - // CLI::wait(0); - // stream_filter_remove($spoofer); - // $this->assertEquals(0, time() - $time); + PhpStreamWrapper::restore(); } public function testIsWindows()