[DependencyInjection] Tests + refacto for "instanceof" definitions
This commit is contained in:
parent
2fb601983f
commit
773eca7794
@ -283,6 +283,21 @@ class YamlFileLoader extends FileLoader
|
||||
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
|
||||
*
|
||||
|
@ -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);
|
||||
@ -30,7 +31,7 @@ class PassConfigTest extends \PHPUnit_Framework_TestCase
|
||||
$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: ~
|
@ -719,4 +719,25 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$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
|
||||
{
|
||||
}
|
||||
|
@ -469,6 +469,19 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$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").
|
||||
@ -500,3 +513,11 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$loader->load('services_underscore.yml');
|
||||
}
|
||||
}
|
||||
|
||||
interface FooInterface
|
||||
{
|
||||
}
|
||||
|
||||
class Foo implements FooInterface
|
||||
{
|
||||
}
|
||||
|
Reference in New Issue
Block a user