FragmentRendererPassTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\ServiceClosureArgument;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\ServiceLocator;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\DependencyInjection\FragmentRendererPass;
  18. use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
  19. class FragmentRendererPassTest extends TestCase
  20. {
  21. /**
  22. * Tests that content rendering not implementing FragmentRendererInterface
  23. * triggers an exception.
  24. *
  25. * @expectedException \InvalidArgumentException
  26. */
  27. public function testContentRendererWithoutInterface()
  28. {
  29. $builder = new ContainerBuilder();
  30. $fragmentHandlerDefinition = $builder->register('fragment.handler');
  31. $builder->register('my_content_renderer', 'Symfony\Component\DependencyInjection\Definition')
  32. ->addTag('kernel.fragment_renderer', ['alias' => 'foo']);
  33. $pass = new FragmentRendererPass();
  34. $pass->process($builder);
  35. $this->assertEquals([['addRendererService', ['foo', 'my_content_renderer']]], $fragmentHandlerDefinition->getMethodCalls());
  36. }
  37. public function testValidContentRenderer()
  38. {
  39. $builder = new ContainerBuilder();
  40. $fragmentHandlerDefinition = $builder->register('fragment.handler')
  41. ->addArgument(null);
  42. $builder->register('my_content_renderer', 'Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService')
  43. ->addTag('kernel.fragment_renderer', ['alias' => 'foo']);
  44. $pass = new FragmentRendererPass();
  45. $pass->process($builder);
  46. $serviceLocatorDefinition = $builder->getDefinition((string) $fragmentHandlerDefinition->getArgument(0));
  47. $this->assertSame(ServiceLocator::class, $serviceLocatorDefinition->getClass());
  48. $this->assertEquals(['foo' => new ServiceClosureArgument(new Reference('my_content_renderer'))], $serviceLocatorDefinition->getArgument(0));
  49. }
  50. }
  51. class RendererService implements FragmentRendererInterface
  52. {
  53. public function render($uri, Request $request = null, array $options = [])
  54. {
  55. }
  56. public function getName()
  57. {
  58. return 'test';
  59. }
  60. }