Skip to content

Commit

Permalink
feat: make CLI::input() testable
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 3, 2022
1 parent 6536f5c commit e33c5f9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
9 changes: 4 additions & 5 deletions system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand Down
28 changes: 13 additions & 15 deletions tests/system/CLI/CLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\CLI;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\PhpStreamWrapper;
use CodeIgniter\Test\StreamFilterTrait;
use ReflectionProperty;
use RuntimeException;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit e33c5f9

Please sign in to comment.