HttpKernelExtensionTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Bridge\Twig\Tests\Extension;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
  13. use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
  17. use Twig\Environment;
  18. use Twig\Loader\ArrayLoader;
  19. class HttpKernelExtensionTest extends TestCase
  20. {
  21. /**
  22. * @expectedException \Twig\Error\RuntimeError
  23. */
  24. public function testFragmentWithError()
  25. {
  26. $renderer = $this->getFragmentHandler($this->throwException(new \Exception('foo')));
  27. $this->renderTemplate($renderer);
  28. }
  29. public function testRenderFragment()
  30. {
  31. $renderer = $this->getFragmentHandler($this->returnValue(new Response('html')));
  32. $response = $this->renderTemplate($renderer);
  33. $this->assertEquals('html', $response);
  34. }
  35. public function testUnknownFragmentRenderer()
  36. {
  37. $context = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\RequestStack')
  38. ->disableOriginalConstructor()
  39. ->getMock()
  40. ;
  41. $renderer = new FragmentHandler($context);
  42. if (method_exists($this, 'expectException')) {
  43. $this->expectException('InvalidArgumentException');
  44. $this->expectExceptionMessage('The "inline" renderer does not exist.');
  45. } else {
  46. $this->setExpectedException('InvalidArgumentException', 'The "inline" renderer does not exist.');
  47. }
  48. $renderer->render('/foo');
  49. }
  50. protected function getFragmentHandler($return)
  51. {
  52. $strategy = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface')->getMock();
  53. $strategy->expects($this->once())->method('getName')->will($this->returnValue('inline'));
  54. $strategy->expects($this->once())->method('render')->will($return);
  55. $context = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\RequestStack')
  56. ->disableOriginalConstructor()
  57. ->getMock()
  58. ;
  59. $context->expects($this->any())->method('getCurrentRequest')->will($this->returnValue(Request::create('/')));
  60. return new FragmentHandler($context, array($strategy), false);
  61. }
  62. protected function renderTemplate(FragmentHandler $renderer, $template = '{{ render("foo") }}')
  63. {
  64. $loader = new ArrayLoader(array('index' => $template));
  65. $twig = new Environment($loader, array('debug' => true, 'cache' => false));
  66. $twig->addExtension(new HttpKernelExtension());
  67. $loader = $this->getMockBuilder('Twig\RuntimeLoader\RuntimeLoaderInterface')->getMock();
  68. $loader->expects($this->any())->method('load')->will($this->returnValueMap(array(
  69. array('Symfony\Bridge\Twig\Extension\HttpKernelRuntime', new HttpKernelRuntime($renderer)),
  70. )));
  71. $twig->addRuntimeLoader($loader);
  72. return $twig->render('index');
  73. }
  74. }