FormThemeTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\Node;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\Node\FormThemeNode;
  13. use Twig\Compiler;
  14. use Twig\Environment;
  15. use Twig\Node\Expression\ArrayExpression;
  16. use Twig\Node\Expression\ConstantExpression;
  17. use Twig\Node\Expression\NameExpression;
  18. use Twig\Node\Node;
  19. class FormThemeTest extends TestCase
  20. {
  21. public function testConstructor()
  22. {
  23. $form = new NameExpression('form', 0);
  24. $resources = new Node(array(
  25. new ConstantExpression('tpl1', 0),
  26. new ConstantExpression('tpl2', 0),
  27. ));
  28. $node = new FormThemeNode($form, $resources, 0);
  29. $this->assertEquals($form, $node->getNode('form'));
  30. $this->assertEquals($resources, $node->getNode('resources'));
  31. }
  32. public function testCompile()
  33. {
  34. $form = new NameExpression('form', 0);
  35. $resources = new ArrayExpression(array(
  36. new ConstantExpression(0, 0),
  37. new ConstantExpression('tpl1', 0),
  38. new ConstantExpression(1, 0),
  39. new ConstantExpression('tpl2', 0),
  40. ), 0);
  41. $node = new FormThemeNode($form, $resources, 0);
  42. $compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
  43. $this->assertEquals(
  44. sprintf(
  45. '$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"));',
  46. $this->getVariableGetter('form')
  47. ),
  48. trim($compiler->compile($node)->getSource())
  49. );
  50. $resources = new ConstantExpression('tpl1', 0);
  51. $node = new FormThemeNode($form, $resources, 0);
  52. $this->assertEquals(
  53. sprintf(
  54. '$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->setTheme(%s, "tpl1");',
  55. $this->getVariableGetter('form')
  56. ),
  57. trim($compiler->compile($node)->getSource())
  58. );
  59. }
  60. protected function getVariableGetter($name)
  61. {
  62. if (\PHP_VERSION_ID >= 70000) {
  63. return sprintf('($context["%s"] ?? null)', $name, $name);
  64. }
  65. return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
  66. }
  67. }