Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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: #28326 - Overriding services autowired by name under _defaults bind not working
  • Loading branch information
nicolas-grekas committed Apr 12, 2019
2 parents 8297a75 + 39f2084 commit dc2edaf
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions .appveyor.yml
Expand Up @@ -54,6 +54,7 @@ test_script:
- SET X=0 - SET X=0
- SET SYMFONY_PHPUNIT_SKIPPED_TESTS=phpunit.skipped - SET SYMFONY_PHPUNIT_SKIPPED_TESTS=phpunit.skipped
- copy /Y c:\php\php.ini-min c:\php\php.ini - copy /Y c:\php\php.ini-min c:\php\php.ini
- IF %APPVEYOR_REPO_BRANCH% neq master (rm -Rf src\Symfony\Bridge\PhpUnit)
- php phpunit src\Symfony --exclude-group benchmark,intl-data || SET X=!errorlevel! - php phpunit src\Symfony --exclude-group benchmark,intl-data || SET X=!errorlevel!
- copy /Y c:\php\php.ini-max c:\php\php.ini - copy /Y c:\php\php.ini-max c:\php\php.ini
- php phpunit src\Symfony --exclude-group benchmark,intl-data || SET X=!errorlevel! - php phpunit src\Symfony --exclude-group benchmark,intl-data || SET X=!errorlevel!
Expand Down
6 changes: 6 additions & 0 deletions .travis.yml
Expand Up @@ -197,6 +197,12 @@ install:
SYMFONY_VERSION=$(cat composer.json | grep '^ *"dev-master". *"[1-9]' | grep -o '[0-9.]*') SYMFONY_VERSION=$(cat composer.json | grep '^ *"dev-master". *"[1-9]' | grep -o '[0-9.]*')
fi fi
- |
# Skip the phpunit-bridge on not-master branches when $deps is empty
if [[ ! $deps && $TRAVIS_BRANCH != master ]]; then
COMPONENTS=$(find src/Symfony -mindepth 3 -type f -name phpunit.xml.dist -not -wholename '*/Bridge/PhpUnit/*' -printf '%h\n')
fi
- | - |
# Install symfony/flex # Install symfony/flex
if [[ $deps = low ]]; then if [[ $deps = low ]]; then
Expand Down
Expand Up @@ -34,6 +34,8 @@ class ResolveBindingsPass extends AbstractRecursivePass
*/ */
public function process(ContainerBuilder $container) public function process(ContainerBuilder $container)
{ {
$this->usedBindings = $container->getRemovedBindingIds();

try { try {
parent::process($container); parent::process($container);


Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Expand Up @@ -124,6 +124,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface


private $removedIds = []; private $removedIds = [];


private $removedBindingIds = [];

private static $internalTypes = [ private static $internalTypes = [
'int' => true, 'int' => true,
'float' => true, 'float' => true,
Expand Down Expand Up @@ -1460,6 +1462,35 @@ public function log(CompilerPassInterface $pass, string $message)
$this->getCompiler()->log($pass, $this->resolveEnvPlaceholders($message)); $this->getCompiler()->log($pass, $this->resolveEnvPlaceholders($message));
} }


/**
* Gets removed binding ids.
*
* @return array
*
* @internal
*/
public function getRemovedBindingIds()
{
return $this->removedBindingIds;
}

/**
* Adds a removed binding id.
*
* @param int $id
*
* @internal
*/
public function addRemovedBindingIds($id)
{
if ($this->hasDefinition($id)) {
foreach ($this->getDefinition($id)->getBindings() as $key => $binding) {
list(, $bindingId) = $binding->getValues();
$this->removedBindingIds[(int) $bindingId] = true;
}
}
}

/** /**
* Returns the Service Conditionals. * Returns the Service Conditionals.
* *
Expand Down
Expand Up @@ -59,6 +59,8 @@ public function __destruct()
{ {
parent::__destruct(); parent::__destruct();


$this->container->addRemovedBindingIds($this->id);

if (!$this->definition instanceof ChildDefinition) { if (!$this->definition instanceof ChildDefinition) {
$this->container->setDefinition($this->id, $this->definition->setInstanceofConditionals($this->instanceof)); $this->container->setDefinition($this->id, $this->definition->setInstanceofConditionals($this->instanceof));
} else { } else {
Expand Down
Expand Up @@ -91,6 +91,8 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e
*/ */
protected function setDefinition($id, Definition $definition) protected function setDefinition($id, Definition $definition)
{ {
$this->container->addRemovedBindingIds($id);

if ($this->isLoadingInstanceof) { if ($this->isLoadingInstanceof) {
if (!$definition instanceof ChildDefinition) { if (!$definition instanceof ChildDefinition) {
throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, \get_class($definition))); throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, \get_class($definition)));
Expand Down
Expand Up @@ -13,8 +13,11 @@


class Bar implements BarInterface class Bar implements BarInterface
{ {
public $quz;

public function __construct($quz = null, \NonExistent $nonExistent = null, BarInterface $decorated = null, array $foo = []) public function __construct($quz = null, \NonExistent $nonExistent = null, BarInterface $decorated = null, array $foo = [])
{ {
$this->quz = $quz;
} }


public static function create(\NonExistent $nonExistent = null, $factory = null) public static function create(\NonExistent $nonExistent = null, $factory = null)
Expand Down
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<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">
<services>
<defaults>
<bind key="$quz">value</bind>
<bind key="$foo" type="collection">
<bind>value</bind>
</bind>
</defaults>

<service id="bar" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar"/>

<service id="foo" class="stdClass"/>
</services>
</container>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<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">
<services>
<defaults>
<bind key="$quz">overridden</bind>
</defaults>

<service id="bar" class="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar"/>
</services>
</container>
@@ -0,0 +1,11 @@
services:
_defaults:
bind:
$quz: value
$foo: [value]

bar:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Bar

foo:
class: stdClass
@@ -0,0 +1,7 @@
services:
_defaults:
bind:
$quz: overridden

bar:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Bar
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Config\Resource\GlobResource; use Symfony\Component\Config\Resource\GlobResource;
use Symfony\Component\DependencyInjection\Argument\BoundArgument; use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Loader\IniFileLoader; use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
Expand Down Expand Up @@ -863,4 +864,20 @@ public function testTsantosContainer()
$dump = $dumper->dump(); $dump = $dumper->dump();
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_tsantos.php', $dumper->dump()); $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_tsantos.php', $dumper->dump());
} }

/**
* The pass may throw an exception, which will cause the test to fail.
*/
public function testOverriddenDefaultsBindings()
{
$container = new ContainerBuilder();

$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('defaults_bindings.xml');
$loader->load('defaults_bindings2.xml');

(new ResolveBindingsPass())->process($container);

$this->assertSame('overridden', $container->get('bar')->quz);
}
} }
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Config\Resource\GlobResource; use Symfony\Component\Config\Resource\GlobResource;
use Symfony\Component\DependencyInjection\Argument\BoundArgument; use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\IniFileLoader; use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
Expand Down Expand Up @@ -781,4 +782,20 @@ public function testServiceWithSameNameAsInterfaceAndFactoryIsNotTagged()
$tagged = $container->findTaggedServiceIds('bar'); $tagged = $container->findTaggedServiceIds('bar');
$this->assertCount(1, $tagged); $this->assertCount(1, $tagged);
} }

/**
* The pass may throw an exception, which will cause the test to fail.
*/
public function testOverriddenDefaultsBindings()
{
$container = new ContainerBuilder();

$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('defaults_bindings.yml');
$loader->load('defaults_bindings2.yml');

(new ResolveBindingsPass())->process($container);

$this->assertSame('overridden', $container->get('bar')->quz);
}
} }

0 comments on commit dc2edaf

Please sign in to comment.