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