4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Compiler;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Compiler\AutoAliasServicePass;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 class AutoAliasServicePassTest extends TestCase
21 * @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException
23 public function testProcessWithMissingParameter()
25 $container = new ContainerBuilder();
27 $container->register('example')
28 ->addTag('auto_alias', array('format' => '%non_existing%.example'));
30 $pass = new AutoAliasServicePass();
31 $pass->process($container);
35 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
37 public function testProcessWithMissingFormat()
39 $container = new ContainerBuilder();
41 $container->register('example')
42 ->addTag('auto_alias', array());
43 $container->setParameter('existing', 'mysql');
45 $pass = new AutoAliasServicePass();
46 $pass->process($container);
49 public function testProcessWithNonExistingAlias()
51 $container = new ContainerBuilder();
53 $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
54 ->addTag('auto_alias', array('format' => '%existing%.example'));
55 $container->setParameter('existing', 'mysql');
57 $pass = new AutoAliasServicePass();
58 $pass->process($container);
60 $this->assertEquals('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->getDefinition('example')->getClass());
63 public function testProcessWithExistingAlias()
65 $container = new ContainerBuilder();
67 $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
68 ->addTag('auto_alias', array('format' => '%existing%.example'));
70 $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
71 $container->setParameter('existing', 'mysql');
73 $pass = new AutoAliasServicePass();
74 $pass->process($container);
76 $this->assertTrue($container->hasAlias('example'));
77 $this->assertEquals('mysql.example', $container->getAlias('example'));
78 $this->assertSame('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql', $container->getDefinition('mysql.example')->getClass());
81 public function testProcessWithManualAlias()
83 $container = new ContainerBuilder();
85 $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
86 ->addTag('auto_alias', array('format' => '%existing%.example'));
88 $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
89 $container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb');
90 $container->setAlias('example', 'mariadb.example');
91 $container->setParameter('existing', 'mysql');
93 $pass = new AutoAliasServicePass();
94 $pass->process($container);
96 $this->assertTrue($container->hasAlias('example'));
97 $this->assertEquals('mariadb.example', $container->getAlias('example'));
98 $this->assertSame('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->getDefinition('mariadb.example')->getClass());
102 class ServiceClassDefault
106 class ServiceClassMysql extends ServiceClassDefault
110 class ServiceClassMariaDb extends ServiceClassMysql