merged branch romainneutron/process-builder (PR #7821)

This PR was merged into the master branch.

Discussion
----------

[Process] Fix two process-builder minor bugs

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

Everything is in the commit messages :

 - Use new ProcessUtils::escapeArgument to escape ProcessBuillder prefix
 - Do not throw LogicException in ProcessBuilder::getProcess if no arguments are set but a prefix is

Commits
-------

1de29b9 [Process] Do not throw LogicException in ProcessBuilder::getProcess if no arguments are set but a prefix is
209799b [Process] Use new ProcessUtils::escapeArgument to escape ProcessBuilder prefix
This commit is contained in:
Fabien Potencier 2013-04-24 10:37:01 +02:00
commit ae88c02236
2 changed files with 41 additions and 3 deletions

View File

@ -154,14 +154,14 @@ class ProcessBuilder
public function getProcess()
{
if (!count($this->arguments)) {
if (!$this->prefix && !count($this->arguments)) {
throw new LogicException('You must add() command arguments before calling getProcess().');
}
$options = $this->options;
$script = ($this->prefix ? escapeshellarg($this->prefix) . ' ' : '')
.implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $this->arguments));
$arguments = $this->prefix ? array_merge(array($this->prefix), $this->arguments) : $this->arguments;
$script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
if ($this->inheritEnv) {
$env = $this->env ? $this->env + $_ENV : null;

View File

@ -139,4 +139,42 @@ class ProcessBuilderTest extends \PHPUnit_Framework_TestCase
$this->assertSame("'%path%' 'foo \" bar'", $proc->getCommandLine());
}
}
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());
}
}
/**
* @expectedException \Symfony\Component\Process\Exception\LogicException
*/
public function testShouldThrowALogicExceptionIfNoPrefixAndNoArgument()
{
ProcessBuilder::create()->getProcess();
}
public function testShouldNotThrowALogicExceptionIfNoArgument()
{
$process = ProcessBuilder::create()
->setPrefix('/usr/bin/php')
->getProcess();
$this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
}
public function testShouldNotThrowALogicExceptionIfNoPrefix()
{
$process = ProcessBuilder::create(array('/usr/bin/php'))
->getProcess();
$this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
}
}