RoutableFragmentRendererTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\Fragment\Tests\FragmentRenderer;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  13. use Symfony\Component\HttpKernel\Fragment\RoutableFragmentRenderer;
  14. class RoutableFragmentRendererTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @dataProvider getGenerateFragmentUriData
  18. */
  19. public function testGenerateFragmentUri($uri, $controller)
  20. {
  21. $this->assertEquals($uri, $this->getRenderer()->doGenerateFragmentUri($controller, Request::create('/')));
  22. }
  23. /**
  24. * @dataProvider getGenerateFragmentUriData
  25. */
  26. public function testGenerateAbsoluteFragmentUri($uri, $controller)
  27. {
  28. $this->assertEquals('http://localhost'.$uri, $this->getRenderer()->doGenerateFragmentUri($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. );
  39. }
  40. public function testGenerateFragmentUriWithARequest()
  41. {
  42. $request = Request::create('/');
  43. $request->attributes->set('_format', 'json');
  44. $request->setLocale('fr');
  45. $controller = new ControllerReference('controller', array(), array());
  46. $this->assertEquals('/_fragment?_path=_format%3Djson%26_locale%3Dfr%26_controller%3Dcontroller', $this->getRenderer()->doGenerateFragmentUri($controller, $request));
  47. }
  48. private function getRenderer()
  49. {
  50. return new Renderer();
  51. }
  52. }
  53. class Renderer extends RoutableFragmentRenderer
  54. {
  55. public function render($uri, Request $request, array $options = array()) {}
  56. public function getName() {}
  57. public function doGenerateFragmentUri(ControllerReference $reference, Request $request, $absolute = false)
  58. {
  59. return parent::generateFragmentUri($reference, $request, $absolute);
  60. }
  61. }