FragmentRendererPassTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\Definition;
  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. * Tests that content rendering not implementing FragmentRendererInterface
  21. * trigger an exception.
  22. *
  23. * @expectedException \InvalidArgumentException
  24. */
  25. public function testContentRendererWithoutInterface()
  26. {
  27. // one service, not implementing any interface
  28. $services = array(
  29. 'my_content_renderer' => array(array('alias' => 'foo')),
  30. );
  31. $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
  32. $builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
  33. $builder->expects($this->any())
  34. ->method('hasDefinition')
  35. ->will($this->returnValue(true));
  36. // We don't test kernel.fragment_renderer here
  37. $builder->expects($this->atLeastOnce())
  38. ->method('findTaggedServiceIds')
  39. ->will($this->returnValue($services));
  40. $builder->expects($this->atLeastOnce())
  41. ->method('getDefinition')
  42. ->will($this->returnValue($definition));
  43. $pass = new FragmentRendererPass();
  44. $pass->process($builder);
  45. }
  46. public function testValidContentRenderer()
  47. {
  48. $services = array(
  49. 'my_content_renderer' => array(array('alias' => 'foo')),
  50. );
  51. $renderer = new Definition('', array(null));
  52. $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
  53. $definition->expects($this->atLeastOnce())
  54. ->method('getClass')
  55. ->will($this->returnValue('Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService'));
  56. $builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition', 'getReflectionClass'))->getMock();
  57. $builder->expects($this->any())
  58. ->method('hasDefinition')
  59. ->will($this->returnValue(true));
  60. // We don't test kernel.fragment_renderer here
  61. $builder->expects($this->atLeastOnce())
  62. ->method('findTaggedServiceIds')
  63. ->will($this->returnValue($services));
  64. $builder->expects($this->atLeastOnce())
  65. ->method('getDefinition')
  66. ->will($this->onConsecutiveCalls($renderer, $definition));
  67. $builder->expects($this->atLeastOnce())
  68. ->method('getReflectionClass')
  69. ->with('Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService')
  70. ->will($this->returnValue(new \ReflectionClass('Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService')));
  71. $pass = new FragmentRendererPass();
  72. $pass->process($builder);
  73. $this->assertInstanceOf(Reference::class, $renderer->getArgument(0));
  74. }
  75. }
  76. class RendererService implements FragmentRendererInterface
  77. {
  78. public function render($uri, Request $request = null, array $options = array())
  79. {
  80. }
  81. public function getName()
  82. {
  83. return 'test';
  84. }
  85. }