From 2c7a2dee5ceb4dbbf8fd8be377b55892e8162015 Mon Sep 17 00:00:00 2001 From: techno-express Date: Fri, 14 Feb 2020 21:43:21 -0500 Subject: [PATCH] BC - corrections, name changes, revert, bug fix - have parent output on `wait()` call --- .vscode/settings.json | 3 +++ Processor/Launcher.php | 20 ++++++++++++-------- Processor/functions.php | 2 +- appveyor.yml | 8 -------- composer.json | 2 +- tests/ErrorHandlingTest.php | 6 +++--- tests/ProcessorTest.php | 25 ++++++++++++------------- 7 files changed, 32 insertions(+), 34 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index a891e1f..f0396ba 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,7 +2,10 @@ "cSpell.words": [ "Commandline", "Symplely", + "autoload", + "opis", "pcntl", + "phpunit", "symfony" ] } \ No newline at end of file diff --git a/Processor/Launcher.php b/Processor/Launcher.php index b95375c..aa6cf38 100644 --- a/Processor/Launcher.php +++ b/Processor/Launcher.php @@ -55,7 +55,7 @@ public function start(): self $this->startTime = \microtime(true); $this->process->start(function ($type, $buffer) { - $this->realTimeOutput = $buffer; + $this->realTimeOutput .= $buffer; }); $this->pid = $this->process->getPid(); @@ -101,6 +101,11 @@ public function wait($waitTimer = 1000, bool $useYield = false) return $this->triggerTimeout(); } + if ($this->showOutput) { + \printf('%s', $this->getRealOutput()); + $this->realOutput = $this->realTimeOutput = null; + } + if ($useYield) $this->yieldLiveUpdate($this->getRealOutput()); else @@ -145,12 +150,7 @@ public function isTimedOut(): bool public function isRunning(): bool { - $isRunning = $this->process->isRunning(); - if ($isRunning && $this->showOutput) { - echo $this->getRealOutput(); - } - - return $isRunning; + return $this->process->isRunning(); } public function showOutput(): self @@ -198,10 +198,14 @@ public function getRealOutput() $this->realOutput = $processOutput; } } elseif ($this->realTimeOutput) { - $this->realOutput .= @\unserialize(\base64_decode((string) $this->realTimeOutput)); + $this->realOutput = @\unserialize(\base64_decode((string) $this->realTimeOutput)); $this->realTimeOutput = null; } + $this->realOutput = \is_string($this->realOutput) + ? \rtrim($this->realOutput, 'Tjs=') + : $this->realOutput; + return $this->realOutput; } diff --git a/Processor/functions.php b/Processor/functions.php index d47d262..7d288b6 100644 --- a/Processor/functions.php +++ b/Processor/functions.php @@ -20,7 +20,7 @@ function spawn($shellCallable, int $timeout = 300, $processChannel = null): Laun return Processor::create($shellCallable, $timeout, $processChannel); } - function await_spawn(LauncherInterface $process) + function spawn_run(LauncherInterface $process) { return $process->run(); } diff --git a/appveyor.yml b/appveyor.yml index d36da20..4daadf9 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -32,17 +32,10 @@ install: - IF %PHP%==1 echo extension=php_openssl.dll >> php.ini - IF %PHP%==1 echo extension=php_mbstring.dll >> php.ini - IF %PHP%==1 echo extension=php_fileinfo.dll >> php.ini - - IF %PHP%==1 echo extension=php_sockets.dll >> php.ini - ps: >- If ($env:PHP -eq "1") { - Invoke-WebRequest "https://windows.php.net/downloads/pecl/releases/uv/0.2.4/php_uv-0.2.4-7.3-nts-vc15-x64.zip" -OutFile "php_uv-0.2.4-7.3-nts-vc15-x64.zip" - 7z x -y php_uv-0.2.4-7.3-nts-vc15-x64.zip libuv.dll php_uv.dll - copy php_uv.dll ext\php_uv.dll - del php_uv.dll - del php_uv-0.2.4-7.3-nts-vc15-x64.zip Invoke-WebRequest "https://xdebug.org/files/php_xdebug-2.7.2-7.3-vc15-nts-x86_64.dll" -OutFile "C:\tools\php73\ext\php_xdebug-2.7.2-7.3-vc15-nts-x86_64.dll" } - - IF %PHP%==1 echo extension=php_uv.dll >> php.ini - IF %PHP%==1 echo [xdebug] >> php.ini - IF %PHP%==1 echo zend_extension=php_xdebug-2.7.2-7.3-vc15-nts-x86_64.dll >> php.ini - IF %PHP%==1 echo zend.assertions=1 >> php.ini @@ -51,7 +44,6 @@ install: - IF %PHP%==1 echo xdebug.remote_autostart=1 >> php.ini - IF %PHP%==1 echo xdebug.profiler_enable=off >> php.ini - cd c:\projects\php-project-workspace - - php -m - composer self-update on_success: diff --git a/composer.json b/composer.json index 0cd9d30..732dcea 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ } }, "require-dev": { - "phpunit/phpunit": ">5.7" + "phpunit/phpunit": "^6 | ^7 | ^8" }, "autoload-dev": { "psr-4": { diff --git a/tests/ErrorHandlingTest.php b/tests/ErrorHandlingTest.php index c119a5c..21833e3 100644 --- a/tests/ErrorHandlingTest.php +++ b/tests/ErrorHandlingTest.php @@ -18,7 +18,7 @@ public function testIt_can_handle_exceptions_via_catch_callback() $this->assertRegExp('/test/', $e->getMessage()); }); - await_spawn($process); + spawn_run($process); $this->assertFalse($process->isSuccessful()); $this->assertTrue($process->isTerminated()); } @@ -35,7 +35,7 @@ public function testIt_can_handle_exceptions_via_catch_callback_yield() $this->assertNull($pause->current()); $this->assertTrue($process->isTerminated()); } - + public function testIt_handles_stderr_as_processor_error() { $process = spawn(function () { @@ -44,7 +44,7 @@ public function testIt_handles_stderr_as_processor_error() $this->assertStringContainsString('test', $error->getMessage()); }); - await_spawn($process); + spawn_run($process); $this->assertTrue($process->isSuccessful()); $this->assertEquals('test', $process->getErrorOutput()); $this->assertNull($process->getOutput()); diff --git a/tests/ProcessorTest.php b/tests/ProcessorTest.php index 16bc9d9..c28d572 100644 --- a/tests/ProcessorTest.php +++ b/tests/ProcessorTest.php @@ -19,7 +19,7 @@ public function testIt_can_handle_success() $counter = $output; }); - await_spawn($process); + spawn_run($process); $this->assertTrue($process->isSuccessful()); $this->assertEquals(2, $counter); @@ -103,9 +103,8 @@ public function testLiveOutput() $process = Processor::create(function () { echo 'hello child'; }); - $this->expectOutputRegex('/hello child/'); - $process->showOutput()->start();; - $process->wait(); + $this->expectOutputString('hello child'); + $process->showOutput()->run(); } public function testGetOutputShell() @@ -147,7 +146,7 @@ public function testGetErrorOutput() $this->assertEquals(3, preg_match_all('/ERROR/', $error->getMessage(), $matches)); }); - await_spawn($p); + spawn_run($p); } public function testGetErrorOutputYield() @@ -266,13 +265,13 @@ public function testPhpPathExecutable() $this->assertEquals('default', $result); } - public function testLargeOutputs() - { - $process = Processor::create(function () { - return str_repeat('abcd', 1024 * 512); - }); + public function testLargeOutputs() + { + $process = Processor::create(function () { + return str_repeat('abcd', 1024 * 512); + }); - $process->run(); - $this->assertEquals(str_repeat('abcd', 1024 * 512), $process->getOutput()); - } + $process->run(); + $this->assertEquals(str_repeat('abcd', 1024 * 512), $process->getOutput()); + } }