2011-12-16 14:41:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\Process\Tests;
|
2011-12-16 14:41:31 +00:00
|
|
|
|
|
|
|
use Symfony\Component\Process\ProcessBuilder;
|
|
|
|
|
|
|
|
class ProcessBuilderTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2013-05-06 10:50:13 +01:00
|
|
|
public function testInheritEnvironmentVars()
|
2011-12-16 14:41:31 +00:00
|
|
|
{
|
2013-08-11 20:20:34 +01:00
|
|
|
$_ENV['MY_VAR_1'] = 'foo';
|
2011-12-16 14:41:31 +00:00
|
|
|
|
2013-08-11 20:20:34 +01:00
|
|
|
$proc = ProcessBuilder::create()
|
|
|
|
->add('foo')
|
|
|
|
->getProcess();
|
2011-12-16 14:41:31 +00:00
|
|
|
|
2013-08-11 20:20:34 +01:00
|
|
|
unset($_ENV['MY_VAR_1']);
|
2011-12-16 14:41:31 +00:00
|
|
|
|
2013-08-11 20:20:34 +01:00
|
|
|
$env = $proc->getEnv();
|
|
|
|
$this->assertArrayHasKey('MY_VAR_1', $env);
|
|
|
|
$this->assertEquals('foo', $env['MY_VAR_1']);
|
2011-12-16 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 12:54:31 +01:00
|
|
|
public function testAddEnvironmentVariables()
|
|
|
|
{
|
|
|
|
$pb = new ProcessBuilder();
|
|
|
|
$env = array(
|
|
|
|
'foo' => 'bar',
|
|
|
|
'foo2' => 'bar2',
|
|
|
|
);
|
|
|
|
$proc = $pb
|
|
|
|
->add('command')
|
|
|
|
->setEnv('foo', 'bar2')
|
|
|
|
->addEnvironmentVariables($env)
|
2013-09-27 15:31:42 +01:00
|
|
|
->inheritEnvironmentVariables(false)
|
2013-06-04 12:54:31 +01:00
|
|
|
->getProcess()
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->assertSame($env, $proc->getEnv());
|
|
|
|
}
|
|
|
|
|
2013-05-06 10:50:13 +01:00
|
|
|
public function testProcessShouldInheritAndOverrideEnvironmentVars()
|
2012-02-22 13:10:14 +00:00
|
|
|
{
|
2013-08-11 20:20:34 +01:00
|
|
|
$_ENV['MY_VAR_1'] = 'foo';
|
2012-02-22 13:10:14 +00:00
|
|
|
|
2013-08-11 20:20:34 +01:00
|
|
|
$proc = ProcessBuilder::create()
|
|
|
|
->setEnv('MY_VAR_1', 'bar')
|
2011-12-16 14:41:31 +00:00
|
|
|
->add('foo')
|
2013-08-11 20:20:34 +01:00
|
|
|
->getProcess();
|
2011-12-16 14:41:31 +00:00
|
|
|
|
2013-08-11 20:20:34 +01:00
|
|
|
unset($_ENV['MY_VAR_1']);
|
2011-12-16 14:41:31 +00:00
|
|
|
|
2013-08-11 20:20:34 +01:00
|
|
|
$env = $proc->getEnv();
|
|
|
|
$this->assertArrayHasKey('MY_VAR_1', $env);
|
|
|
|
$this->assertEquals('bar', $env['MY_VAR_1']);
|
2011-12-16 14:41:31 +00:00
|
|
|
}
|
2012-07-10 14:21:23 +01:00
|
|
|
|
|
|
|
/**
|
2013-01-05 17:52:40 +00:00
|
|
|
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
|
2012-07-10 14:21:23 +01:00
|
|
|
*/
|
|
|
|
public function testNegativeTimeoutFromSetter()
|
|
|
|
{
|
|
|
|
$pb = new ProcessBuilder();
|
|
|
|
$pb->setTimeout(-1);
|
|
|
|
}
|
2012-07-11 13:21:05 +01:00
|
|
|
|
|
|
|
public function testNullTimeout()
|
|
|
|
{
|
|
|
|
$pb = new ProcessBuilder();
|
|
|
|
$pb->setTimeout(10);
|
|
|
|
$pb->setTimeout(null);
|
|
|
|
|
|
|
|
$r = new \ReflectionObject($pb);
|
|
|
|
$p = $r->getProperty('timeout');
|
|
|
|
$p->setAccessible(true);
|
|
|
|
|
|
|
|
$this->assertNull($p->getValue($pb));
|
|
|
|
}
|
2012-10-28 15:19:50 +00:00
|
|
|
|
|
|
|
public function testShouldSetArguments()
|
|
|
|
{
|
|
|
|
$pb = new ProcessBuilder(array('initial'));
|
|
|
|
$pb->setArguments(array('second'));
|
|
|
|
|
|
|
|
$proc = $pb->getProcess();
|
|
|
|
|
|
|
|
$this->assertContains("second", $proc->getCommandLine());
|
|
|
|
}
|
2013-04-06 20:28:54 +01:00
|
|
|
|
|
|
|
public function testPrefixIsPrependedToAllGeneratedProcess()
|
|
|
|
{
|
|
|
|
$pb = new ProcessBuilder();
|
|
|
|
$pb->setPrefix('/usr/bin/php');
|
|
|
|
|
|
|
|
$proc = $pb->setArguments(array('-v'))->getProcess();
|
2013-04-25 14:23:11 +01:00
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
|
|
|
$this->assertEquals('"/usr/bin/php" "-v"', $proc->getCommandLine());
|
|
|
|
} else {
|
|
|
|
$this->assertEquals("'/usr/bin/php' '-v'", $proc->getCommandLine());
|
|
|
|
}
|
2013-04-06 20:28:54 +01:00
|
|
|
|
|
|
|
$proc = $pb->setArguments(array('-i'))->getProcess();
|
2013-04-25 14:23:11 +01:00
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
|
|
|
$this->assertEquals('"/usr/bin/php" "-i"', $proc->getCommandLine());
|
|
|
|
} else {
|
|
|
|
$this->assertEquals("'/usr/bin/php' '-i'", $proc->getCommandLine());
|
|
|
|
}
|
2013-04-06 20:28:54 +01:00
|
|
|
}
|
2013-04-21 09:43:42 +01:00
|
|
|
|
2013-06-03 02:02:46 +01:00
|
|
|
public function testArrayPrefixesArePrependedToAllGeneratedProcess()
|
|
|
|
{
|
|
|
|
$pb = new ProcessBuilder();
|
|
|
|
$pb->setPrefix(array('/usr/bin/php', 'composer.phar'));
|
|
|
|
|
|
|
|
$proc = $pb->setArguments(array('-v'))->getProcess();
|
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
|
|
|
$this->assertEquals('"/usr/bin/php" "composer.phar" "-v"', $proc->getCommandLine());
|
|
|
|
} else {
|
|
|
|
$this->assertEquals("'/usr/bin/php' 'composer.phar' '-v'", $proc->getCommandLine());
|
|
|
|
}
|
|
|
|
|
|
|
|
$proc = $pb->setArguments(array('-i'))->getProcess();
|
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
|
|
|
$this->assertEquals('"/usr/bin/php" "composer.phar" "-i"', $proc->getCommandLine());
|
|
|
|
} else {
|
|
|
|
$this->assertEquals("'/usr/bin/php' 'composer.phar' '-i'", $proc->getCommandLine());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-18 12:53:35 +00:00
|
|
|
public function testShouldEscapeArguments()
|
|
|
|
{
|
2014-03-18 13:37:22 +00:00
|
|
|
$pb = new ProcessBuilder(array('%path%', 'foo " bar', '%baz%baz'));
|
2013-01-18 12:53:35 +00:00
|
|
|
$proc = $pb->getProcess();
|
|
|
|
|
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
2014-03-18 13:37:22 +00:00
|
|
|
$this->assertSame('^%"path"^% "foo \\" bar" "%baz%baz"', $proc->getCommandLine());
|
2013-01-18 12:53:35 +00:00
|
|
|
} else {
|
2014-03-18 13:37:22 +00:00
|
|
|
$this->assertSame("'%path%' 'foo \" bar' '%baz%baz'", $proc->getCommandLine());
|
2013-01-18 12:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-23 21:48:37 +01:00
|
|
|
|
|
|
|
public function testShouldEscapeArgumentsAndPrefix()
|
|
|
|
{
|
|
|
|
$pb = new ProcessBuilder(array('arg'));
|
|
|
|
$pb->setPrefix('%prefix%');
|
|
|
|
$proc = $pb->getProcess();
|
|
|
|
|
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
|
|
|
$this->assertSame('^%"prefix"^% "arg"', $proc->getCommandLine());
|
|
|
|
} else {
|
|
|
|
$this->assertSame("'%prefix%' 'arg'", $proc->getCommandLine());
|
|
|
|
}
|
|
|
|
}
|
2013-04-23 21:53:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Process\Exception\LogicException
|
|
|
|
*/
|
|
|
|
public function testShouldThrowALogicExceptionIfNoPrefixAndNoArgument()
|
|
|
|
{
|
|
|
|
ProcessBuilder::create()->getProcess();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShouldNotThrowALogicExceptionIfNoArgument()
|
|
|
|
{
|
|
|
|
$process = ProcessBuilder::create()
|
|
|
|
->setPrefix('/usr/bin/php')
|
|
|
|
->getProcess();
|
|
|
|
|
2013-04-25 14:23:11 +01:00
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
|
|
|
$this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
|
|
|
|
} else {
|
|
|
|
$this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
|
|
|
|
}
|
2013-04-23 21:53:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShouldNotThrowALogicExceptionIfNoPrefix()
|
|
|
|
{
|
|
|
|
$process = ProcessBuilder::create(array('/usr/bin/php'))
|
|
|
|
->getProcess();
|
|
|
|
|
2013-04-25 14:23:11 +01:00
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
|
|
|
$this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
|
|
|
|
} else {
|
|
|
|
$this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
|
|
|
|
}
|
2013-04-23 21:53:10 +01:00
|
|
|
}
|
2014-05-17 18:27:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
|
|
|
|
* @expectedExceptionMessage Symfony\Component\Process\ProcessBuilder::setInput only accepts strings.
|
|
|
|
*/
|
|
|
|
public function testInvalidInput()
|
|
|
|
{
|
|
|
|
$builder = ProcessBuilder::create();
|
|
|
|
$builder->setInput(array());
|
|
|
|
}
|
2011-12-16 14:41:31 +00:00
|
|
|
}
|