File tree Expand file tree Collapse file tree 4 files changed +61
-0
lines changed
src/Symfony/Component/Process Expand file tree Collapse file tree 4 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ CHANGELOG
66
77 * added the ability to define an idle timeout
88 * added support for PTY mode
9+ * added the convenience method "mustRun"
910
10112.3.0
1112-----
Original file line number Diff line number Diff line change 1313
1414use Symfony \Component \Process \Exception \InvalidArgumentException ;
1515use Symfony \Component \Process \Exception \LogicException ;
16+ use Symfony \Component \Process \Exception \ProcessFailedException ;
1617use Symfony \Component \Process \Exception \ProcessTimedOutException ;
1718use Symfony \Component \Process \Exception \RuntimeException ;
1819
@@ -209,6 +210,25 @@ public function run($callback = null)
209210 return $ this ->wait ($ callback );
210211 }
211212
213+ /**
214+ * Runs the process.
215+ *
216+ * This is identical to run() except that an exception is thrown if the process
217+ * exits with a non-zero exit code.
218+ *
219+ * @param callable|null $callback
220+ *
221+ * @return self
222+ */
223+ public function mustRun ($ callback = null )
224+ {
225+ if (0 !== $ this ->run ($ callback )) {
226+ throw new ProcessFailedException ($ this );
227+ }
228+
229+ return $ this ;
230+ }
231+
212232 /**
213233 * Starts the process and returns after sending the STDIN.
214234 *
Original file line number Diff line number Diff line change @@ -217,6 +217,28 @@ public function testPTYCommand()
217217 $ this ->assertEquals ("foo \r\n" , $ process ->getOutput ());
218218 }
219219
220+ /**
221+ * @group mustRun
222+ */
223+ public function testMustRun ()
224+ {
225+ $ process = $ this ->getProcess ('echo "foo" ' );
226+
227+ $ this ->assertSame ($ process , $ process ->mustRun ());
228+ $ this ->assertEquals ("foo \n" , $ process ->getOutput ());
229+ $ this ->assertEquals (0 , $ process ->getExitCode ());
230+ }
231+
232+ /**
233+ * @expectedException Symfony\Component\Process\Exception\ProcessFailedException
234+ * @group mustRun
235+ */
236+ public function testMustRunThrowsException ()
237+ {
238+ $ process = $ this ->getProcess ('exit 1 ' );
239+ $ process ->mustRun ();
240+ }
241+
220242 public function testExitCodeText ()
221243 {
222244 $ process = $ this ->getProcess ('' );
Original file line number Diff line number Diff line change @@ -29,6 +29,24 @@ public function testExitCodeCommandFailed()
2929 parent ::testExitCodeCommandFailed ();
3030 }
3131
32+ /**
33+ * @expectedException \Symfony\Component\Process\Exception\RuntimeException
34+ * @group mustRun
35+ */
36+ public function testMustRun ()
37+ {
38+ parent ::testMustRun ();
39+ }
40+
41+ /**
42+ * @expectedException \Symfony\Component\Process\Exception\RuntimeException
43+ * @group mustRun
44+ */
45+ public function testMustRunThrowsException ()
46+ {
47+ parent ::testMustRunThrowsException ();
48+ }
49+
3250 /**
3351 * @expectedException \Symfony\Component\Process\Exception\RuntimeException
3452 */
You can’t perform that action at this time.
0 commit comments