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\Compiler\CheckDefinitionValidityPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  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. * @group legacy
  29. */
  30. public function testProcessDetectsSyntheticPrototypeDefinitions()
  31. {
  32. $container = new ContainerBuilder();
  33. $container->register('a')->setSynthetic(true)->setScope(ContainerInterface::SCOPE_PROTOTYPE);
  34. $this->process($container);
  35. }
  36. /**
  37. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  38. * @group legacy
  39. */
  40. public function testProcessDetectsSharedPrototypeDefinitions()
  41. {
  42. $container = new ContainerBuilder();
  43. $container->register('a')->setShared(true)->setScope(ContainerInterface::SCOPE_PROTOTYPE);
  44. $this->process($container);
  45. }
  46. /**
  47. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  48. */
  49. public function testProcessDetectsNonSyntheticNonAbstractDefinitionWithoutClass()
  50. {
  51. $container = new ContainerBuilder();
  52. $container->register('a')->setSynthetic(false)->setAbstract(false);
  53. $this->process($container);
  54. }
  55. /**
  56. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  57. * @group legacy
  58. */
  59. public function testLegacyProcessDetectsBothFactorySyntaxesUsed()
  60. {
  61. $container = new ContainerBuilder();
  62. $container->register('a')->setFactory(array('a', 'b'))->setFactoryClass('a');
  63. $this->process($container);
  64. }
  65. public function testProcess()
  66. {
  67. $container = new ContainerBuilder();
  68. $container->register('a', 'class');
  69. $container->register('b', 'class')->setSynthetic(true)->setPublic(true);
  70. $container->register('c', 'class')->setAbstract(true);
  71. $container->register('d', 'class')->setSynthetic(true);
  72. $this->process($container);
  73. $this->addToAssertionCount(1);
  74. }
  75. public function testValidTags()
  76. {
  77. $container = new ContainerBuilder();
  78. $container->register('a', 'class')->addTag('foo', array('bar' => 'baz'));
  79. $container->register('b', 'class')->addTag('foo', array('bar' => null));
  80. $container->register('c', 'class')->addTag('foo', array('bar' => 1));
  81. $container->register('d', 'class')->addTag('foo', array('bar' => 1.1));
  82. $this->process($container);
  83. $this->addToAssertionCount(1);
  84. }
  85. /**
  86. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  87. */
  88. public function testInvalidTags()
  89. {
  90. $container = new ContainerBuilder();
  91. $container->register('a', 'class')->addTag('foo', array('bar' => array('baz' => 'baz')));
  92. $this->process($container);
  93. }
  94. protected function process(ContainerBuilder $container)
  95. {
  96. $pass = new CheckDefinitionValidityPass();
  97. $pass->process($container);
  98. }
  99. }