[Process] fixed fatal errors in getOutput and getErrorOutput when process was not started

This commit is contained in:
Max Voloshin 2013-11-06 00:13:55 +02:00 committed by Romain Neutron
parent 02088bc62f
commit 0ae685878c
2 changed files with 47 additions and 0 deletions

View File

@ -296,9 +296,14 @@ class Process
*
* @throws RuntimeException When process timed out
* @throws RuntimeException When process stopped after receiving signal
* @throws LogicException When process is not started
*/
public function wait($callback = null)
{
if (!$this->isStarted()) {
throw new LogicException(sprintf('Process must be started before calling %s', __FUNCTION__));
}
$this->updateStatus(false);
if (null !== $callback) {
$this->callback = $this->buildCallback($callback);
@ -366,10 +371,16 @@ class Process
*
* @return string The process output
*
* @throws LogicException In case the process is not started
*
* @api
*/
public function getOutput()
{
if (!$this->isStarted()) {
throw new LogicException(sprintf('Process must be started before calling %s', __FUNCTION__));
}
$this->readPipes(false, defined('PHP_WINDOWS_VERSION_BUILD') ? !$this->processInformation['running'] : true);
return $this->stdout;
@ -398,10 +409,16 @@ class Process
*
* @return string The process error output
*
* @throws LogicException In case the process is not started
*
* @api
*/
public function getErrorOutput()
{
if (!$this->isStarted()) {
throw new LogicException(sprintf('Process must be started before calling %s', __FUNCTION__));
}
$this->readPipes(false, defined('PHP_WINDOWS_VERSION_BUILD') ? !$this->processInformation['running'] : true);
return $this->stderr;

View File

@ -617,6 +617,36 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
$process->signal(SIGHUP);
}
/**
* @expectedException \Symfony\Component\Process\Exception\LogicException
* @expectedExceptionMessage Process must be started before calling getOutput
*/
public function testGetOutputProcessNotStarted()
{
$process = $this->getProcess('php -m');
$process->getOutput();
}
/**
* @expectedException \Symfony\Component\Process\Exception\LogicException
* @expectedExceptionMessage Process must be started before calling getErrorOutput
*/
public function testGetErrorOutputProcessNotStarted()
{
$process = $this->getProcess('php -m');
$process->getErrorOutput();
}
/**
* @expectedException \Symfony\Component\Process\Exception\LogicException
* @expectedExceptionMessage Process must be started before calling wait
*/
public function testWaitProcessWithoutTimeoutNotStarted()
{
$process = $this->getProcess('php -m')->setTimeout(null);
$process->wait();
}
private function verifyPosixIsEnabled()
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {