AutowireExceptionPassTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\AutowireExceptionPass;
  13. use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
  14. use Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
  17. class AutowireExceptionPassTest extends TestCase
  18. {
  19. public function testThrowsException()
  20. {
  21. $autowirePass = $this->getMockBuilder(AutowirePass::class)
  22. ->getMock();
  23. $autowireException = new AutowiringFailedException('foo_service_id', 'An autowiring exception message');
  24. $autowirePass->expects($this->any())
  25. ->method('getAutowiringExceptions')
  26. ->will($this->returnValue(array($autowireException)));
  27. $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
  28. ->getMock();
  29. $inlinePass->expects($this->any())
  30. ->method('getInlinedServiceIds')
  31. ->will($this->returnValue(array()));
  32. $container = new ContainerBuilder();
  33. $container->register('foo_service_id');
  34. $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
  35. try {
  36. $pass->process($container);
  37. $this->fail('->process() should throw the exception if the service id exists');
  38. } catch (\Exception $e) {
  39. $this->assertSame($autowireException, $e);
  40. }
  41. }
  42. public function testThrowExceptionIfServiceInlined()
  43. {
  44. $autowirePass = $this->getMockBuilder(AutowirePass::class)
  45. ->getMock();
  46. $autowireException = new AutowiringFailedException('a_service', 'An autowiring exception message');
  47. $autowirePass->expects($this->any())
  48. ->method('getAutowiringExceptions')
  49. ->will($this->returnValue(array($autowireException)));
  50. $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
  51. ->getMock();
  52. $inlinePass->expects($this->any())
  53. ->method('getInlinedServiceIds')
  54. ->will($this->returnValue(array(
  55. // a_service inlined into b_service
  56. 'a_service' => array('b_service'),
  57. // b_service inlined into c_service
  58. 'b_service' => array('c_service'),
  59. )));
  60. $container = new ContainerBuilder();
  61. // ONLY register c_service in the final container
  62. $container->register('c_service', 'stdClass');
  63. $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
  64. try {
  65. $pass->process($container);
  66. $this->fail('->process() should throw the exception if the service id exists');
  67. } catch (\Exception $e) {
  68. $this->assertSame($autowireException, $e);
  69. }
  70. }
  71. public function testDoNotThrowExceptionIfServiceInlinedButRemoved()
  72. {
  73. $autowirePass = $this->getMockBuilder(AutowirePass::class)
  74. ->getMock();
  75. $autowireException = new AutowiringFailedException('a_service', 'An autowiring exception message');
  76. $autowirePass->expects($this->any())
  77. ->method('getAutowiringExceptions')
  78. ->will($this->returnValue(array($autowireException)));
  79. $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
  80. ->getMock();
  81. $inlinePass->expects($this->any())
  82. ->method('getInlinedServiceIds')
  83. ->will($this->returnValue(array(
  84. // a_service inlined into b_service
  85. 'a_service' => array('b_service'),
  86. // b_service inlined into c_service
  87. 'b_service' => array('c_service'),
  88. )));
  89. // do NOT register c_service in the container
  90. $container = new ContainerBuilder();
  91. $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
  92. $pass->process($container);
  93. // mark the test as passed
  94. $this->assertTrue(true);
  95. }
  96. public function testNoExceptionIfServiceRemoved()
  97. {
  98. $autowirePass = $this->getMockBuilder(AutowirePass::class)
  99. ->getMock();
  100. $autowireException = new AutowiringFailedException('non_existent_service');
  101. $autowirePass->expects($this->any())
  102. ->method('getAutowiringExceptions')
  103. ->will($this->returnValue(array($autowireException)));
  104. $inlinePass = $this->getMockBuilder(InlineServiceDefinitionsPass::class)
  105. ->getMock();
  106. $inlinePass->expects($this->any())
  107. ->method('getInlinedServiceIds')
  108. ->will($this->returnValue(array()));
  109. $container = new ContainerBuilder();
  110. $pass = new AutowireExceptionPass($autowirePass, $inlinePass);
  111. $pass->process($container);
  112. // mark the test as passed
  113. $this->assertTrue(true);
  114. }
  115. }