ControllerArgumentValueResolverPassTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\HttpKernel\Tests\DependencyInjection;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
  16. use Symfony\Component\HttpKernel\DependencyInjection\ControllerArgumentValueResolverPass;
  17. use Symfony\Component\Stopwatch\Stopwatch;
  18. class ControllerArgumentValueResolverPassTest extends TestCase
  19. {
  20. public function testServicesAreOrderedAccordingToPriority()
  21. {
  22. $services = [
  23. 'n3' => [[]],
  24. 'n1' => [['priority' => 200]],
  25. 'n2' => [['priority' => 100]],
  26. ];
  27. $expected = [
  28. new Reference('n1'),
  29. new Reference('n2'),
  30. new Reference('n3'),
  31. ];
  32. $definition = new Definition(ArgumentResolver::class, [null, []]);
  33. $container = new ContainerBuilder();
  34. $container->setDefinition('argument_resolver', $definition);
  35. foreach ($services as $id => list($tag)) {
  36. $container->register($id)->addTag('controller.argument_value_resolver', $tag);
  37. }
  38. $container->setParameter('kernel.debug', false);
  39. (new ControllerArgumentValueResolverPass())->process($container);
  40. $this->assertEquals($expected, $definition->getArgument(1)->getValues());
  41. $this->assertFalse($container->hasDefinition('n1.traceable'));
  42. $this->assertFalse($container->hasDefinition('n2.traceable'));
  43. $this->assertFalse($container->hasDefinition('n3.traceable'));
  44. }
  45. public function testInDebugWithStopWatchDefinition()
  46. {
  47. $services = [
  48. 'n3' => [[]],
  49. 'n1' => [['priority' => 200]],
  50. 'n2' => [['priority' => 100]],
  51. ];
  52. $expected = [
  53. new Reference('n1'),
  54. new Reference('n2'),
  55. new Reference('n3'),
  56. ];
  57. $definition = new Definition(ArgumentResolver::class, [null, []]);
  58. $container = new ContainerBuilder();
  59. $container->register('debug.stopwatch', Stopwatch::class);
  60. $container->setDefinition('argument_resolver', $definition);
  61. foreach ($services as $id => list($tag)) {
  62. $container->register($id)->addTag('controller.argument_value_resolver', $tag);
  63. }
  64. $container->setParameter('kernel.debug', true);
  65. (new ControllerArgumentValueResolverPass())->process($container);
  66. $this->assertEquals($expected, $definition->getArgument(1)->getValues());
  67. $this->assertTrue($container->hasDefinition('debug.n1'));
  68. $this->assertTrue($container->hasDefinition('debug.n2'));
  69. $this->assertTrue($container->hasDefinition('debug.n3'));
  70. $this->assertTrue($container->hasDefinition('n1'));
  71. $this->assertTrue($container->hasDefinition('n2'));
  72. $this->assertTrue($container->hasDefinition('n3'));
  73. }
  74. public function testInDebugWithouStopWatchDefinition()
  75. {
  76. $expected = [new Reference('n1')];
  77. $definition = new Definition(ArgumentResolver::class, [null, []]);
  78. $container = new ContainerBuilder();
  79. $container->register('n1')->addTag('controller.argument_value_resolver');
  80. $container->setDefinition('argument_resolver', $definition);
  81. $container->setParameter('kernel.debug', true);
  82. (new ControllerArgumentValueResolverPass())->process($container);
  83. $this->assertEquals($expected, $definition->getArgument(1)->getValues());
  84. $this->assertFalse($container->hasDefinition('debug.n1'));
  85. $this->assertTrue($container->hasDefinition('n1'));
  86. }
  87. public function testReturningEmptyArrayWhenNoService()
  88. {
  89. $definition = new Definition(ArgumentResolver::class, [null, []]);
  90. $container = new ContainerBuilder();
  91. $container->setDefinition('argument_resolver', $definition);
  92. $container->setParameter('kernel.debug', false);
  93. (new ControllerArgumentValueResolverPass())->process($container);
  94. $this->assertEquals([], $definition->getArgument(1)->getValues());
  95. }
  96. public function testNoArgumentResolver()
  97. {
  98. $container = new ContainerBuilder();
  99. (new ControllerArgumentValueResolverPass())->process($container);
  100. $this->assertFalse($container->hasDefinition('argument_resolver'));
  101. }
  102. }