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\ProcessBuilder;
20 class ProcessBuilderTest extends TestCase
22 public function testInheritEnvironmentVars()
24 $proc = ProcessBuilder::create()
28 $this->assertTrue($proc->areEnvironmentVariablesInherited());
30 $proc = ProcessBuilder::create()
32 ->inheritEnvironmentVariables(false)
35 $this->assertFalse($proc->areEnvironmentVariablesInherited());
38 public function testAddEnvironmentVariables()
40 $pb = new ProcessBuilder();
47 ->setEnv('foo', 'bar2')
48 ->addEnvironmentVariables($env)
52 $this->assertSame($env, $proc->getEnv());
56 * @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
58 public function testNegativeTimeoutFromSetter()
60 $pb = new ProcessBuilder();
64 public function testNullTimeout()
66 $pb = new ProcessBuilder();
68 $pb->setTimeout(null);
70 $r = new \ReflectionObject($pb);
71 $p = $r->getProperty('timeout');
72 $p->setAccessible(true);
74 $this->assertNull($p->getValue($pb));
77 public function testShouldSetArguments()
79 $pb = new ProcessBuilder(array('initial'));
80 $pb->setArguments(array('second'));
82 $proc = $pb->getProcess();
84 $this->assertContains('second', $proc->getCommandLine());
87 public function testPrefixIsPrependedToAllGeneratedProcess()
89 $pb = new ProcessBuilder();
90 $pb->setPrefix('/usr/bin/php');
92 $proc = $pb->setArguments(array('-v'))->getProcess();
93 if ('\\' === DIRECTORY_SEPARATOR) {
94 $this->assertEquals('"/usr/bin/php" -v', $proc->getCommandLine());
96 $this->assertEquals("'/usr/bin/php' '-v'", $proc->getCommandLine());
99 $proc = $pb->setArguments(array('-i'))->getProcess();
100 if ('\\' === DIRECTORY_SEPARATOR) {
101 $this->assertEquals('"/usr/bin/php" -i', $proc->getCommandLine());
103 $this->assertEquals("'/usr/bin/php' '-i'", $proc->getCommandLine());
107 public function testArrayPrefixesArePrependedToAllGeneratedProcess()
109 $pb = new ProcessBuilder();
110 $pb->setPrefix(array('/usr/bin/php', 'composer.phar'));
112 $proc = $pb->setArguments(array('-v'))->getProcess();
113 if ('\\' === DIRECTORY_SEPARATOR) {
114 $this->assertEquals('"/usr/bin/php" composer.phar -v', $proc->getCommandLine());
116 $this->assertEquals("'/usr/bin/php' 'composer.phar' '-v'", $proc->getCommandLine());
119 $proc = $pb->setArguments(array('-i'))->getProcess();
120 if ('\\' === DIRECTORY_SEPARATOR) {
121 $this->assertEquals('"/usr/bin/php" composer.phar -i', $proc->getCommandLine());
123 $this->assertEquals("'/usr/bin/php' 'composer.phar' '-i'", $proc->getCommandLine());
127 public function testShouldEscapeArguments()
129 $pb = new ProcessBuilder(array('%path%', 'foo " bar', '%baz%baz'));
130 $proc = $pb->getProcess();
132 if ('\\' === DIRECTORY_SEPARATOR) {
133 $this->assertSame('""^%"path"^%"" "foo "" bar" ""^%"baz"^%"baz"', $proc->getCommandLine());
135 $this->assertSame("'%path%' 'foo \" bar' '%baz%baz'", $proc->getCommandLine());
139 public function testShouldEscapeArgumentsAndPrefix()
141 $pb = new ProcessBuilder(array('arg'));
142 $pb->setPrefix('%prefix%');
143 $proc = $pb->getProcess();
145 if ('\\' === DIRECTORY_SEPARATOR) {
146 $this->assertSame('""^%"prefix"^%"" arg', $proc->getCommandLine());
148 $this->assertSame("'%prefix%' 'arg'", $proc->getCommandLine());
153 * @expectedException \Symfony\Component\Process\Exception\LogicException
155 public function testShouldThrowALogicExceptionIfNoPrefixAndNoArgument()
157 ProcessBuilder::create()->getProcess();
160 public function testShouldNotThrowALogicExceptionIfNoArgument()
162 $process = ProcessBuilder::create()
163 ->setPrefix('/usr/bin/php')
166 if ('\\' === DIRECTORY_SEPARATOR) {
167 $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
169 $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
173 public function testShouldNotThrowALogicExceptionIfNoPrefix()
175 $process = ProcessBuilder::create(array('/usr/bin/php'))
178 if ('\\' === DIRECTORY_SEPARATOR) {
179 $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
181 $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
185 public function testShouldReturnProcessWithDisabledOutput()
187 $process = ProcessBuilder::create(array('/usr/bin/php'))
191 $this->assertTrue($process->isOutputDisabled());
194 public function testShouldReturnProcessWithEnabledOutput()
196 $process = ProcessBuilder::create(array('/usr/bin/php'))
201 $this->assertFalse($process->isOutputDisabled());
205 * @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
206 * @expectedExceptionMessage Symfony\Component\Process\ProcessBuilder::setInput only accepts strings, Traversable objects or stream resources.
208 public function testInvalidInput()
210 $builder = ProcessBuilder::create();
211 $builder->setInput(array());
214 public function testDoesNotPrefixExec()
216 if ('\\' === DIRECTORY_SEPARATOR) {
217 $this->markTestSkipped('This test cannot run on Windows.');
220 $builder = ProcessBuilder::create(array('command', '-v', 'ls'));
221 $process = $builder->getProcess();
224 $this->assertTrue($process->isSuccessful());