ProcessFailedExceptionTest.php 4.6 KB

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