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
12namespace Symfony\Component\Process\Tests;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\Process\Exception\ProcessFailedException;
16
17/**
18 * @author Sebastian Marek <proofek@gmail.com>
19 */
20class 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(['isSuccessful'])->setConstructorArgs(['php'])->getMock();
28        $process->expects($this->once())
29            ->method('isSuccessful')
30            ->willReturn(true);
31
32        $this->expectException(\InvalidArgumentException::class);
33        $this->expectExceptionMessage('Expected a failed process, but the given process was successful.');
34
35        new ProcessFailedException($process);
36    }
37
38    /**
39     * tests ProcessFailedException uses information from process output
40     * to generate exception message.
41     */
42    public function testProcessFailedExceptionPopulatesInformationFromProcessOutput()
43    {
44        $cmd = 'php';
45        $exitCode = 1;
46        $exitText = 'General error';
47        $output = 'Command output';
48        $errorOutput = 'FATAL: Unexpected error';
49        $workingDirectory = getcwd();
50
51        $process = $this->getMockBuilder('Symfony\Component\Process\Process')->setMethods(['isSuccessful', 'getOutput', 'getErrorOutput', 'getExitCode', 'getExitCodeText', 'isOutputDisabled', 'getWorkingDirectory'])->setConstructorArgs([$cmd])->getMock();
52        $process->expects($this->once())
53            ->method('isSuccessful')
54            ->willReturn(false);
55
56        $process->expects($this->once())
57            ->method('getOutput')
58            ->willReturn($output);
59
60        $process->expects($this->once())
61            ->method('getErrorOutput')
62            ->willReturn($errorOutput);
63
64        $process->expects($this->once())
65            ->method('getExitCode')
66            ->willReturn($exitCode);
67
68        $process->expects($this->once())
69            ->method('getExitCodeText')
70            ->willReturn($exitText);
71
72        $process->expects($this->once())
73            ->method('isOutputDisabled')
74            ->willReturn(false);
75
76        $process->expects($this->once())
77            ->method('getWorkingDirectory')
78            ->willReturn($workingDirectory);
79
80        $exception = new ProcessFailedException($process);
81
82        $this->assertEquals(
83            "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}\n\nOutput:\n================\n{$output}\n\nError Output:\n================\n{$errorOutput}",
84            $exception->getMessage()
85        );
86    }
87
88    /**
89     * Tests that ProcessFailedException does not extract information from
90     * process output if it was previously disabled.
91     */
92    public function testDisabledOutputInFailedExceptionDoesNotPopulateOutput()
93    {
94        $cmd = 'php';
95        $exitCode = 1;
96        $exitText = 'General error';
97        $workingDirectory = getcwd();
98
99        $process = $this->getMockBuilder('Symfony\Component\Process\Process')->setMethods(['isSuccessful', 'isOutputDisabled', 'getExitCode', 'getExitCodeText', 'getOutput', 'getErrorOutput', 'getWorkingDirectory'])->setConstructorArgs([$cmd])->getMock();
100        $process->expects($this->once())
101            ->method('isSuccessful')
102            ->willReturn(false);
103
104        $process->expects($this->never())
105            ->method('getOutput');
106
107        $process->expects($this->never())
108            ->method('getErrorOutput');
109
110        $process->expects($this->once())
111            ->method('getExitCode')
112            ->willReturn($exitCode);
113
114        $process->expects($this->once())
115            ->method('getExitCodeText')
116            ->willReturn($exitText);
117
118        $process->expects($this->once())
119            ->method('isOutputDisabled')
120            ->willReturn(true);
121
122        $process->expects($this->once())
123            ->method('getWorkingDirectory')
124            ->willReturn($workingDirectory);
125
126        $exception = new ProcessFailedException($process);
127
128        $this->assertEquals(
129            "The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}",
130            $exception->getMessage()
131        );
132    }
133}
134