RoutingExtensionTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\Bridge\Twig\Tests\Extension;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\Extension\RoutingExtension;
  13. use Twig\Environment;
  14. use Twig\Node\Expression\FilterExpression;
  15. use Twig\Source;
  16. class RoutingExtensionTest extends TestCase
  17. {
  18. /**
  19. * @dataProvider getEscapingTemplates
  20. */
  21. public function testEscaping($template, $mustBeEscaped)
  22. {
  23. $twig = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock(), array('debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0));
  24. $twig->addExtension(new RoutingExtension($this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock()));
  25. $nodes = $twig->parse($twig->tokenize(new Source($template, '')));
  26. $this->assertSame($mustBeEscaped, $nodes->getNode('body')->getNode(0)->getNode('expr') instanceof FilterExpression);
  27. }
  28. public function getEscapingTemplates()
  29. {
  30. return array(
  31. array('{{ path("foo") }}', false),
  32. array('{{ path("foo", {}) }}', false),
  33. array('{{ path("foo", { foo: "foo" }) }}', false),
  34. array('{{ path("foo", foo) }}', true),
  35. array('{{ path("foo", { foo: foo }) }}', true),
  36. array('{{ path("foo", { foo: ["foo", "bar"] }) }}', true),
  37. array('{{ path("foo", { foo: "foo", bar: "bar" }) }}', true),
  38. array('{{ path(name = "foo", parameters = {}) }}', false),
  39. array('{{ path(name = "foo", parameters = { foo: "foo" }) }}', false),
  40. array('{{ path(name = "foo", parameters = foo) }}', true),
  41. array('{{ path(name = "foo", parameters = { foo: ["foo", "bar"] }) }}', true),
  42. array('{{ path(name = "foo", parameters = { foo: foo }) }}', true),
  43. array('{{ path(name = "foo", parameters = { foo: "foo", bar: "bar" }) }}', true),
  44. );
  45. }
  46. }