scratch – Blame information for rev 115

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Symfony\Component\Process\Tests;
13  
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Process\Exception\ProcessFailedException;
16  
17 /**
18 * @author Sebastian Marek <proofek@gmail.com>
19 */
20 class ProcessFailedExceptionTest extends TestCase
21 {
22 /**
23 * tests ProcessFailedException throws exception if the process was successful.
24 */
25 public function testProcessFailedExceptionThrowsException()
26 {
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));
31  
32 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(
33 '\InvalidArgumentException',
34 'Expected a failed process, but the given process was successful.'
35 );
36  
37 new ProcessFailedException($process);
38 }
39  
40 /**
41 * tests ProcessFailedException uses information from process output
42 * to generate exception message.
43 */
44 public function testProcessFailedExceptionPopulatesInformationFromProcessOutput()
45 {
46 $cmd = 'php';
47 $exitCode = 1;
48 $exitText = 'General error';
49 $output = 'Command output';
50 $errorOutput = 'FATAL: Unexpected error';
51 $workingDirectory = getcwd();
52  
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));
57  
58 $process->expects($this->once())
59 ->method('getOutput')
60 ->will($this->returnValue($output));
61  
62 $process->expects($this->once())
63 ->method('getErrorOutput')
64 ->will($this->returnValue($errorOutput));
65  
66 $process->expects($this->once())
67 ->method('getExitCode')
68 ->will($this->returnValue($exitCode));
69  
70 $process->expects($this->once())
71 ->method('getExitCodeText')
72 ->will($this->returnValue($exitText));
73  
74 $process->expects($this->once())
75 ->method('isOutputDisabled')
76 ->will($this->returnValue(false));
77  
78 $process->expects($this->once())
79 ->method('getWorkingDirectory')
80 ->will($this->returnValue($workingDirectory));
81  
82 $exception = new ProcessFailedException($process);
83  
84 $this->assertEquals(
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()
87 );
88 }
89  
90 /**
91 * Tests that ProcessFailedException does not extract information from
92 * process output if it was previously disabled.
93 */
94 public function testDisabledOutputInFailedExceptionDoesNotPopulateOutput()
95 {
96 $cmd = 'php';
97 $exitCode = 1;
98 $exitText = 'General error';
99 $workingDirectory = getcwd();
100  
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));
105  
106 $process->expects($this->never())
107 ->method('getOutput');
108  
109 $process->expects($this->never())
110 ->method('getErrorOutput');
111  
112 $process->expects($this->once())
113 ->method('getExitCode')
114 ->will($this->returnValue($exitCode));
115  
116 $process->expects($this->once())
117 ->method('getExitCodeText')
118 ->will($this->returnValue($exitText));
119  
120 $process->expects($this->once())
121 ->method('isOutputDisabled')
122 ->will($this->returnValue(true));
123  
124 $process->expects($this->once())
125 ->method('getWorkingDirectory')
126 ->will($this->returnValue($workingDirectory));
127  
128 $exception = new ProcessFailedException($process);
129  
130 $this->assertEquals(
131 "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}",
132 $exception->getMessage()
133 );
134 }
135 }