ProcessFailedExceptionTest.php 4.8 KB

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