RegisterLocaleAwareServicesPassTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\Argument\IteratorArgument;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\HttpKernel\DependencyInjection\RegisterLocaleAwareServicesPass;
  16. use Symfony\Component\HttpKernel\EventListener\LocaleAwareListener;
  17. use Symfony\Contracts\Translation\LocaleAwareInterface;
  18. class RegisterLocaleAwareServicesPassTest extends TestCase
  19. {
  20. public function testCompilerPass()
  21. {
  22. $container = new ContainerBuilder();
  23. $container->register('locale_aware_listener', LocaleAwareListener::class)
  24. ->setPublic(true)
  25. ->setArguments([null, null]);
  26. $container->register('some_locale_aware_service', LocaleAwareInterface::class)
  27. ->setPublic(true)
  28. ->addTag('kernel.locale_aware');
  29. $container->register('another_locale_aware_service', LocaleAwareInterface::class)
  30. ->setPublic(true)
  31. ->addTag('kernel.locale_aware');
  32. $container->addCompilerPass(new RegisterLocaleAwareServicesPass());
  33. $container->compile();
  34. $this->assertEquals(
  35. [
  36. new IteratorArgument([
  37. 0 => new Reference('some_locale_aware_service'),
  38. 1 => new Reference('another_locale_aware_service'),
  39. ]),
  40. null,
  41. ],
  42. $container->getDefinition('locale_aware_listener')->getArguments()
  43. );
  44. }
  45. public function testListenerUnregisteredWhenNoLocaleAwareServices()
  46. {
  47. $container = new ContainerBuilder();
  48. $container->register('locale_aware_listener', LocaleAwareListener::class)
  49. ->setPublic(true)
  50. ->setArguments([null, null]);
  51. $container->addCompilerPass(new RegisterLocaleAwareServicesPass());
  52. $container->compile();
  53. $this->assertFalse($container->hasDefinition('locale_aware_listener'));
  54. }
  55. }