Skip to content

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

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

Merged
merged 1 commit into from
Feb 28, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getValues()
public function setValues(array $values)
{
foreach ($values as $v) {
if (!$v instanceof Reference) {
if (!$v instanceof Reference && null !== $v) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nulls will create services that have "has()" return true, and "get()" return null. Not sure it's useful at all. What about skipping them instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self answer: bad idea: maybe the consumer expects a value from get for the provided key. :)
Test case missing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test case added

throw new InvalidArgumentException('Values of a ServiceLocatorArgument must be Reference objects.');
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}