ProcessFailedExceptionTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. $workingDirectory = getcwd();
  48. $process = $this->getMock(
  49. 'Symfony\Component\Process\Process',
  50. array('isSuccessful', 'getOutput', 'getErrorOutput', 'getExitCode', 'getExitCodeText', 'isOutputDisabled', 'getWorkingDirectory'),
  51. array($cmd)
  52. );
  53. $process->expects($this->once())
  54. ->method('isSuccessful')
  55. ->will($this->returnValue(false));
  56. $process->expects($this->once())
  57. ->method('getOutput')
  58. ->will($this->returnValue($output));
  59. $process->expects($this->once())
  60. ->method('getErrorOutput')
  61. ->will($this->returnValue($errorOutput));
  62. $process->expects($this->once())
  63. ->method('getExitCode')
  64. ->will($this->returnValue($exitCode));
  65. $process->expects($this->once())
  66. ->method('getExitCodeText')
  67. ->will($this->returnValue($exitText));
  68. $process->expects($this->once())
  69. ->method('isOutputDisabled')
  70. ->will($this->returnValue(false));
  71. $process->expects($this->once())
  72. ->method('getWorkingDirectory')
  73. ->will($this->returnValue($workingDirectory));
  74. $exception = new ProcessFailedException($process);
  75. $this->assertEquals(
  76. "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}\n\nOutput:\n================\n{$output}\n\nError Output:\n================\n{$errorOutput}",
  77. $exception->getMessage()
  78. );
  79. }
  80. /**
  81. * Tests that ProcessFailedException does not extract information from
  82. * process output if it was previously disabled.
  83. */
  84. public function testDisabledOutputInFailedExceptionDoesNotPopulateOutput()
  85. {
  86. $cmd = 'php';
  87. $exitCode = 1;
  88. $exitText = 'General error';
  89. $workingDirectory = getcwd();
  90. $process = $this->getMock(
  91. 'Symfony\Component\Process\Process',
  92. array('isSuccessful', 'isOutputDisabled', 'getExitCode', 'getExitCodeText', 'getOutput', 'getErrorOutput', 'getWorkingDirectory'),
  93. array($cmd)
  94. );
  95. $process->expects($this->once())
  96. ->method('isSuccessful')
  97. ->will($this->returnValue(false));
  98. $process->expects($this->never())
  99. ->method('getOutput');
  100. $process->expects($this->never())
  101. ->method('getErrorOutput');
  102. $process->expects($this->once())
  103. ->method('getExitCode')
  104. ->will($this->returnValue($exitCode));
  105. $process->expects($this->once())
  106. ->method('getExitCodeText')
  107. ->will($this->returnValue($exitText));
  108. $process->expects($this->once())
  109. ->method('isOutputDisabled')
  110. ->will($this->returnValue(true));
  111. $process->expects($this->once())
  112. ->method('getWorkingDirectory')
  113. ->will($this->returnValue($workingDirectory));
  114. $exception = new ProcessFailedException($process);
  115. $this->assertEquals(
  116. "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}",
  117. $exception->getMessage()
  118. );
  119. }
  120. }