TranslationPassTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Translation\DependencyInjection\TranslatorPass;
  17. class TranslationPassTest extends TestCase
  18. {
  19. public function testValidCollector()
  20. {
  21. $loader = (new Definition())
  22. ->addTag('translation.loader', ['alias' => 'xliff', 'legacy-alias' => 'xlf']);
  23. $reader = new Definition();
  24. $translator = (new Definition())
  25. ->setArguments([null, null, null, null]);
  26. $container = new ContainerBuilder();
  27. $container->setDefinition('translator.default', $translator);
  28. $container->setDefinition('translation.reader', $reader);
  29. $container->setDefinition('translation.xliff_loader', $loader);
  30. $pass = new TranslatorPass('translator.default', 'translation.reader');
  31. $pass->process($container);
  32. $expectedReader = (new Definition())
  33. ->addMethodCall('addLoader', ['xliff', new Reference('translation.xliff_loader')])
  34. ->addMethodCall('addLoader', ['xlf', new Reference('translation.xliff_loader')])
  35. ;
  36. $this->assertEquals($expectedReader, $reader);
  37. $expectedLoader = (new Definition())
  38. ->addTag('translation.loader', ['alias' => 'xliff', 'legacy-alias' => 'xlf'])
  39. ;
  40. $this->assertEquals($expectedLoader, $loader);
  41. $this->assertSame(['translation.xliff_loader' => ['xliff', 'xlf']], $translator->getArgument(3));
  42. $expected = ['translation.xliff_loader' => new ServiceClosureArgument(new Reference('translation.xliff_loader'))];
  43. $this->assertEquals($expected, $container->getDefinition((string) $translator->getArgument(0))->getArgument(0));
  44. }
  45. public function testValidCommandsViewPathsArgument()
  46. {
  47. $container = new ContainerBuilder();
  48. $container->register('translator.default')
  49. ->setArguments([null, null, null, null])
  50. ;
  51. $debugCommand = $container->register('console.command.translation_debug')
  52. ->setArguments([null, null, null, null, null, [], []])
  53. ;
  54. $updateCommand = $container->register('console.command.translation_update')
  55. ->setArguments([null, null, null, null, null, null, [], []])
  56. ;
  57. $container->register('twig.template_iterator')
  58. ->setArguments([null, null, ['other/templates' => null, 'tpl' => 'App']])
  59. ;
  60. $container->setParameter('twig.default_path', 'templates');
  61. $pass = new TranslatorPass('translator.default');
  62. $pass->process($container);
  63. $expectedViewPaths = ['other/templates', 'tpl'];
  64. $this->assertSame('templates', $debugCommand->getArgument(4));
  65. $this->assertSame('templates', $updateCommand->getArgument(5));
  66. $this->assertSame($expectedViewPaths, $debugCommand->getArgument(6));
  67. $this->assertSame($expectedViewPaths, $updateCommand->getArgument(7));
  68. }
  69. public function testCommandsViewPathsArgumentsAreIgnoredWithOldServiceDefinitions()
  70. {
  71. $container = new ContainerBuilder();
  72. $container->register('translator.default')
  73. ->setArguments([null, null, null, null])
  74. ;
  75. $debugCommand = $container->register('console.command.translation_debug')
  76. ->setArguments([
  77. new Reference('translator'),
  78. new Reference('translation.reader'),
  79. new Reference('translation.extractor'),
  80. '%translator.default_path%',
  81. null,
  82. ])
  83. ;
  84. $updateCommand = $container->register('console.command.translation_update')
  85. ->setArguments([
  86. new Reference('translation.writer'),
  87. new Reference('translation.reader'),
  88. new Reference('translation.extractor'),
  89. '%kernel.default_locale%',
  90. '%translator.default_path%',
  91. null,
  92. ])
  93. ;
  94. $container->register('twig.template_iterator')
  95. ->setArguments([null, null, ['other/templates' => null, 'tpl' => 'App']])
  96. ;
  97. $container->setParameter('twig.default_path', 'templates');
  98. $pass = new TranslatorPass('translator.default');
  99. $pass->process($container);
  100. $this->assertSame('templates', $debugCommand->getArgument(4));
  101. $this->assertSame('templates', $updateCommand->getArgument(5));
  102. }
  103. }