bug #21794 [DI] Fix ServiceLocatorArgument::setValues() for non-reference values (chalasr)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Fix ServiceLocatorArgument::setValues() for non-reference values

| Q             | A
| ------------- | ---
| Branch?       | master
| Fixed tickets | https://github.com/symfony/symfony/pull/21625#issuecomment-282938336
| Tests pass?   | yes
| License       | MIT

`ResolveInvalidReferencesPass` [calls `setValues()`](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/ResolveInvalidReferencesPass.php#L91) with resolved invalid reference (null), the `Reference` type check should occurs at construction only.

Commits
-------

256b836 [DI] Fix ServiceLocatorArgument::setValues() for non-reference values
This commit is contained in:
Nicolas Grekas 2017-02-28 13:08:10 +01:00
commit 7f012b685a
2 changed files with 36 additions and 1 deletions

View File

@ -39,7 +39,7 @@ class ServiceLocatorArgument implements ArgumentInterface
public function setValues(array $values)
{
foreach ($values as $v) {
if (!$v instanceof Reference) {
if (!$v instanceof Reference && null !== $v) {
throw new InvalidArgumentException('Values of a ServiceLocatorArgument must be Reference objects.');
}
}

View File

@ -0,0 +1,35 @@
<?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.
*/
namespace Symfony\Component\DependencyInjection\Tests\Argument;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Reference;
class ServiceLocatorArgumentTest extends TestCase
{
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Values of a ServiceLocatorArgument must be Reference objects.
*/
public function testThrowsOnNonReferenceValues()
{
new ServiceLocatorArgument(array('foo' => 'bar'));
}
public function testAcceptsReferencesOrNulls()
{
$locator = new ServiceLocatorArgument($values = array('foo' => new Reference('bar'), 'bar' => null));
$this->assertSame($values, $locator->getValues());
}
}