HttpKernelExtensionTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 Symfony\Bridge\Twig\Extension\HttpKernelExtension;
  12. use Symfony\Bridge\Twig\Tests\TestCase;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
  15. class HttpKernelExtensionTest extends TestCase
  16. {
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
  21. $this->markTestSkipped('The "HttpKernel" component is not available');
  22. }
  23. if (!class_exists('Twig_Environment')) {
  24. $this->markTestSkipped('Twig is not available.');
  25. }
  26. }
  27. /**
  28. * @expectedException \Twig_Error_Runtime
  29. */
  30. public function testFragmentWithError()
  31. {
  32. $kernel = $this->getFragmentHandler($this->throwException(new \Exception('foo')));
  33. $loader = new \Twig_Loader_Array(array('index' => '{{ fragment("foo") }}'));
  34. $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  35. $twig->addExtension(new HttpKernelExtension($kernel));
  36. $this->renderTemplate($kernel);
  37. }
  38. protected function getFragmentHandler($return)
  39. {
  40. $strategy = $this->getMock('Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface');
  41. $strategy->expects($this->once())->method('getName')->will($this->returnValue('inline'));
  42. $strategy->expects($this->once())->method('render')->will($return);
  43. $renderer = new FragmentHandler(array($strategy));
  44. $renderer->setRequest(Request::create('/'));
  45. return $renderer;
  46. }
  47. protected function renderTemplate(FragmentHandler $renderer, $template = '{{ render("foo") }}')
  48. {
  49. $loader = new \Twig_Loader_Array(array('index' => $template));
  50. $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  51. $twig->addExtension(new HttpKernelExtension($renderer));
  52. return $twig->render('index');
  53. }
  54. }