AutoAliasServicePassTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Tests\Compiler;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\Compiler\AutoAliasServicePass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. class AutoAliasServicePassTest extends TestCase
  15. {
  16. /**
  17. * @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException
  18. */
  19. public function testProcessWithMissingParameter()
  20. {
  21. $container = new ContainerBuilder();
  22. $container->register('example')
  23. ->addTag('auto_alias', array('format' => '%non_existing%.example'));
  24. $pass = new AutoAliasServicePass();
  25. $pass->process($container);
  26. }
  27. /**
  28. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  29. */
  30. public function testProcessWithMissingFormat()
  31. {
  32. $container = new ContainerBuilder();
  33. $container->register('example')
  34. ->addTag('auto_alias', array());
  35. $container->setParameter('existing', 'mysql');
  36. $pass = new AutoAliasServicePass();
  37. $pass->process($container);
  38. }
  39. public function testProcessWithNonExistingAlias()
  40. {
  41. $container = new ContainerBuilder();
  42. $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
  43. ->addTag('auto_alias', array('format' => '%existing%.example'));
  44. $container->setParameter('existing', 'mysql');
  45. $pass = new AutoAliasServicePass();
  46. $pass->process($container);
  47. $this->assertEquals('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->getDefinition('example')->getClass());
  48. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->get('example'));
  49. }
  50. public function testProcessWithExistingAlias()
  51. {
  52. $container = new ContainerBuilder();
  53. $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
  54. ->addTag('auto_alias', array('format' => '%existing%.example'));
  55. $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
  56. $container->setParameter('existing', 'mysql');
  57. $pass = new AutoAliasServicePass();
  58. $pass->process($container);
  59. $this->assertTrue($container->hasAlias('example'));
  60. $this->assertEquals('mysql.example', $container->getAlias('example'));
  61. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql', $container->get('example'));
  62. }
  63. public function testProcessWithManualAlias()
  64. {
  65. $container = new ContainerBuilder();
  66. $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault')
  67. ->addTag('auto_alias', array('format' => '%existing%.example'));
  68. $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
  69. $container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariadb');
  70. $container->setAlias('example', 'mariadb.example');
  71. $container->setParameter('existing', 'mysql');
  72. $pass = new AutoAliasServicePass();
  73. $pass->process($container);
  74. $this->assertTrue($container->hasAlias('example'));
  75. $this->assertEquals('mariadb.example', $container->getAlias('example'));
  76. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->get('example'));
  77. }
  78. }
  79. class ServiceClassDefault
  80. {
  81. }
  82. class ServiceClassMysql extends ServiceClassDefault
  83. {
  84. }
  85. class ServiceClassMariaDb extends ServiceClassMysql
  86. {
  87. }