Nav apraksta

FragmentRendererPassTest.php 3.6KB

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 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->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
  29. $builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
  30. $builder->expects($this->any())
  31. ->method('hasDefinition')
  32. ->will($this->returnValue(true));
  33. // We don't test kernel.fragment_renderer here
  34. $builder->expects($this->atLeastOnce())
  35. ->method('findTaggedServiceIds')
  36. ->will($this->returnValue($services));
  37. $builder->expects($this->atLeastOnce())
  38. ->method('getDefinition')
  39. ->will($this->returnValue($definition));
  40. $pass = new FragmentRendererPass();
  41. $pass->process($builder);
  42. }
  43. public function testValidContentRenderer()
  44. {
  45. $services = array(
  46. 'my_content_renderer' => array(array('alias' => 'foo')),
  47. );
  48. $renderer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
  49. $renderer
  50. ->expects($this->once())
  51. ->method('addMethodCall')
  52. ->with('addRendererService', array('foo', 'my_content_renderer'))
  53. ;
  54. $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
  55. $definition->expects($this->atLeastOnce())
  56. ->method('getClass')
  57. ->will($this->returnValue('Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService'));
  58. $definition
  59. ->expects($this->once())
  60. ->method('isPublic')
  61. ->will($this->returnValue(true))
  62. ;
  63. $builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
  64. $builder->expects($this->any())
  65. ->method('hasDefinition')
  66. ->will($this->returnValue(true));
  67. // We don't test kernel.fragment_renderer here
  68. $builder->expects($this->atLeastOnce())
  69. ->method('findTaggedServiceIds')
  70. ->will($this->returnValue($services));
  71. $builder->expects($this->atLeastOnce())
  72. ->method('getDefinition')
  73. ->will($this->onConsecutiveCalls($renderer, $definition));
  74. $pass = new FragmentRendererPass();
  75. $pass->process($builder);
  76. }
  77. }
  78. class RendererService implements FragmentRendererInterface
  79. {
  80. public function render($uri, Request $request = null, array $options = array())
  81. {
  82. }
  83. public function getName()
  84. {
  85. return 'test';
  86. }
  87. }