Keine Beschreibung

RoutableFragmentRendererTest.php 3.8KB

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