CheckDefinitionValidityPassTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Alias;
  13. use Symfony\Component\DependencyInjection\Compiler\CheckDefinitionValidityPass;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. class CheckDefinitionValidityPassTest extends TestCase
  16. {
  17. /**
  18. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  19. */
  20. public function testProcessDetectsSyntheticNonPublicDefinitions()
  21. {
  22. $container = new ContainerBuilder();
  23. $container->register('a')->setSynthetic(true)->setPublic(false);
  24. $this->process($container);
  25. }
  26. /**
  27. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  28. */
  29. public function testProcessDetectsNonSyntheticNonAbstractDefinitionWithoutClass()
  30. {
  31. $container = new ContainerBuilder();
  32. $container->register('a')->setSynthetic(false)->setAbstract(false);
  33. $this->process($container);
  34. }
  35. public function testProcess()
  36. {
  37. $container = new ContainerBuilder();
  38. $container->register('a', 'class');
  39. $container->register('b', 'class')->setSynthetic(true)->setPublic(true);
  40. $container->register('c', 'class')->setAbstract(true);
  41. $container->register('d', 'class')->setSynthetic(true);
  42. $this->process($container);
  43. $this->addToAssertionCount(1);
  44. }
  45. public function testValidTags()
  46. {
  47. $container = new ContainerBuilder();
  48. $container->register('a', 'class')->addTag('foo', array('bar' => 'baz'));
  49. $container->register('b', 'class')->addTag('foo', array('bar' => null));
  50. $container->register('c', 'class')->addTag('foo', array('bar' => 1));
  51. $container->register('d', 'class')->addTag('foo', array('bar' => 1.1));
  52. $this->process($container);
  53. $this->addToAssertionCount(1);
  54. }
  55. /**
  56. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  57. */
  58. public function testInvalidTags()
  59. {
  60. $container = new ContainerBuilder();
  61. $container->register('a', 'class')->addTag('foo', array('bar' => array('baz' => 'baz')));
  62. $this->process($container);
  63. }
  64. /**
  65. * @expectedException \Symfony\Component\DependencyInjection\Exception\EnvParameterException
  66. */
  67. public function testDynamicPublicServiceName()
  68. {
  69. $container = new ContainerBuilder();
  70. $env = $container->getParameterBag()->get('env(BAR)');
  71. $container->register("foo.$env", 'class')->setPublic(true);
  72. $this->process($container);
  73. }
  74. /**
  75. * @expectedException \Symfony\Component\DependencyInjection\Exception\EnvParameterException
  76. */
  77. public function testDynamicPublicAliasName()
  78. {
  79. $container = new ContainerBuilder();
  80. $env = $container->getParameterBag()->get('env(BAR)');
  81. $container->setAlias("foo.$env", new Alias('class', true));
  82. $this->process($container);
  83. }
  84. public function testDynamicPrivateName()
  85. {
  86. $container = new ContainerBuilder();
  87. $env = $container->getParameterBag()->get('env(BAR)');
  88. $container->register("foo.$env", 'class')->setPublic(false);
  89. $container->setAlias("bar.$env", new Alias('class', false));
  90. $this->process($container);
  91. $this->addToAssertionCount(1);
  92. }
  93. protected function process(ContainerBuilder $container)
  94. {
  95. $pass = new CheckDefinitionValidityPass();
  96. $pass->process($container);
  97. }
  98. }