FragmentHandlerTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\Fragment;
  11. use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class FragmentHandlerTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @expectedException \InvalidArgumentException
  18. */
  19. public function testRenderWhenRendererDoesNotExist()
  20. {
  21. $handler = new FragmentHandler();
  22. $handler->render('/', 'foo');
  23. }
  24. /**
  25. * @expectedException InvalidArgumentException
  26. */
  27. public function testRenderWithUnknownRenderer()
  28. {
  29. $handler = $this->getHandler($this->returnValue(new Response('foo')));
  30. $handler->render('/', 'bar');
  31. }
  32. /**
  33. * @expectedException RuntimeException
  34. * @expectedExceptionMessage Error when rendering "http://localhost/" (Status code is 404).
  35. */
  36. public function testDeliverWithUnsuccessfulResponse()
  37. {
  38. $handler = $this->getHandler($this->returnValue(new Response('foo', 404)));
  39. $handler->render('/', 'foo');
  40. }
  41. public function testRender()
  42. {
  43. $handler = $this->getHandler($this->returnValue(new Response('foo')), array('/', Request::create('/'), array('foo' => 'foo', 'ignore_errors' => true)));
  44. $this->assertEquals('foo', $handler->render('/', 'foo', array('foo' => 'foo')));
  45. }
  46. protected function getHandler($returnValue, $arguments = array())
  47. {
  48. $renderer = $this->getMock('Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface');
  49. $renderer
  50. ->expects($this->any())
  51. ->method('getName')
  52. ->will($this->returnValue('foo'))
  53. ;
  54. $e = $renderer
  55. ->expects($this->any())
  56. ->method('render')
  57. ->will($returnValue)
  58. ;
  59. if ($arguments) {
  60. call_user_func_array(array($e, 'with'), $arguments);
  61. }
  62. $handler = new FragmentHandler();
  63. $handler->addRenderer($renderer);
  64. $handler->setRequest(Request::create('/'));
  65. return $handler;
  66. }
  67. }