4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Process\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Process\Exception\ProcessFailedException;
18 * @author Sebastian Marek <proofek@gmail.com>
20 class ProcessFailedExceptionTest extends TestCase
23 * tests ProcessFailedException throws exception if the process was successful.
25 public function testProcessFailedExceptionThrowsException()
27 $process = $this->getMockBuilder('Symfony\Component\Process\Process')->setMethods(array('isSuccessful'))->setConstructorArgs(array('php'))->getMock();
28 $process->expects($this->once())
29 ->method('isSuccessful')
30 ->will($this->returnValue(true));
32 if (method_exists($this, 'expectException')) {
33 $this->expectException(\InvalidArgumentException::class);
34 $this->expectExceptionMessage('Expected a failed process, but the given process was successful.');
36 $this->setExpectedException(\InvalidArgumentException::class, 'Expected a failed process, but the given process was successful.');
39 new ProcessFailedException($process);
43 * tests ProcessFailedException uses information from process output
44 * to generate exception message.
46 public function testProcessFailedExceptionPopulatesInformationFromProcessOutput()
50 $exitText = 'General error';
51 $output = 'Command output';
52 $errorOutput = 'FATAL: Unexpected error';
53 $workingDirectory = getcwd();
55 $process = $this->getMockBuilder('Symfony\Component\Process\Process')->setMethods(array('isSuccessful', 'getOutput', 'getErrorOutput', 'getExitCode', 'getExitCodeText', 'isOutputDisabled', 'getWorkingDirectory'))->setConstructorArgs(array($cmd))->getMock();
56 $process->expects($this->once())
57 ->method('isSuccessful')
58 ->will($this->returnValue(false));
60 $process->expects($this->once())
62 ->will($this->returnValue($output));
64 $process->expects($this->once())
65 ->method('getErrorOutput')
66 ->will($this->returnValue($errorOutput));
68 $process->expects($this->once())
69 ->method('getExitCode')
70 ->will($this->returnValue($exitCode));
72 $process->expects($this->once())
73 ->method('getExitCodeText')
74 ->will($this->returnValue($exitText));
76 $process->expects($this->once())
77 ->method('isOutputDisabled')
78 ->will($this->returnValue(false));
80 $process->expects($this->once())
81 ->method('getWorkingDirectory')
82 ->will($this->returnValue($workingDirectory));
84 $exception = new ProcessFailedException($process);
87 "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}\n\nOutput:\n================\n{$output}\n\nError Output:\n================\n{$errorOutput}",
88 $exception->getMessage()
93 * Tests that ProcessFailedException does not extract information from
94 * process output if it was previously disabled.
96 public function testDisabledOutputInFailedExceptionDoesNotPopulateOutput()
100 $exitText = 'General error';
101 $workingDirectory = getcwd();
103 $process = $this->getMockBuilder('Symfony\Component\Process\Process')->setMethods(array('isSuccessful', 'isOutputDisabled', 'getExitCode', 'getExitCodeText', 'getOutput', 'getErrorOutput', 'getWorkingDirectory'))->setConstructorArgs(array($cmd))->getMock();
104 $process->expects($this->once())
105 ->method('isSuccessful')
106 ->will($this->returnValue(false));
108 $process->expects($this->never())
109 ->method('getOutput');
111 $process->expects($this->never())
112 ->method('getErrorOutput');
114 $process->expects($this->once())
115 ->method('getExitCode')
116 ->will($this->returnValue($exitCode));
118 $process->expects($this->once())
119 ->method('getExitCodeText')
120 ->will($this->returnValue($exitText));
122 $process->expects($this->once())
123 ->method('isOutputDisabled')
124 ->will($this->returnValue(true));
126 $process->expects($this->once())
127 ->method('getWorkingDirectory')
128 ->will($this->returnValue($workingDirectory));
130 $exception = new ProcessFailedException($process);
133 "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}",
134 $exception->getMessage()