FragmentRendererPassTest.php 3.6 KB

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