ProcessFailedExceptionTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Exception\ProcessFailedException;
  12. /**
  13. * @author Sebastian Marek <proofek@gmail.com>
  14. */
  15. class ProcessFailedExceptionTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * tests ProcessFailedException throws exception if the process was successful
  19. */
  20. public function testProcessFailedExceptionThrowsException()
  21. {
  22. $process = $this->getMock(
  23. 'Symfony\Component\Process\Process',
  24. array('isSuccessful'),
  25. array('php')
  26. );
  27. $process->expects($this->once())
  28. ->method('isSuccessful')
  29. ->will($this->returnValue(true));
  30. $this->setExpectedException(
  31. '\InvalidArgumentException',
  32. 'Expected a failed process, but the given process was successful.'
  33. );
  34. new ProcessFailedException($process);
  35. }
  36. /**
  37. * tests ProcessFailedException uses information from process output
  38. * to generate exception message
  39. */
  40. public function testProcessFailedExceptionPopulatesInformationFromProcessOutput()
  41. {
  42. $cmd = 'php';
  43. $exitCode = 1;
  44. $exitText = 'General error';
  45. $output = "Command output";
  46. $errorOutput = "FATAL: Unexpected error";
  47. $process = $this->getMock(
  48. 'Symfony\Component\Process\Process',
  49. array('isSuccessful', 'getOutput', 'getErrorOutput', 'getExitCode', 'getExitCodeText'),
  50. array($cmd)
  51. );
  52. $process->expects($this->once())
  53. ->method('isSuccessful')
  54. ->will($this->returnValue(false));
  55. $process->expects($this->once())
  56. ->method('getOutput')
  57. ->will($this->returnValue($output));
  58. $process->expects($this->once())
  59. ->method('getErrorOutput')
  60. ->will($this->returnValue($errorOutput));
  61. $process->expects($this->once())
  62. ->method('getExitCode')
  63. ->will($this->returnValue($exitCode));
  64. $process->expects($this->once())
  65. ->method('getExitCodeText')
  66. ->will($this->returnValue($exitText));
  67. $exception = new ProcessFailedException($process);
  68. $this->assertEquals(
  69. "The command \"$cmd\" failed.\nExit Code: $exitCode($exitText)\n\nOutput:\n================\n{$output}\n\nError Output:\n================\n{$errorOutput}",
  70. $exception->getMessage()
  71. );
  72. }
  73. }