ProcessFailedExceptionTest.php 4.3 KB

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