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());
61 $this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->get('example'));
64 public function testProcessWithExistingAlias()
66 $container = new ContainerBuilder();
68 $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
69 ->addTag('auto_alias', array('format' => '%existing%.example'));
71 $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
72 $container->setParameter('existing', 'mysql');
74 $pass = new AutoAliasServicePass();
75 $pass->process($container);
77 $this->assertTrue($container->hasAlias('example'));
78 $this->assertEquals('mysql.example', $container->getAlias('example'));
79 $this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql', $container->get('example'));
82 public function testProcessWithManualAlias()
84 $container = new ContainerBuilder();
86 $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
87 ->addTag('auto_alias', array('format' => '%existing%.example'));
89 $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
90 $container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariadb');
91 $container->setAlias('example', 'mariadb.example');
92 $container->setParameter('existing', 'mysql');
94 $pass = new AutoAliasServicePass();
95 $pass->process($container);
97 $this->assertTrue($container->hasAlias('example'));
98 $this->assertEquals('mariadb.example', $container->getAlias('example'));
99 $this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->get('example'));
103 class ServiceClassDefault
107 class ServiceClassMysql extends ServiceClassDefault
111 class ServiceClassMariaDb extends ServiceClassMysql