TranslationExtractorPassTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
  15. class TranslationExtractorPassTest extends TestCase
  16. {
  17. public function testProcess()
  18. {
  19. $container = new ContainerBuilder();
  20. $extractorDefinition = $container->register('translation.extractor');
  21. $container->register('foo.id')
  22. ->addTag('translation.extractor', ['alias' => 'bar.alias']);
  23. $translationDumperPass = new TranslationExtractorPass();
  24. $translationDumperPass->process($container);
  25. $this->assertEquals([['addExtractor', ['bar.alias', new Reference('foo.id')]]], $extractorDefinition->getMethodCalls());
  26. }
  27. public function testProcessNoDefinitionFound()
  28. {
  29. $container = new ContainerBuilder();
  30. $definitionsBefore = \count($container->getDefinitions());
  31. $aliasesBefore = \count($container->getAliases());
  32. $translationDumperPass = new TranslationExtractorPass();
  33. $translationDumperPass->process($container);
  34. // the container is untouched (i.e. no new definitions or aliases)
  35. $this->assertCount($definitionsBefore, $container->getDefinitions());
  36. $this->assertCount($aliasesBefore, $container->getAliases());
  37. }
  38. /**
  39. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  40. * @expectedExceptionMessage The alias for the tag "translation.extractor" of service "foo.id" must be set.
  41. */
  42. public function testProcessMissingAlias()
  43. {
  44. $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->disableOriginalConstructor()->getMock();
  45. $container = new ContainerBuilder();
  46. $container->register('translation.extractor');
  47. $container->register('foo.id')
  48. ->addTag('translation.extractor', []);
  49. $definition->expects($this->never())->method('addMethodCall');
  50. $translationDumperPass = new TranslationExtractorPass();
  51. $translationDumperPass->process($container);
  52. }
  53. }