RoutableFragmentRendererTest.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\HttpKernel\Controller\ControllerReference;
  14. use Symfony\Component\HttpKernel\Fragment\RoutableFragmentRenderer;
  15. class RoutableFragmentRendererTest extends TestCase
  16. {
  17. /**
  18. * @dataProvider getGenerateFragmentUriData
  19. */
  20. public function testGenerateFragmentUri($uri, $controller)
  21. {
  22. $this->assertEquals($uri, $this->callGenerateFragmentUriMethod($controller, Request::create('/')));
  23. }
  24. /**
  25. * @dataProvider getGenerateFragmentUriData
  26. */
  27. public function testGenerateAbsoluteFragmentUri($uri, $controller)
  28. {
  29. $this->assertEquals('http://localhost'.$uri, $this->callGenerateFragmentUriMethod($controller, Request::create('/'), true));
  30. }
  31. public static function getGenerateFragmentUriData()
  32. {
  33. return [
  34. ['/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', [], [])],
  35. ['/_fragment?_path=_format%3Dxml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', ['_format' => 'xml'], [])],
  36. ['/_fragment?_path=foo%3Dfoo%26_format%3Djson%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', ['foo' => 'foo', '_format' => 'json'], [])],
  37. ['/_fragment?bar=bar&_path=foo%3Dfoo%26_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', ['foo' => 'foo'], ['bar' => 'bar'])],
  38. ['/_fragment?foo=foo&_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', [], ['foo' => 'foo'])],
  39. ['/_fragment?_path=foo%255B0%255D%3Dfoo%26foo%255B1%255D%3Dbar%26_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', ['foo' => ['foo', 'bar']], [])],
  40. ];
  41. }
  42. public function testGenerateFragmentUriWithARequest()
  43. {
  44. $request = Request::create('/');
  45. $request->attributes->set('_format', 'json');
  46. $request->setLocale('fr');
  47. $controller = new ControllerReference('controller', [], []);
  48. $this->assertEquals('/_fragment?_path=_format%3Djson%26_locale%3Dfr%26_controller%3Dcontroller', $this->callGenerateFragmentUriMethod($controller, $request));
  49. }
  50. /**
  51. * @dataProvider getGenerateFragmentUriDataWithNonScalar
  52. */
  53. public function testGenerateFragmentUriWithNonScalar($controller)
  54. {
  55. $this->expectException(\LogicException::class);
  56. $this->callGenerateFragmentUriMethod($controller, Request::create('/'));
  57. }
  58. public static function getGenerateFragmentUriDataWithNonScalar()
  59. {
  60. return [
  61. [new ControllerReference('controller', ['foo' => new Foo(), 'bar' => 'bar'], [])],
  62. [new ControllerReference('controller', ['foo' => ['foo' => 'foo'], 'bar' => ['bar' => new Foo()]], [])],
  63. ];
  64. }
  65. private function callGenerateFragmentUriMethod(ControllerReference $reference, Request $request, $absolute = false)
  66. {
  67. $renderer = $this->getMockForAbstractClass(RoutableFragmentRenderer::class);
  68. $r = new \ReflectionObject($renderer);
  69. $m = $r->getMethod('generateFragmentUri');
  70. $m->setAccessible(true);
  71. return $m->invoke($renderer, $reference, $request, $absolute);
  72. }
  73. }
  74. class Foo
  75. {
  76. public $foo;
  77. public function getFoo()
  78. {
  79. return $this->foo;
  80. }
  81. }