adds convenience method mustRun

This commit is contained in:
Johannes M. Schmitt 2013-08-03 09:24:32 +02:00
parent 53441aa3d0
commit 2ff187038a
4 changed files with 61 additions and 0 deletions

View File

@ -6,6 +6,7 @@ CHANGELOG
* added the ability to define an idle timeout
* added support for PTY mode
* added the convenience method "mustRun"
2.3.0
-----

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Process;
use Symfony\Component\Process\Exception\InvalidArgumentException;
use Symfony\Component\Process\Exception\LogicException;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Exception\RuntimeException;
@ -209,6 +210,25 @@ class Process
return $this->wait($callback);
}
/**
* Runs the process.
*
* This is identical to run() except that an exception is thrown if the process
* exits with a non-zero exit code.
*
* @param callable|null $callback
*
* @return self
*/
public function mustRun($callback = null)
{
if (0 !== $this->run($callback)) {
throw new ProcessFailedException($this);
}
return $this;
}
/**
* Starts the process and returns after sending the STDIN.
*

View File

@ -217,6 +217,28 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
$this->assertEquals("foo\r\n", $process->getOutput());
}
/**
* @group mustRun
*/
public function testMustRun()
{
$process = $this->getProcess('echo "foo"');
$this->assertSame($process, $process->mustRun());
$this->assertEquals("foo\n", $process->getOutput());
$this->assertEquals(0, $process->getExitCode());
}
/**
* @expectedException Symfony\Component\Process\Exception\ProcessFailedException
* @group mustRun
*/
public function testMustRunThrowsException()
{
$process = $this->getProcess('exit 1');
$process->mustRun();
}
public function testExitCodeText()
{
$process = $this->getProcess('');

View File

@ -29,6 +29,24 @@ class SigchildDisabledProcessTest extends AbstractProcessTest
parent::testExitCodeCommandFailed();
}
/**
* @expectedException \Symfony\Component\Process\Exception\RuntimeException
* @group mustRun
*/
public function testMustRun()
{
parent::testMustRun();
}
/**
* @expectedException \Symfony\Component\Process\Exception\RuntimeException
* @group mustRun
*/
public function testMustRunThrowsException()
{
parent::testMustRunThrowsException();
}
/**
* @expectedException \Symfony\Component\Process\Exception\RuntimeException
*/