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

This commit is contained in:
Robin Chalas 2017-02-28 09:42:28 +01:00
parent 924c1f06bf
commit 256b836482
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());
}
}