Pull merge.
[yaffs-website] / vendor / symfony / process / Tests / ProcessFailedExceptionTest.php
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         if (method_exists($this, 'expectException')) {
33             $this->expectException(\InvalidArgumentException::class);
34             $this->expectExceptionMessage('Expected a failed process, but the given process was successful.');
35         } else {
36             $this->setExpectedException(\InvalidArgumentException::class, 'Expected a failed process, but the given process was successful.');
37         }
38
39         new ProcessFailedException($process);
40     }
41
42     /**
43      * tests ProcessFailedException uses information from process output
44      * to generate exception message.
45      */
46     public function testProcessFailedExceptionPopulatesInformationFromProcessOutput()
47     {
48         $cmd = 'php';
49         $exitCode = 1;
50         $exitText = 'General error';
51         $output = 'Command output';
52         $errorOutput = 'FATAL: Unexpected error';
53         $workingDirectory = getcwd();
54
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));
59
60         $process->expects($this->once())
61             ->method('getOutput')
62             ->will($this->returnValue($output));
63
64         $process->expects($this->once())
65             ->method('getErrorOutput')
66             ->will($this->returnValue($errorOutput));
67
68         $process->expects($this->once())
69             ->method('getExitCode')
70             ->will($this->returnValue($exitCode));
71
72         $process->expects($this->once())
73             ->method('getExitCodeText')
74             ->will($this->returnValue($exitText));
75
76         $process->expects($this->once())
77             ->method('isOutputDisabled')
78             ->will($this->returnValue(false));
79
80         $process->expects($this->once())
81             ->method('getWorkingDirectory')
82             ->will($this->returnValue($workingDirectory));
83
84         $exception = new ProcessFailedException($process);
85
86         $this->assertEquals(
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()
89         );
90     }
91
92     /**
93      * Tests that ProcessFailedException does not extract information from
94      * process output if it was previously disabled.
95      */
96     public function testDisabledOutputInFailedExceptionDoesNotPopulateOutput()
97     {
98         $cmd = 'php';
99         $exitCode = 1;
100         $exitText = 'General error';
101         $workingDirectory = getcwd();
102
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));
107
108         $process->expects($this->never())
109             ->method('getOutput');
110
111         $process->expects($this->never())
112             ->method('getErrorOutput');
113
114         $process->expects($this->once())
115             ->method('getExitCode')
116             ->will($this->returnValue($exitCode));
117
118         $process->expects($this->once())
119             ->method('getExitCodeText')
120             ->will($this->returnValue($exitText));
121
122         $process->expects($this->once())
123             ->method('isOutputDisabled')
124             ->will($this->returnValue(true));
125
126         $process->expects($this->once())
127             ->method('getWorkingDirectory')
128             ->will($this->returnValue($workingDirectory));
129
130         $exception = new ProcessFailedException($process);
131
132         $this->assertEquals(
133             "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}",
134             $exception->getMessage()
135         );
136     }
137 }