FragmentHandlerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Exception\HttpException;
  16. use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
  17. use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
  18. class FragmentHandlerTest extends TestCase
  19. {
  20. private $requestStack;
  21. protected function setUp(): void
  22. {
  23. $this->requestStack = $this->createMock(RequestStack::class);
  24. $this->requestStack
  25. ->expects($this->any())
  26. ->method('getCurrentRequest')
  27. ->willReturn(Request::create('/'))
  28. ;
  29. }
  30. public function testRenderWhenRendererDoesNotExist()
  31. {
  32. $this->expectException(\InvalidArgumentException::class);
  33. $handler = new FragmentHandler($this->requestStack);
  34. $handler->render('/', 'foo');
  35. }
  36. public function testRenderWithUnknownRenderer()
  37. {
  38. $this->expectException(\InvalidArgumentException::class);
  39. $handler = $this->getHandler($this->returnValue(new Response('foo')));
  40. $handler->render('/', 'bar');
  41. }
  42. public function testDeliverWithUnsuccessfulResponse()
  43. {
  44. $handler = $this->getHandler($this->returnValue(new Response('foo', 404)));
  45. try {
  46. $handler->render('/', 'foo');
  47. $this->fail('->render() throws a \RuntimeException exception if response is not successful');
  48. } catch (\Exception $e) {
  49. $this->assertInstanceOf(\RuntimeException::class, $e);
  50. $this->assertEquals(0, $e->getCode());
  51. $this->assertEquals('Error when rendering "http://localhost/" (Status code is 404).', $e->getMessage());
  52. $previousException = $e->getPrevious();
  53. $this->assertInstanceOf(HttpException::class, $previousException);
  54. $this->assertEquals(404, $previousException->getStatusCode());
  55. $this->assertEquals(0, $previousException->getCode());
  56. }
  57. }
  58. public function testRender()
  59. {
  60. $expectedRequest = Request::create('/');
  61. $handler = $this->getHandler(
  62. $this->returnValue(new Response('foo')),
  63. [
  64. '/',
  65. $this->callback(function (Request $request) use ($expectedRequest) {
  66. $expectedRequest->server->remove('REQUEST_TIME_FLOAT');
  67. $request->server->remove('REQUEST_TIME_FLOAT');
  68. return $expectedRequest == $request;
  69. }),
  70. ['foo' => 'foo', 'ignore_errors' => true],
  71. ]
  72. );
  73. $this->assertEquals('foo', $handler->render('/', 'foo', ['foo' => 'foo']));
  74. }
  75. protected function getHandler($returnValue, $arguments = [])
  76. {
  77. $renderer = $this->createMock(FragmentRendererInterface::class);
  78. $renderer
  79. ->expects($this->any())
  80. ->method('getName')
  81. ->willReturn('foo')
  82. ;
  83. $e = $renderer
  84. ->expects($this->any())
  85. ->method('render')
  86. ->will($returnValue)
  87. ;
  88. if ($arguments) {
  89. $e->with(...$arguments);
  90. }
  91. $handler = new FragmentHandler($this->requestStack);
  92. $handler->addRenderer($renderer);
  93. return $handler;
  94. }
  95. }