ProcessFailedExceptionTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Process\Tests;
  11. use Symfony\Component\Process\Process,
  12. Symfony\Component\Process\Exception\ProcessFailedException;
  13. /**
  14. * @author Sebastian Marek <proofek@gmail.com>
  15. */
  16. class ProcessFailedExceptionTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * tests ProcessFailedException throws exception if the process was successful
  20. */
  21. public function testProcessFailedExceptionThrowsException()
  22. {
  23. $process = $this->getMock(
  24. 'Symfony\Component\Process\Process',
  25. array('isSuccessful'),
  26. array('php')
  27. );
  28. $process->expects($this->once())
  29. ->method('isSuccessful')
  30. ->will($this->returnValue(true));
  31. $this->setExpectedException(
  32. '\InvalidArgumentException',
  33. 'Expected a failed process, but the given process was successful.'
  34. );
  35. new ProcessFailedException($process);
  36. }
  37. /**
  38. * tests ProcessFailedException uses information from process output
  39. * to generate exception message
  40. */
  41. public function testProcessFailedExceptionPopulatesInformationFromProcessOutput()
  42. {
  43. $cmd = 'php';
  44. $output = "Command output";
  45. $errorOutput = "FATAL: Unexpected error";
  46. $process = $this->getMock(
  47. 'Symfony\Component\Process\Process',
  48. array('isSuccessful', 'getOutput', 'getErrorOutput'),
  49. array($cmd)
  50. );
  51. $process->expects($this->once())
  52. ->method('isSuccessful')
  53. ->will($this->returnValue(false));
  54. $process->expects($this->once())
  55. ->method('getOutput')
  56. ->will($this->returnValue($output));
  57. $process->expects($this->once())
  58. ->method('getErrorOutput')
  59. ->will($this->returnValue($errorOutput));
  60. $exception = new ProcessFailedException($process);
  61. $this->assertEquals(
  62. "The command \"$cmd\" failed.\n\nOutput:\n================\n{$output}\n\nError Output:\n================\n{$errorOutput}",
  63. $exception->getMessage()
  64. );
  65. }
  66. }