RoutableFragmentRendererTest.php 3.7 KB

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