Skip to content

Commit

Permalink
Merge pull request #38 from utopia-php/refactor-stdin-to-input
Browse files Browse the repository at this point in the history
Variable refactoring
  • Loading branch information
christyjacob4 authored May 8, 2024
2 parents 89f3d5d + 6785f25 commit 48610e0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/CLI/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ public static function exit(int $status = 0): void
* This function was inspired by: https://stackoverflow.com/a/13287902/2299554
*
* @param string $cmd
* @param string $stdin
* @param string $input
* @param string $output
* @param int $timeout
* @return int
*/
public static function execute(string $cmd, string $stdin, string &$output, int $timeout = -1, callable $onProgress = null): int
public static function execute(string $cmd, string $input, string &$output, int $timeout = -1, callable $onProgress = null): int
{
$cmd = '( '.$cmd.' ) 3>/dev/null ; echo $? >&3';

Expand All @@ -150,7 +150,7 @@ public static function execute(string $cmd, string $stdin, string &$output, int
\stream_set_blocking($pipes[2], false);
\stream_set_blocking($pipes[3], false);

\fwrite($pipes[0], $stdin);
\fwrite($pipes[0], $input);
\fclose($pipes[0]);
}

Expand Down
36 changes: 18 additions & 18 deletions tests/CLI/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function testLogs()
public function testExecuteBasic()
{
$output = '';
$stdin = '';
$code = Console::execute('php -r "echo \'hello world\';"', $stdin, $output, 10);
$input = '';
$code = Console::execute('php -r "echo \'hello world\';"', $input, $output, 10);

$this->assertEquals('hello world', $output);
$this->assertEquals(0, $code);
Expand All @@ -39,10 +39,10 @@ public function testExecuteBasic()
public function testExecuteStream()
{
$output = '';
$stdin = '';
$input = '';

$outputStream = '';
$code = Console::execute('printf 1 && sleep 1 && printf 2 && sleep 1 && printf 3 && sleep 1 && printf 4 && sleep 1 && printf 5', $stdin, $output, 10, function ($output) use (&$outputStream) {
$code = Console::execute('printf 1 && sleep 1 && printf 2 && sleep 1 && printf 3 && sleep 1 && printf 4 && sleep 1 && printf 5', $input, $output, 10, function ($output) use (&$outputStream) {
$outputStream .= $output;
});

Expand All @@ -54,8 +54,8 @@ public function testExecuteStream()
public function testExecuteStdOut()
{
$output = '';
$stdin = '';
$code = Console::execute('>&1 echo "success"', $stdin, $output, 3);
$input = '';
$code = Console::execute('>&1 echo "success"', $input, $output, 3);

$this->assertEquals("success\n", $output);
$this->assertEquals(0, $code);
Expand All @@ -64,8 +64,8 @@ public function testExecuteStdOut()
public function testExecuteStdErr()
{
$output = '';
$stdin = '';
$code = Console::execute('>&2 echo "error"', $stdin, $output, 3);
$input = '';
$code = Console::execute('>&2 echo "error"', $input, $output, 3);

$this->assertEquals("error\n", $output);
$this->assertEquals(0, $code);
Expand All @@ -74,15 +74,15 @@ public function testExecuteStdErr()
public function testExecuteExitCode()
{
$output = '';
$stdin = '';
$code = Console::execute('php -r "echo \'hello world\'; exit(2);"', $stdin, $output, 10);
$input = '';
$code = Console::execute('php -r "echo \'hello world\'; exit(2);"', $input, $output, 10);

$this->assertEquals('hello world', $output);
$this->assertEquals(2, $code);

$output = '';
$stdin = '';
$code = Console::execute('php -r "echo \'hello world\'; exit(100);"', $stdin, $output, 10);
$input = '';
$code = Console::execute('php -r "echo \'hello world\'; exit(100);"', $input, $output, 10);

$this->assertEquals('hello world', $output);
$this->assertEquals(100, $code);
Expand All @@ -91,15 +91,15 @@ public function testExecuteExitCode()
public function testExecuteTimeout()
{
$output = '';
$stdin = '';
$code = Console::execute('php -r "sleep(1); echo \'hello world\'; exit(0);"', $stdin, $output, 3);
$input = '';
$code = Console::execute('php -r "sleep(1); echo \'hello world\'; exit(0);"', $input, $output, 3);

$this->assertEquals('hello world', $output);
$this->assertEquals(0, $code);

$output = '';
$stdin = '';
$code = Console::execute('php -r "sleep(4); echo \'hello world\'; exit(0);"', $stdin, $output, 3);
$input = '';
$code = Console::execute('php -r "sleep(4); echo \'hello world\'; exit(0);"', $input, $output, 3);

$this->assertEquals('', $output);
$this->assertEquals(1, $code);
Expand All @@ -108,9 +108,9 @@ public function testExecuteTimeout()
public function testLoop()
{
$file = __DIR__.'/../resources/loop.php';
$stdin = '';
$input = '';
$output = '';
$code = Console::execute('php '.$file, $stdin, $output, 30);
$code = Console::execute('php '.$file, $input, $output, 30);

$this->assertGreaterThan(30, count(explode("\n", $output)));
$this->assertLessThan(50, count(explode("\n", $output)));
Expand Down

0 comments on commit 48610e0

Please sign in to comment.