LazyLoadingFragmentHandlerTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\HttpKernel\DependencyInjection\LazyLoadingFragmentHandler;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. class LazyLoadingFragmentHandlerTest extends TestCase
  16. {
  17. /**
  18. * @group legacy
  19. * @expectedDeprecation The Symfony\Component\HttpKernel\DependencyInjection\LazyLoadingFragmentHandler::addRendererService() method is deprecated since version 3.3 and will be removed in 4.0.
  20. */
  21. public function testRenderWithLegacyMapping()
  22. {
  23. $renderer = $this->getMockBuilder('Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface')->getMock();
  24. $renderer->expects($this->once())->method('getName')->will($this->returnValue('foo'));
  25. $renderer->expects($this->any())->method('render')->will($this->returnValue(new Response()));
  26. $requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
  27. $requestStack->expects($this->any())->method('getCurrentRequest')->will($this->returnValue(Request::create('/')));
  28. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  29. $container->expects($this->once())->method('get')->will($this->returnValue($renderer));
  30. $handler = new LazyLoadingFragmentHandler($container, $requestStack, false);
  31. $handler->addRendererService('foo', 'foo');
  32. $handler->render('/foo', 'foo');
  33. // second call should not lazy-load anymore (see once() above on the get() method)
  34. $handler->render('/foo', 'foo');
  35. }
  36. public function testRender()
  37. {
  38. $renderer = $this->getMockBuilder('Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface')->getMock();
  39. $renderer->expects($this->once())->method('getName')->will($this->returnValue('foo'));
  40. $renderer->expects($this->any())->method('render')->will($this->returnValue(new Response()));
  41. $requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
  42. $requestStack->expects($this->any())->method('getCurrentRequest')->will($this->returnValue(Request::create('/')));
  43. $container = $this->getMockBuilder('Psr\Container\ContainerInterface')->getMock();
  44. $container->expects($this->once())->method('has')->with('foo')->willReturn(true);
  45. $container->expects($this->once())->method('get')->will($this->returnValue($renderer));
  46. $handler = new LazyLoadingFragmentHandler($container, $requestStack, false);
  47. $handler->render('/foo', 'foo');
  48. // second call should not lazy-load anymore (see once() above on the get() method)
  49. $handler->render('/foo', 'foo');
  50. }
  51. }