RoutableFragmentRendererTest.php 3.8 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. 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 array(
  33. array('/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array(), array())),
  34. array('/_fragment?_path=_format%3Dxml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('_format' => 'xml'), array())),
  35. array('/_fragment?_path=foo%3Dfoo%26_format%3Djson%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo', '_format' => 'json'), array())),
  36. array('/_fragment?bar=bar&_path=foo%3Dfoo%26_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo'), array('bar' => 'bar'))),
  37. array('/_fragment?foo=foo&_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array(), array('foo' => 'foo'))),
  38. array('/_fragment?_path=foo%255B0%255D%3Dfoo%26foo%255B1%255D%3Dbar%26_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => array('foo', 'bar')), array())),
  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', array(), array());
  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 array(
  60. array(new ControllerReference('controller', array('foo' => new Foo(), 'bar' => 'bar'), array())),
  61. array(new ControllerReference('controller', array('foo' => array('foo' => 'foo'), 'bar' => array('bar' => new Foo())), array())),
  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. }