FragmentRendererPassTest.php 2.9 KB

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