ProcessFailedExceptionTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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->getMockBuilder('Symfony\Component\Process\Process')->setMethods(array('isSuccessful'))->setConstructorArgs(array('php'))->getMock();
  23. $process->expects($this->once())
  24. ->method('isSuccessful')
  25. ->will($this->returnValue(true));
  26. $this->setExpectedException(
  27. '\InvalidArgumentException',
  28. 'Expected a failed process, but the given process was successful.'
  29. );
  30. new ProcessFailedException($process);
  31. }
  32. /**
  33. * tests ProcessFailedException uses information from process output
  34. * to generate exception message.
  35. */
  36. public function testProcessFailedExceptionPopulatesInformationFromProcessOutput()
  37. {
  38. $cmd = 'php';
  39. $exitCode = 1;
  40. $exitText = 'General error';
  41. $output = 'Command output';
  42. $errorOutput = 'FATAL: Unexpected error';
  43. $workingDirectory = getcwd();
  44. $process = $this->getMockBuilder('Symfony\Component\Process\Process')->setMethods(array('isSuccessful', 'getOutput', 'getErrorOutput', 'getExitCode', 'getExitCodeText', 'isOutputDisabled', 'getWorkingDirectory'))->setConstructorArgs(array($cmd))->getMock();
  45. $process->expects($this->once())
  46. ->method('isSuccessful')
  47. ->will($this->returnValue(false));
  48. $process->expects($this->once())
  49. ->method('getOutput')
  50. ->will($this->returnValue($output));
  51. $process->expects($this->once())
  52. ->method('getErrorOutput')
  53. ->will($this->returnValue($errorOutput));
  54. $process->expects($this->once())
  55. ->method('getExitCode')
  56. ->will($this->returnValue($exitCode));
  57. $process->expects($this->once())
  58. ->method('getExitCodeText')
  59. ->will($this->returnValue($exitText));
  60. $process->expects($this->once())
  61. ->method('isOutputDisabled')
  62. ->will($this->returnValue(false));
  63. $process->expects($this->once())
  64. ->method('getWorkingDirectory')
  65. ->will($this->returnValue($workingDirectory));
  66. $exception = new ProcessFailedException($process);
  67. $this->assertEquals(
  68. "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}\n\nOutput:\n================\n{$output}\n\nError Output:\n================\n{$errorOutput}",
  69. $exception->getMessage()
  70. );
  71. }
  72. /**
  73. * Tests that ProcessFailedException does not extract information from
  74. * process output if it was previously disabled.
  75. */
  76. public function testDisabledOutputInFailedExceptionDoesNotPopulateOutput()
  77. {
  78. $cmd = 'php';
  79. $exitCode = 1;
  80. $exitText = 'General error';
  81. $workingDirectory = getcwd();
  82. $process = $this->getMockBuilder('Symfony\Component\Process\Process')->setMethods(array('isSuccessful', 'isOutputDisabled', 'getExitCode', 'getExitCodeText', 'getOutput', 'getErrorOutput', 'getWorkingDirectory'))->setConstructorArgs(array($cmd))->getMock();
  83. $process->expects($this->once())
  84. ->method('isSuccessful')
  85. ->will($this->returnValue(false));
  86. $process->expects($this->never())
  87. ->method('getOutput');
  88. $process->expects($this->never())
  89. ->method('getErrorOutput');
  90. $process->expects($this->once())
  91. ->method('getExitCode')
  92. ->will($this->returnValue($exitCode));
  93. $process->expects($this->once())
  94. ->method('getExitCodeText')
  95. ->will($this->returnValue($exitText));
  96. $process->expects($this->once())
  97. ->method('isOutputDisabled')
  98. ->will($this->returnValue(true));
  99. $process->expects($this->once())
  100. ->method('getWorkingDirectory')
  101. ->will($this->returnValue($workingDirectory));
  102. $exception = new ProcessFailedException($process);
  103. $this->assertEquals(
  104. "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}",
  105. $exception->getMessage()
  106. );
  107. }
  108. }