Skip to content

Commit dc2edaf

Browse files
Merge branch '3.4' into 4.2
* 3.4: Skip testing the phpunit-bridge on not-master branches when $deps is empty more tests [DI] Fixes: symfony#28326 - Overriding services autowired by name under _defaults bind not working
2 parents 8297a75 + 39f2084 commit dc2edaf

13 files changed

+124
-0
lines changed

.appveyor.yml

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ test_script:
5454
- SET X=0
5555
- SET SYMFONY_PHPUNIT_SKIPPED_TESTS=phpunit.skipped
5656
- copy /Y c:\php\php.ini-min c:\php\php.ini
57+
- IF %APPVEYOR_REPO_BRANCH% neq master (rm -Rf src\Symfony\Bridge\PhpUnit)
5758
- php phpunit src\Symfony --exclude-group benchmark,intl-data || SET X=!errorlevel!
5859
- copy /Y c:\php\php.ini-max c:\php\php.ini
5960
- php phpunit src\Symfony --exclude-group benchmark,intl-data || SET X=!errorlevel!

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ install:
197197
SYMFONY_VERSION=$(cat composer.json | grep '^ *"dev-master". *"[1-9]' | grep -o '[0-9.]*')
198198
fi
199199
200+
- |
201+
# Skip the phpunit-bridge on not-master branches when $deps is empty
202+
if [[ ! $deps && $TRAVIS_BRANCH != master ]]; then
203+
COMPONENTS=$(find src/Symfony -mindepth 3 -type f -name phpunit.xml.dist -not -wholename '*/Bridge/PhpUnit/*' -printf '%h\n')
204+
fi
205+
200206
- |
201207
# Install symfony/flex
202208
if [[ $deps = low ]]; then

src/Symfony/Component/DependencyInjection/Compiler/ResolveBindingsPass.php

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class ResolveBindingsPass extends AbstractRecursivePass
3434
*/
3535
public function process(ContainerBuilder $container)
3636
{
37+
$this->usedBindings = $container->getRemovedBindingIds();
38+
3739
try {
3840
parent::process($container);
3941

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

+31
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
124124

125125
private $removedIds = [];
126126

127+
private $removedBindingIds = [];
128+
127129
private static $internalTypes = [
128130
'int' => true,
129131
'float' => true,
@@ -1460,6 +1462,35 @@ public function log(CompilerPassInterface $pass, string $message)
14601462
$this->getCompiler()->log($pass, $this->resolveEnvPlaceholders($message));
14611463
}
14621464

1465+
/**
1466+
* Gets removed binding ids.
1467+
*
1468+
* @return array
1469+
*
1470+
* @internal
1471+
*/
1472+
public function getRemovedBindingIds()
1473+
{
1474+
return $this->removedBindingIds;
1475+
}
1476+
1477+
/**
1478+
* Adds a removed binding id.
1479+
*
1480+
* @param int $id
1481+
*
1482+
* @internal
1483+
*/
1484+
public function addRemovedBindingIds($id)
1485+
{
1486+
if ($this->hasDefinition($id)) {
1487+
foreach ($this->getDefinition($id)->getBindings() as $key => $binding) {
1488+
list(, $bindingId) = $binding->getValues();
1489+
$this->removedBindingIds[(int) $bindingId] = true;
1490+
}
1491+
}
1492+
}
1493+
14631494
/**
14641495
* Returns the Service Conditionals.
14651496
*

src/Symfony/Component/DependencyInjection/Loader/Configurator/ServiceConfigurator.php

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public function __destruct()
5959
{
6060
parent::__destruct();
6161

62+
$this->container->addRemovedBindingIds($this->id);
63+
6264
if (!$this->definition instanceof ChildDefinition) {
6365
$this->container->setDefinition($this->id, $this->definition->setInstanceofConditionals($this->instanceof));
6466
} else {

src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e
9191
*/
9292
protected function setDefinition($id, Definition $definition)
9393
{
94+
$this->container->addRemovedBindingIds($id);
95+
9496
if ($this->isLoadingInstanceof) {
9597
if (!$definition instanceof ChildDefinition) {
9698
throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, \get_class($definition)));

src/Symfony/Component/DependencyInjection/Tests/Fixtures/Bar.php

+3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313

1414
class Bar implements BarInterface
1515
{
16+
public $quz;
17+
1618
public function __construct($quz = null, \NonExistent $nonExistent = null, BarInterface $decorated = null, array $foo = [])
1719
{
20+
$this->quz = $quz;
1821
}
1922

2023
public static function create(\NonExistent $nonExistent = null, $factory = null)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<defaults>
5+
<bind key="$quz">value</bind>
6+
<bind key="$foo" type="collection">
7+
<bind>value</bind>
8+
</bind>
9+
</defaults>
10+
11+
<service id="bar" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar"/>
12+
13+
<service id="foo" class="stdClass"/>
14+
</services>
15+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<defaults>
5+
<bind key="$quz">overridden</bind>
6+
</defaults>
7+
8+
<service id="bar" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar"/>
9+
</services>
10+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
_defaults:
3+
bind:
4+
$quz: value
5+
$foo: [value]
6+
7+
bar:
8+
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Bar
9+
10+
foo:
11+
class: stdClass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
_defaults:
3+
bind:
4+
$quz: overridden
5+
6+
bar:
7+
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Bar

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Config\Resource\GlobResource;
1919
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
2020
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
21+
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
2122
use Symfony\Component\DependencyInjection\ContainerBuilder;
2223
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
2324
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
@@ -863,4 +864,20 @@ public function testTsantosContainer()
863864
$dump = $dumper->dump();
864865
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_tsantos.php', $dumper->dump());
865866
}
867+
868+
/**
869+
* The pass may throw an exception, which will cause the test to fail.
870+
*/
871+
public function testOverriddenDefaultsBindings()
872+
{
873+
$container = new ContainerBuilder();
874+
875+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
876+
$loader->load('defaults_bindings.xml');
877+
$loader->load('defaults_bindings2.xml');
878+
879+
(new ResolveBindingsPass())->process($container);
880+
881+
$this->assertSame('overridden', $container->get('bar')->quz);
882+
}
866883
}

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Config\Resource\GlobResource;
1919
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
2020
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
21+
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
2122
use Symfony\Component\DependencyInjection\ContainerBuilder;
2223
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
2324
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
@@ -781,4 +782,20 @@ public function testServiceWithSameNameAsInterfaceAndFactoryIsNotTagged()
781782
$tagged = $container->findTaggedServiceIds('bar');
782783
$this->assertCount(1, $tagged);
783784
}
785+
786+
/**
787+
* The pass may throw an exception, which will cause the test to fail.
788+
*/
789+
public function testOverriddenDefaultsBindings()
790+
{
791+
$container = new ContainerBuilder();
792+
793+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
794+
$loader->load('defaults_bindings.yml');
795+
$loader->load('defaults_bindings2.yml');
796+
797+
(new ResolveBindingsPass())->process($container);
798+
799+
$this->assertSame('overridden', $container->get('bar')->quz);
800+
}
784801
}

0 commit comments

Comments
 (0)