Skip to content

Commit

Permalink
[DependencyInjection] Tests + refacto for "instanceof" definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas authored and nicolas-grekas committed Feb 17, 2017
1 parent 2fb6019 commit 773eca7
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
Expand Up @@ -283,6 +283,21 @@ private function parseDefaults(array &$content, $file)
return $defaults;
}

private function isUnderscoredParamValid($content, $name, $file)
{
if (!isset($content['services'][$name])) {
return false;
}

if (!is_array($underscoreParam = $content['services'][$name])) {
throw new InvalidArgumentException(sprintf('Service "%s" key must be an array, "%s" given in "%s".', $name, gettype($underscoreParam), $file));
}

// @deprecated condition, to be removed in 4.0

return !isset($underscoreParam['alias']) && !isset($underscoreParam['class']) && !isset($underscoreParam['factory']);
}

/**
* @param array $service
*
Expand Down
Expand Up @@ -22,6 +22,7 @@ class PassConfigTest extends \PHPUnit_Framework_TestCase
public function testPassOrdering()
{
$config = new PassConfig();
$config->setBeforeOptimizationPasses(array());

$pass1 = $this->getMockBuilder(CompilerPassInterface::class)->getMock();
$config->addPass($pass1, PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
Expand All @@ -30,7 +31,7 @@ public function testPassOrdering()
$config->addPass($pass2, PassConfig::TYPE_BEFORE_OPTIMIZATION, 30);

$passes = $config->getBeforeOptimizationPasses();
$this->assertSame($pass2, $passes[1]);
$this->assertSame($pass1, $passes[2]);
$this->assertSame($pass2, $passes[0]);
$this->assertSame($pass1, $passes[1]);
}
}
@@ -0,0 +1,12 @@
<?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>
<instanceof id="Symfony\Component\DependencyInjection\Tests\Loader\BarInterface" lazy="true">
<autowire>set*</autowire>
<tag name="foo" />
<tag name="bar" />
</instanceof>

<service id="Symfony\Component\DependencyInjection\Tests\Loader\Bar" class="Symfony\Component\DependencyInjection\Tests\Loader\Bar" autowire="true" />
</services>
</container>
@@ -0,0 +1,10 @@
services:
_instanceof:
Symfony\Component\DependencyInjection\Tests\Loader\FooInterface:
autowire: true
lazy: true
tags:
- { name: foo }
- { name: bar }

Symfony\Component\DependencyInjection\Tests\Loader\Foo: ~
Expand Up @@ -719,4 +719,25 @@ public function testNamedArguments()
$this->assertEquals(array(null, 'ABCD'), $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
$this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition(NamedArgumentsDummy::class)->getMethodCalls());
}

public function testInstanceof()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services_instanceof.xml');
$container->compile();

$definition = $container->getDefinition(Bar::class);
$this->assertSame(array('__construct', 'set*'), $definition->getAutowiredCalls());
$this->assertTrue($definition->isLazy());
$this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
}
}

interface BarInterface
{
}

class Bar implements BarInterface
{
}
Expand Up @@ -469,6 +469,19 @@ public function testNamedArguments()
$this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition('another_one')->getMethodCalls());
}

public function testInstanceof()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services_instanceof.yml');
$container->compile();

$definition = $container->getDefinition(Foo::class);
$this->assertTrue($definition->isAutowired());
$this->assertTrue($definition->isLazy());
$this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage The value of the "decorates" option for the "bar" service must be the id of the service without the "@" prefix (replace "@foo" with "foo").
Expand Down Expand Up @@ -500,3 +513,11 @@ public function testUnderscoreServiceId()
$loader->load('services_underscore.yml');
}
}

interface FooInterface
{
}

class Foo implements FooInterface
{
}

0 comments on commit 773eca7

Please sign in to comment.