diff --git a/src/Symfony/Component/Process/CHANGELOG.md b/src/Symfony/Component/Process/CHANGELOG.md index 37a2f7ab07..3a18983865 100644 --- a/src/Symfony/Component/Process/CHANGELOG.md +++ b/src/Symfony/Component/Process/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index 258a519e9e..2ad1ee40fb 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -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. * diff --git a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php index 6b0dff2edf..fb341d41bb 100644 --- a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php +++ b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php @@ -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(''); diff --git a/src/Symfony/Component/Process/Tests/SigchildDisabledProcessTest.php b/src/Symfony/Component/Process/Tests/SigchildDisabledProcessTest.php index 2e364d6392..2743f29052 100644 --- a/src/Symfony/Component/Process/Tests/SigchildDisabledProcessTest.php +++ b/src/Symfony/Component/Process/Tests/SigchildDisabledProcessTest.php @@ -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 */