Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #31541 [DI] fix using bindings with locators of service subscribe…
…rs (nicolas-grekas) This PR was merged into the 3.4 branch. Discussion ---------- [DI] fix using bindings with locators of service subscribers | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Spotted during the workshop in Sao Paulo, with @tsantos84, @andreia and others :) Commits ------- 7146b95 [DI] fix using bindings with locators of service subscribers
- Loading branch information
Showing
3 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?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\Compiler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Argument\BoundArgument; | ||
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition2; | ||
|
||
require_once __DIR__.'/../Fixtures/includes/classes.php'; | ||
|
||
class ServiceLocatorTagPassTest extends TestCase | ||
{ | ||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException | ||
* @expectedExceptionMessage Invalid definition for service "foo": an array of references is expected as first argument when the "container.service_locator" tag is set. | ||
*/ | ||
public function testNoServices() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->register('foo', ServiceLocator::class) | ||
->addTag('container.service_locator') | ||
; | ||
|
||
(new ServiceLocatorTagPass())->process($container); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException | ||
* @expectedExceptionMessage Invalid definition for service "foo": an array of references is expected as first argument when the "container.service_locator" tag is set, "string" found for key "0". | ||
*/ | ||
public function testInvalidServices() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->register('foo', ServiceLocator::class) | ||
->setArguments([[ | ||
'dummy', | ||
]]) | ||
->addTag('container.service_locator') | ||
; | ||
|
||
(new ServiceLocatorTagPass())->process($container); | ||
} | ||
|
||
public function testProcessValue() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->register('bar', CustomDefinition::class); | ||
$container->register('baz', CustomDefinition::class); | ||
|
||
$container->register('foo', ServiceLocator::class) | ||
->setArguments([[ | ||
new Reference('bar'), | ||
new Reference('baz'), | ||
'some.service' => new Reference('bar'), | ||
]]) | ||
->addTag('container.service_locator') | ||
; | ||
|
||
(new ServiceLocatorTagPass())->process($container); | ||
|
||
/** @var ServiceLocator $locator */ | ||
$locator = $container->get('foo'); | ||
|
||
$this->assertSame(CustomDefinition::class, \get_class($locator('bar'))); | ||
$this->assertSame(CustomDefinition::class, \get_class($locator('baz'))); | ||
$this->assertSame(CustomDefinition::class, \get_class($locator('some.service'))); | ||
} | ||
|
||
public function testServiceWithKeyOverwritesPreviousInheritedKey() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->register('bar', TestDefinition1::class); | ||
$container->register('baz', TestDefinition2::class); | ||
|
||
$container->register('foo', ServiceLocator::class) | ||
->setArguments([[ | ||
new Reference('bar'), | ||
'bar' => new Reference('baz'), | ||
]]) | ||
->addTag('container.service_locator') | ||
; | ||
|
||
(new ServiceLocatorTagPass())->process($container); | ||
|
||
/** @var ServiceLocator $locator */ | ||
$locator = $container->get('foo'); | ||
|
||
$this->assertSame(TestDefinition2::class, \get_class($locator('bar'))); | ||
} | ||
|
||
public function testInheritedKeyOverwritesPreviousServiceWithKey() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->register('bar', TestDefinition1::class); | ||
$container->register('baz', TestDefinition2::class); | ||
|
||
$container->register('foo', ServiceLocator::class) | ||
->setArguments([[ | ||
'bar' => new Reference('baz'), | ||
new Reference('bar'), | ||
]]) | ||
->addTag('container.service_locator') | ||
; | ||
|
||
(new ServiceLocatorTagPass())->process($container); | ||
|
||
/** @var ServiceLocator $locator */ | ||
$locator = $container->get('foo'); | ||
|
||
$this->assertSame(TestDefinition1::class, \get_class($locator('bar'))); | ||
} | ||
|
||
public function testBindingsAreCopied() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->register('foo') | ||
->setBindings(['foo' => 'foo']); | ||
|
||
$locator = ServiceLocatorTagPass::register($container, ['foo' => new Reference('foo')], 'foo'); | ||
$locator = $container->getDefinition($locator); | ||
$locator = $container->getDefinition($locator->getFactory()[0]); | ||
|
||
$this->assertSame(['foo'], array_keys($locator->getBindings())); | ||
$this->assertInstanceOf(BoundArgument::class, $locator->getBindings()['foo']); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Symfony/Component/DependencyInjection/Tests/Fixtures/TestDefinition2.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures; | ||
|
||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
class TestDefinition2 extends Definition | ||
{ | ||
} |