TranslationPathsPassTest.php 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\Translation\Tests\DependencyInjection;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\DependencyInjection\ServiceLocator;
  17. use Symfony\Component\Translation\DependencyInjection\TranslatorPathsPass;
  18. use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ControllerArguments;
  19. use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceArguments;
  20. use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceMethodCalls;
  21. use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceProperties;
  22. use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceSubscriber;
  23. class TranslationPathsPassTest extends TestCase
  24. {
  25. public function testProcess()
  26. {
  27. $container = new ContainerBuilder();
  28. $container->register('translator');
  29. $debugCommand = $container->register('console.command.translation_debug')
  30. ->setArguments([null, null, null, null, null, [], []])
  31. ;
  32. $updateCommand = $container->register('console.command.translation_update')
  33. ->setArguments([null, null, null, null, null, null, [], []])
  34. ;
  35. $container->register(ControllerArguments::class, ControllerArguments::class)
  36. ->setTags(['controller.service_arguments'])
  37. ;
  38. $container->register(ServiceArguments::class, ServiceArguments::class)
  39. ->setArguments([new Reference('translator')])
  40. ;
  41. $container->register(ServiceProperties::class, ServiceProperties::class)
  42. ->setProperties([new Reference('translator')])
  43. ;
  44. $container->register(ServiceMethodCalls::class, ServiceMethodCalls::class)
  45. ->setMethodCalls([['setTranslator', [new Reference('translator')]]])
  46. ;
  47. $container->register('service_rc')
  48. ->setArguments([new Definition(), new Reference(ServiceMethodCalls::class)])
  49. ;
  50. $serviceLocator1 = $container->register('.service_locator.foo', ServiceLocator::class)
  51. ->setArguments([new ServiceClosureArgument(new Reference('translator'))])
  52. ;
  53. $serviceLocator2 = (new Definition(ServiceLocator::class))
  54. ->setArguments([ServiceSubscriber::class, new Reference('service_container')])
  55. ->setFactory([$serviceLocator1, 'withContext'])
  56. ;
  57. $container->register('service_subscriber', ServiceSubscriber::class)
  58. ->setArguments([$serviceLocator2])
  59. ;
  60. $container->register('.service_locator.bar', ServiceLocator::class)
  61. ->setArguments([[
  62. ControllerArguments::class.'::index' => new ServiceClosureArgument(new Reference('.service_locator.foo')),
  63. ControllerArguments::class.'::__invoke' => new ServiceClosureArgument(new Reference('.service_locator.foo')),
  64. ControllerArguments::class => new ServiceClosureArgument(new Reference('.service_locator.foo')),
  65. ]])
  66. ;
  67. $container->register('argument_resolver.service')
  68. ->setArguments([new Reference('.service_locator.bar')])
  69. ;
  70. $pass = new TranslatorPathsPass('translator', 'console.command.translation_debug', 'console.command.translation_update', 'argument_resolver.service');
  71. $pass->process($container);
  72. $expectedPaths = [
  73. $container->getReflectionClass(ServiceArguments::class)->getFileName(),
  74. $container->getReflectionClass(ServiceProperties::class)->getFileName(),
  75. $container->getReflectionClass(ServiceMethodCalls::class)->getFileName(),
  76. $container->getReflectionClass(ControllerArguments::class)->getFileName(),
  77. $container->getReflectionClass(ServiceSubscriber::class)->getFileName(),
  78. ];
  79. $this->assertSame($expectedPaths, $debugCommand->getArgument(6));
  80. $this->assertSame($expectedPaths, $updateCommand->getArgument(7));
  81. }
  82. }