ProcessFailedExceptionTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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', 'isOutputDisabled'),
  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. $process->expects($this->once())
  68. ->method('isOutputDisabled')
  69. ->will($this->returnValue(false));
  70. $exception = new ProcessFailedException($process);
  71. $this->assertEquals(
  72. "The command \"$cmd\" failed.\nExit Code: $exitCode($exitText)\n\nOutput:\n================\n{$output}\n\nError Output:\n================\n{$errorOutput}",
  73. $exception->getMessage()
  74. );
  75. }
  76. /**
  77. * Tests that ProcessFailedException does not extract information from
  78. * process output if it was previously disabled
  79. */
  80. public function testDisabledOutputInFailedExceptionDoesNotPopulateOutput()
  81. {
  82. $cmd = 'php';
  83. $exitCode = 1;
  84. $exitText = 'General error';
  85. $process = $this->getMock(
  86. 'Symfony\Component\Process\Process',
  87. array('isSuccessful', 'isOutputDisabled', 'getExitCode', 'getExitCodeText', 'getOutput', 'getErrorOutput'),
  88. array($cmd)
  89. );
  90. $process->expects($this->once())
  91. ->method('isSuccessful')
  92. ->will($this->returnValue(false));
  93. $process->expects($this->never())
  94. ->method('getOutput');
  95. $process->expects($this->never())
  96. ->method('getErrorOutput');
  97. $process->expects($this->once())
  98. ->method('getExitCode')
  99. ->will($this->returnValue($exitCode));
  100. $process->expects($this->once())
  101. ->method('getExitCodeText')
  102. ->will($this->returnValue($exitText));
  103. $process->expects($this->once())
  104. ->method('isOutputDisabled')
  105. ->will($this->returnValue(true));
  106. $exception = new ProcessFailedException($process);
  107. $this->assertEquals(
  108. "The command \"$cmd\" failed.\nExit Code: $exitCode($exitText)",
  109. $exception->getMessage()
  110. );
  111. }
  112. }