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