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 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(
33 '\InvalidArgumentException',
34 'Expected a failed process, but the given process was successful.'
37 new ProcessFailedException($process);
41 * tests ProcessFailedException uses information from process output
42 * to generate exception message.
44 public function testProcessFailedExceptionPopulatesInformationFromProcessOutput()
48 $exitText = 'General error';
49 $output = 'Command output';
50 $errorOutput = 'FATAL: Unexpected error';
51 $workingDirectory = getcwd();
53 $process = $this->getMockBuilder('Symfony\Component\Process\Process')->setMethods(array('isSuccessful', 'getOutput', 'getErrorOutput', 'getExitCode', 'getExitCodeText', 'isOutputDisabled', 'getWorkingDirectory'))->setConstructorArgs(array($cmd))->getMock();
54 $process->expects($this->once())
55 ->method('isSuccessful')
56 ->will($this->returnValue(false));
58 $process->expects($this->once())
60 ->will($this->returnValue($output));
62 $process->expects($this->once())
63 ->method('getErrorOutput')
64 ->will($this->returnValue($errorOutput));
66 $process->expects($this->once())
67 ->method('getExitCode')
68 ->will($this->returnValue($exitCode));
70 $process->expects($this->once())
71 ->method('getExitCodeText')
72 ->will($this->returnValue($exitText));
74 $process->expects($this->once())
75 ->method('isOutputDisabled')
76 ->will($this->returnValue(false));
78 $process->expects($this->once())
79 ->method('getWorkingDirectory')
80 ->will($this->returnValue($workingDirectory));
82 $exception = new ProcessFailedException($process);
85 "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}\n\nOutput:\n================\n{$output}\n\nError Output:\n================\n{$errorOutput}",
86 $exception->getMessage()
91 * Tests that ProcessFailedException does not extract information from
92 * process output if it was previously disabled.
94 public function testDisabledOutputInFailedExceptionDoesNotPopulateOutput()
98 $exitText = 'General error';
99 $workingDirectory = getcwd();
101 $process = $this->getMockBuilder('Symfony\Component\Process\Process')->setMethods(array('isSuccessful', 'isOutputDisabled', 'getExitCode', 'getExitCodeText', 'getOutput', 'getErrorOutput', 'getWorkingDirectory'))->setConstructorArgs(array($cmd))->getMock();
102 $process->expects($this->once())
103 ->method('isSuccessful')
104 ->will($this->returnValue(false));
106 $process->expects($this->never())
107 ->method('getOutput');
109 $process->expects($this->never())
110 ->method('getErrorOutput');
112 $process->expects($this->once())
113 ->method('getExitCode')
114 ->will($this->returnValue($exitCode));
116 $process->expects($this->once())
117 ->method('getExitCodeText')
118 ->will($this->returnValue($exitText));
120 $process->expects($this->once())
121 ->method('isOutputDisabled')
122 ->will($this->returnValue(true));
124 $process->expects($this->once())
125 ->method('getWorkingDirectory')
126 ->will($this->returnValue($workingDirectory));
128 $exception = new ProcessFailedException($process);
131 "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}",
132 $exception->getMessage()