ControllerArgumentValueResolverPassTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 = array(
  23. 'n3' => array(array()),
  24. 'n1' => array(array('priority' => 200)),
  25. 'n2' => array(array('priority' => 100)),
  26. );
  27. $expected = array(
  28. new Reference('n1'),
  29. new Reference('n2'),
  30. new Reference('n3'),
  31. );
  32. $definition = new Definition(ArgumentResolver::class, array(null, array()));
  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 = array(
  48. 'n3' => array(array()),
  49. 'n1' => array(array('priority' => 200)),
  50. 'n2' => array(array('priority' => 100)),
  51. );
  52. $expected = array(
  53. new Reference('n1'),
  54. new Reference('n2'),
  55. new Reference('n3'),
  56. );
  57. $definition = new Definition(ArgumentResolver::class, array(null, array()));
  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 = array(new Reference('n1'));
  77. $definition = new Definition(ArgumentResolver::class, array(null, array()));
  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, array(null, array()));
  90. $container = new ContainerBuilder();
  91. $container->setDefinition('argument_resolver', $definition);
  92. $container->setParameter('kernel.debug', false);
  93. (new ControllerArgumentValueResolverPass())->process($container);
  94. $this->assertEquals(array(), $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. }