[DI] Add tests for class named services

This commit is contained in:
Martin Hasoň 2016-10-21 12:29:35 +02:00 committed by Nicolas Grekas
parent 71b17c7790
commit a18c4b6ab2
6 changed files with 86 additions and 0 deletions

View File

@ -32,6 +32,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\ExpressionLanguage\Expression;
class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
@ -926,6 +927,44 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
$container->get('foo');
}
public function testClassFromId()
{
$container = new ContainerBuilder();
$unknown = $container->register('unknown_class');
$class = $container->register(\stdClass::class);
$autoloadClass = $container->register(CaseSensitiveClass::class);
$container->compile();
$this->assertSame('unknown_class', $unknown->getClass());
$this->assertEquals(\stdClass::class, $class->getClass());
$this->assertEquals(CaseSensitiveClass::class, $autoloadClass->getClass());
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The definition for "123_abc" has no class.
*/
public function testNoClassFromNonClassId()
{
$container = new ContainerBuilder();
$definition = $container->register('123_abc');
$container->compile();
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The definition for "\foo" has no class.
*/
public function testNoClassFromNsSeparatorId()
{
$container = new ContainerBuilder();
$definition = $container->register('\\foo');
$container->compile();
}
}
class FooClass

View File

@ -0,0 +1,16 @@
<?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\Fixtures;
class CaseSensitiveClass
{
}

View File

@ -0,0 +1,6 @@
<?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>
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass" />
</services>
</container>

View File

@ -0,0 +1,3 @@
services:
Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass:
autowire: true

View File

@ -20,6 +20,7 @@ use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\ExpressionLanguage\Expression;
class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
@ -582,6 +583,16 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
$loader->load('services28.xml');
}
public function testClassFromId()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('class_from_id.xml');
$container->compile();
$this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass());
}
/**
* @group legacy
* @expectedDeprecation Using the attribute "class" is deprecated for the service "bar" which is defined as an alias %s.

View File

@ -20,6 +20,7 @@ use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\ExpressionLanguage\Expression;
class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
@ -341,6 +342,16 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('set*', 'bar'), $container->getDefinition('autowire_array')->getAutowiredMethods());
}
public function testClassFromId()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('class_from_id.yml');
$container->compile();
$this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass());
}
/**
* @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").