Skip to content

Commit

Permalink
[Process] Use a consistent way to reset data of the process latest run
Browse files Browse the repository at this point in the history
It is actually useful when cloning or running again a process.
  • Loading branch information
romainneutron committed Aug 20, 2013
1 parent d58a1e3 commit 0ed724e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
38 changes: 23 additions & 15 deletions Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,7 @@ public function __destruct()

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();
}

/**
Expand Down Expand Up @@ -231,11 +221,8 @@ public function start($callback = null)
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.
Expand Down Expand Up @@ -1154,4 +1141,25 @@ private function processReadPipes(array $pipes)
}
}
}

/**
* 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;
}
}
21 changes: 21 additions & 0 deletions Tests/AbstractProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,27 @@ public function testUpdateStatus()
$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');
Expand Down
16 changes: 16 additions & 0 deletions Tests/SigchildDisabledProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ public function testGetExitCode()
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
*/
Expand Down

0 comments on commit 0ed724e

Please sign in to comment.