Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Process] Fixed issue where $env or $_ENV can contain array values #8123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null
}
$statusCode = $e->getCode();

$statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1;
$statusCode = $statusCode ? (is_numeric($statusCode) ? (int) $statusCode : 1) : 0;
}

if ($this->autoExit) {
Expand Down Expand Up @@ -193,7 +193,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
$statusCode = $command->run($input, $output);
$this->runningCommand = null;

return is_numeric($statusCode) ? $statusCode : 0;
return $statusCode;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function run(InputInterface $input, OutputInterface $output)
$statusCode = $this->execute($input, $output);
}

return is_numeric($statusCode) ? $statusCode : 0;
return is_numeric($statusCode) ? (int) $statusCode : 0;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,21 @@ public function testRun()
$this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
}

public function testRunReturnsIntegerExitCode()
{
$exception = new \Exception('', 4);

$application = $this->getMock('Symfony\Component\Console\Application', array('doRun'));
$application->setAutoExit(false);
$application->expects($this->once())
->method('doRun')
->will($this->throwException($exception));

$exitCode = $application->run(new ArrayInput(array()), new NullOutput());

$this->assertSame(4, $exitCode, '->run() returns integer exit code extracted from raised exception');
}

/**
* @expectedException \LogicException
* @dataProvider getAddingAlreadySetDefinitionElementData
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ public function testRun()
}
}

public function testRunReturnsIntegerExitCode()
{
$command = new \TestCommand();
$exitCode = $command->run(new StringInput(''), new NullOutput());
$this->assertSame(0, $exitCode, '->run() returns integer exit code (treats null as 0)');

$command = $this->getMock('TestCommand', array('execute'));
$command->expects($this->once())
->method('execute')
->will($this->returnValue('2.3'));
$exitCode = $command->run(new StringInput(''), new NullOutput());
$this->assertSame(2, $exitCode, '->run() returns integer exit code (casts numeric to int)');
}

public function testRunReturnsAlwaysInteger()
{
$command = new \TestCommand();
Expand Down
43 changes: 23 additions & 20 deletions src/Symfony/Component/CssSelector/Node/FunctionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,43 +100,46 @@ protected function _xpath_nth_child($xpath, $expr, $last = false, $addNameTest =
$xpath->addStarPrefix();
if ($a == 0) {
if ($last) {
$b = sprintf('last() - %s', $b);
$b = sprintf('last() - %s', $b - 1);
}
$xpath->addCondition(sprintf('position() = %s', $b));

return $xpath;
}

if ($last) {
// FIXME: I'm not sure if this is right
$a = -$a;
$b = -$b;
}
if ($a < 0) {
if ($b < 1) {
$xpath->addCondition('false()');

if ($b > 0) {
$bNeg = -$b;
return $xpath;
}

$sign = '<=';
} else {
$bNeg = sprintf('+%s', -$b);
$sign = '>=';
}

if ($a != 1) {
$expr = array(sprintf('(position() %s) mod %s = 0', $bNeg, $a));
} else {
$expr = array();
$expr = 'position()';

if ($last) {
$expr = 'last() - '.$expr;
$b--;
}

if ($b >= 0) {
$expr[] = sprintf('position() >= %s', $b);
} elseif ($b < 0 && $last) {
$expr[] = sprintf('position() < (last() %s)', $b);
if (0 !== $b) {
$expr .= ' - '.$b;
}
$expr = implode($expr, ' and ');

$conditions = array(sprintf('%s %s 0', $expr, $sign));

if ($expr) {
$xpath->addCondition($expr);
if (1 !== $a && -1 !== $a) {
$conditions[] = sprintf('(%s) mod %d = 0', $expr, $a);
}

$xpath->addCondition(implode(' and ', $conditions));

return $xpath;

/* FIXME: handle an+b, odd, even
an+b means every-a, plus b, e.g., 2n+1 means odd
0n+b means b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getCssSelectors()
array('h1', "h1"),
array('foo|h1', "foo:h1"),
array('h1, h2, h3', "h1 | h2 | h3"),
array('h1:nth-child(3n+1)', "*/*[name() = 'h1' and ((position() -1) mod 3 = 0 and position() >= 1)]"),
array('h1:nth-child(3n+1)', "*/*[name() = 'h1' and (position() - 1 >= 0 and (position() - 1) mod 3 = 0)]"),
array('h1 > p', "h1/p"),
array('h1#foo', "h1[@id = 'foo']"),
array('h1.foo', "h1[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public function testToXpath()
// h1:nth-child(odd)
$element2 = new ElementNode('*', new Token('Symbol', 'odd', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and ((position() -1) mod 2 = 0 and position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() - 1 >= 0 and (position() - 1) mod 2 = 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-child(even)
$element2 = new ElementNode('*', new Token('Symbol', 'even', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod 2 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() >= 0 and (position()) mod 2 = 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-child(n)
$element2 = new ElementNode('*', new Token('Symbol', 'n', -1));
Expand All @@ -51,12 +51,12 @@ public function testToXpath()
// h1:nth-child(3n+1)
$element2 = new ElementNode('*', new Token('Symbol', '3n+1', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and ((position() -1) mod 3 = 0 and position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() - 1 >= 0 and (position() - 1) mod 3 = 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-child(n+1)
$element2 = new ElementNode('*', new Token('Symbol', 'n+1', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and (position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() - 1 >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-child(1)
$element2 = new ElementNode('*', new Token('Symbol', '2', -1));
Expand All @@ -66,24 +66,24 @@ public function testToXpath()
// h1:nth-child(2n)
$element2 = new ElementNode('*', new Token('Symbol', '2n', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod 2 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() >= 0 and (position()) mod 2 = 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-child(-n)
$element2 = new ElementNode('*', new Token('Symbol', '-n', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod -1 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (false())]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-last-child(2)
$function = new FunctionNode($element, ':', 'nth-last-child', 2);
$this->assertEquals("*/*[name() = 'h1' and (position() = last() - 2)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() = last() - 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-of-type(2)
$function = new FunctionNode($element, ':', 'nth-of-type', 2);
$this->assertEquals("*/h1[position() = 2]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-last-of-type(2)
$function = new FunctionNode($element, ':', 'nth-last-of-type', 2);
$this->assertEquals("*/h1[position() = last() - 2]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/h1[position() = last() - 1]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

/*
// h1:not(p)
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,9 @@ public function children()
throw new \InvalidArgumentException('The current node list is empty.');
}

return new static($this->sibling($this->getNode(0)->firstChild), $this->uri);
$node = $this->getNode(0)->firstChild;

return new static($node ? $this->sibling($node) : array(), $this->uri);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,14 @@ public function testChildren()
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->children() throws an \InvalidArgumentException if the node list is empty');
}

try {
$crawler = new Crawler('<p></p>');
$crawler->filter('p')->children();
$this->assertTrue(true, '->children() does not trigger a notice if the node has no children');
} catch (\PHPUnit_Framework_Error_Notice $e) {
$this->fail('->children() does not trigger a notice if the node has no children');
}
}

public function testParents()
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Process/ProcessBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ public function getProcess()
$env = $this->env;
}

// Process can not handle env values that are arrays
$env = $env ? array_filter($env, function ($value) { if (!is_array($value)) { return true; } }) : $env;

return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/Process/Tests/ProcessBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ public function testProcessShouldInheritAndOverrideEnvironmentVars()
$_ENV = $snapshot;
}

public function testProcessBuilderShouldNotPassEnvArrays()
{
$snapshot = $_ENV;
$_ENV = array('a' => array('b', 'c'), 'd' => 'e', 'f' => 'g');
$expected = array('d' => 'e', 'f' => 'g');

$pb = new ProcessBuilder();
$pb->add('a')->inheritEnvironmentVariables()
->setEnv('d', 'e');
$proc = $pb->getProcess();

$this->assertEquals($expected, $proc->getEnv(), '->inheritEnvironmentVariables() removes array values from $_ENV');

$_ENV = $snapshot;
}

public function testInheritEnvironmentVarsByDefault()
{
$pb = new ProcessBuilder();
Expand Down