No Description

FragmentHandlerTest.php 2.8KB

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