ProcessFailedExceptionTest.php 4.6 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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Process\Exception\ProcessFailedException;
  13. use Symfony\Component\Process\Process;
  14. /**
  15. * @author Sebastian Marek <proofek@gmail.com>
  16. */
  17. class ProcessFailedExceptionTest extends TestCase
  18. {
  19. /**
  20. * tests ProcessFailedException throws exception if the process was successful.
  21. */
  22. public function testProcessFailedExceptionThrowsException()
  23. {
  24. $process = $this->getMockBuilder(Process::class)->onlyMethods(['isSuccessful'])->setConstructorArgs([['php']])->getMock();
  25. $process->expects($this->once())
  26. ->method('isSuccessful')
  27. ->willReturn(true);
  28. $this->expectException(\InvalidArgumentException::class);
  29. $this->expectExceptionMessage('Expected a failed process, but the given process was successful.');
  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(Process::class)->onlyMethods(['isSuccessful', 'getOutput', 'getErrorOutput', 'getExitCode', 'getExitCodeText', 'isOutputDisabled', 'getWorkingDirectory'])->setConstructorArgs([[$cmd]])->getMock();
  45. $process->expects($this->once())
  46. ->method('isSuccessful')
  47. ->willReturn(false);
  48. $process->expects($this->once())
  49. ->method('getOutput')
  50. ->willReturn($output);
  51. $process->expects($this->once())
  52. ->method('getErrorOutput')
  53. ->willReturn($errorOutput);
  54. $process->expects($this->once())
  55. ->method('getExitCode')
  56. ->willReturn($exitCode);
  57. $process->expects($this->once())
  58. ->method('getExitCodeText')
  59. ->willReturn($exitText);
  60. $process->expects($this->once())
  61. ->method('isOutputDisabled')
  62. ->willReturn(false);
  63. $process->expects($this->once())
  64. ->method('getWorkingDirectory')
  65. ->willReturn($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. str_replace("'php'", 'php', $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(Process::class)->onlyMethods(['isSuccessful', 'isOutputDisabled', 'getExitCode', 'getExitCodeText', 'getOutput', 'getErrorOutput', 'getWorkingDirectory'])->setConstructorArgs([[$cmd]])->getMock();
  83. $process->expects($this->once())
  84. ->method('isSuccessful')
  85. ->willReturn(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. ->willReturn($exitCode);
  93. $process->expects($this->once())
  94. ->method('getExitCodeText')
  95. ->willReturn($exitText);
  96. $process->expects($this->once())
  97. ->method('isOutputDisabled')
  98. ->willReturn(true);
  99. $process->expects($this->once())
  100. ->method('getWorkingDirectory')
  101. ->willReturn($workingDirectory);
  102. $exception = new ProcessFailedException($process);
  103. $this->assertEquals(
  104. "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}",
  105. str_replace("'php'", 'php', $exception->getMessage())
  106. );
  107. }
  108. }