FragmentHandlerTest.php 2.8 KB

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