Skip to content

Commit 3e4c8fd

Browse files
hasonnicolas-grekas
authored andcommitted
[DI] Add tests for class named services
1 parent d92da1f commit 3e4c8fd

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

Tests/ContainerBuilderTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
3333
use Symfony\Component\Config\Resource\FileResource;
3434
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
35+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
3536
use Symfony\Component\ExpressionLanguage\Expression;
3637

3738
class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
@@ -926,6 +927,44 @@ public function testClosureProxyOnInvalidException()
926927

927928
$container->get('foo');
928929
}
930+
931+
public function testClassFromId()
932+
{
933+
$container = new ContainerBuilder();
934+
935+
$unknown = $container->register('unknown_class');
936+
$class = $container->register(\stdClass::class);
937+
$autoloadClass = $container->register(CaseSensitiveClass::class);
938+
$container->compile();
939+
940+
$this->assertSame('unknown_class', $unknown->getClass());
941+
$this->assertEquals(\stdClass::class, $class->getClass());
942+
$this->assertEquals(CaseSensitiveClass::class, $autoloadClass->getClass());
943+
}
944+
945+
/**
946+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
947+
* @expectedExceptionMessage The definition for "123_abc" has no class.
948+
*/
949+
public function testNoClassFromNonClassId()
950+
{
951+
$container = new ContainerBuilder();
952+
953+
$definition = $container->register('123_abc');
954+
$container->compile();
955+
}
956+
957+
/**
958+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
959+
* @expectedExceptionMessage The definition for "\foo" has no class.
960+
*/
961+
public function testNoClassFromNsSeparatorId()
962+
{
963+
$container = new ContainerBuilder();
964+
965+
$definition = $container->register('\\foo');
966+
$container->compile();
967+
}
929968
}
930969

931970
class FooClass

Tests/Fixtures/CaseSensitiveClass.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
class CaseSensitiveClass
15+
{
16+
}

Tests/Fixtures/xml/class_from_id.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass" />
5+
</services>
6+
</container>

Tests/Fixtures/yaml/class_from_id.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass:
3+
autowire: true

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
2121
use Symfony\Component\Config\Loader\LoaderResolver;
2222
use Symfony\Component\Config\FileLocator;
23+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
2324
use Symfony\Component\ExpressionLanguage\Expression;
2425

2526
class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
@@ -582,6 +583,16 @@ public function testAutowireAttributeAndTag()
582583
$loader->load('services28.xml');
583584
}
584585

586+
public function testClassFromId()
587+
{
588+
$container = new ContainerBuilder();
589+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
590+
$loader->load('class_from_id.xml');
591+
$container->compile();
592+
593+
$this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass());
594+
}
595+
585596
/**
586597
* @group legacy
587598
* @expectedDeprecation Using the attribute "class" is deprecated for the service "bar" which is defined as an alias %s.

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
2121
use Symfony\Component\Config\Loader\LoaderResolver;
2222
use Symfony\Component\Config\FileLocator;
23+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
2324
use Symfony\Component\ExpressionLanguage\Expression;
2425

2526
class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
@@ -341,6 +342,16 @@ public function testAutowire()
341342
$this->assertEquals(array('set*', 'bar'), $container->getDefinition('autowire_array')->getAutowiredMethods());
342343
}
343344

345+
public function testClassFromId()
346+
{
347+
$container = new ContainerBuilder();
348+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
349+
$loader->load('class_from_id.yml');
350+
$container->compile();
351+
352+
$this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass());
353+
}
354+
344355
/**
345356
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
346357
* @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").

0 commit comments

Comments
 (0)