Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/internal/Magento/Framework/Shell/CommandRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class CommandRenderer implements CommandRendererInterface
*/
public function render($command, array $arguments = [])
{
$command = preg_replace('/(\s+2>&1)*(\s*\|)|$/', ' 2>&1$2', $command);
$arguments = array_map('escapeshellarg', $arguments);
$command = preg_replace('/\s?\||$/', ' 2>&1$0', $command);
$command = vsprintf($command, $arguments);
return $command;
if (empty($arguments)) {
return $command;
}
return vsprintf($command, $arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,40 @@
*/
namespace Magento\Framework\Shell\Test\Unit;

use \Magento\Framework\Shell\CommandRenderer;
use Magento\Framework\Shell\CommandRenderer;

class CommandRendererTest extends \PHPUnit_Framework_TestCase
{
public function testRender()
/**
* @param $expectedCommand
* @param $actualCommand
* @param $testArguments
* @dataProvider commandsDataProvider
*/
public function testRender($expectedCommand, $actualCommand, $testArguments)
{
$testArgument = 'argument';
$testArgument2 = 'argument2';
$commandRenderer = new CommandRenderer();
$this->assertEquals(
"php -r " . escapeshellarg($testArgument) . " 2>&1 | grep " . escapeshellarg($testArgument2) . " 2>&1",
$commandRenderer->render('php -r %s | grep %s', [$testArgument, $testArgument2])
$expectedCommand,
$commandRenderer->render($actualCommand, $testArguments)
);
}

public function commandsDataProvider()
{
$testArgument = 'argument';
$testArgument2 = 'argument2';

$expectedCommand = "php -r %s 2>&1 | grep %s 2>&1";
$expectedCommandArgs = "php -r '" . $testArgument . "' 2>&1 | grep '" . $testArgument2 . "' 2>&1";

return [
[$expectedCommand, 'php -r %s | grep %s', []],
[$expectedCommand, 'php -r %s 2>&1 | grep %s', []],
[$expectedCommand, 'php -r %s 2>&1 2>&1 | grep %s', []],
[$expectedCommandArgs, 'php -r %s | grep %s', [$testArgument, $testArgument2]],
[$expectedCommandArgs, 'php -r %s 2>&1 | grep %s', [$testArgument, $testArgument2]],
[$expectedCommandArgs, 'php -r %s 2>&1 2>&1 | grep %s', [$testArgument, $testArgument2]],
];
}
}