From 0723c10239025561895a342c6df1a8ed53ae1501 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 20 Aug 2013 20:22:08 +0200 Subject: [PATCH] [Process] Use a consistent way to reset data of the process latest run It is actually useful when cloning or running again a process. --- src/Symfony/Component/Process/Process.php | 38 +++++++++++-------- .../Process/Tests/AbstractProcessTest.php | 21 ++++++++++ .../Tests/SigchildDisabledProcessTest.php | 16 ++++++++ 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index 91634297d8..cd84cd4420 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -165,17 +165,7 @@ class Process public function __clone() { - $this->callback = null; - $this->exitcode = null; - $this->fallbackExitcode = null; - $this->processInformation = null; - $this->stdout = null; - $this->stderr = null; - $this->pipes = null; - $this->process = null; - $this->status = self::STATUS_READY; - $this->fileHandles = null; - $this->readBytes = null; + $this->resetProcessData(); } /** @@ -231,11 +221,8 @@ class Process throw new RuntimeException('Process is already running'); } + $this->resetProcessData(); $this->starttime = microtime(true); - $this->stdout = ''; - $this->stderr = ''; - $this->incrementalOutputOffset = 0; - $this->incrementalErrorOutputOffset = 0; $this->callback = $this->buildCallback($callback); //Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big. @@ -1154,4 +1141,25 @@ class Process } } } + + /** + * Resets data related to the latest run of the process. + */ + private function resetProcessData() + { + $this->starttime = null; + $this->callback = null; + $this->exitcode = null; + $this->fallbackExitcode = null; + $this->processInformation = null; + $this->stdout = null; + $this->stderr = null; + $this->pipes = null; + $this->process = null; + $this->status = self::STATUS_READY; + $this->fileHandles = null; + $this->readBytes = null; + $this->incrementalOutputOffset = 0; + $this->incrementalErrorOutputOffset = 0; + } } diff --git a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php index 1021746e76..e78374ae3d 100644 --- a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php +++ b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php @@ -235,6 +235,27 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase $this->assertTrue(strlen($process->getOutput()) > 0); } + public function testGetExitCodeIsNullOnStart() + { + $process = $this->getProcess('php -r "usleep(200000);"'); + $this->assertNull($process->getExitCode()); + $process->start(); + $this->assertNull($process->getExitCode()); + $process->wait(); + $this->assertEquals(0, $process->getExitCode()); + } + + public function testGetExitCodeIsNullOnWhenStartingAgain() + { + $process = $this->getProcess('php -r "usleep(200000);"'); + $process->run(); + $this->assertEquals(0, $process->getExitCode()); + $process->start(); + $this->assertNull($process->getExitCode()); + $process->wait(); + $this->assertEquals(0, $process->getExitCode()); + } + public function testGetExitCode() { $process = $this->getProcess('php -m'); diff --git a/src/Symfony/Component/Process/Tests/SigchildDisabledProcessTest.php b/src/Symfony/Component/Process/Tests/SigchildDisabledProcessTest.php index 1e4dc1d0a2..0ec96f4765 100644 --- a/src/Symfony/Component/Process/Tests/SigchildDisabledProcessTest.php +++ b/src/Symfony/Component/Process/Tests/SigchildDisabledProcessTest.php @@ -21,6 +21,22 @@ class SigchildDisabledProcessTest extends AbstractProcessTest parent::testGetExitCode(); } + /** + * @expectedException \Symfony\Component\Process\Exception\RuntimeException + */ + public function testGetExitCodeIsNullOnStart() + { + parent::testGetExitCodeIsNullOnStart(); + } + + /** + * @expectedException \Symfony\Component\Process\Exception\RuntimeException + */ + public function testGetExitCodeIsNullOnWhenStartingAgain() + { + parent::testGetExitCodeIsNullOnWhenStartingAgain(); + } + /** * @expectedException \Symfony\Component\Process\Exception\RuntimeException */