ModuleTest.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require_once dirname(__FILE__).'/TestCase.php';
  11. class Twig_Tests_Node_ModuleTest extends Twig_Tests_Node_TestCase
  12. {
  13. /**
  14. * @covers Twig_Node_Module::__construct
  15. */
  16. public function testConstructor()
  17. {
  18. $body = new Twig_Node_Text('foo', 0);
  19. $parent = new Twig_Node_Expression_Constant('layout.twig', 0);
  20. $blocks = new Twig_Node();
  21. $macros = new Twig_Node();
  22. $traits = new Twig_Node();
  23. $filename = 'foo.twig';
  24. $node = new Twig_Node_Module($body, $parent, $blocks, $macros, $traits, $filename);
  25. $this->assertEquals($body, $node->getNode('body'));
  26. $this->assertEquals($blocks, $node->getNode('blocks'));
  27. $this->assertEquals($macros, $node->getNode('macros'));
  28. $this->assertEquals($parent, $node->getNode('parent'));
  29. $this->assertEquals($filename, $node->getAttribute('filename'));
  30. }
  31. /**
  32. * @covers Twig_Node_Module::compile
  33. * @covers Twig_Node_Module::compileTemplate
  34. * @covers Twig_Node_Module::compileMacros
  35. * @covers Twig_Node_Module::compileClassHeader
  36. * @covers Twig_Node_Module::compileDisplayHeader
  37. * @covers Twig_Node_Module::compileDisplayBody
  38. * @covers Twig_Node_Module::compileDisplayFooter
  39. * @covers Twig_Node_Module::compileClassFooter
  40. * @dataProvider getTests
  41. */
  42. public function testCompile($node, $source, $environment = null)
  43. {
  44. parent::testCompile($node, $source, $environment);
  45. }
  46. public function getTests()
  47. {
  48. $twig = new Twig_Environment(new Twig_Loader_String());
  49. $tests = array();
  50. $body = new Twig_Node_Text('foo', 0);
  51. $extends = null;
  52. $blocks = new Twig_Node();
  53. $macros = new Twig_Node();
  54. $traits = new Twig_Node();
  55. $filename = 'foo.twig';
  56. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, $filename);
  57. $tests[] = array($node, <<<EOF
  58. <?php
  59. /* foo.twig */
  60. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  61. {
  62. protected function doDisplay(array \$context, array \$blocks = array())
  63. {
  64. echo "foo";
  65. }
  66. public function getTemplateName()
  67. {
  68. return "foo.twig";
  69. }
  70. }
  71. EOF
  72. , $twig);
  73. $import = new Twig_Node_Import(new Twig_Node_Expression_Constant('foo.twig', 0), new Twig_Node_Expression_AssignName('macro', 0), 0);
  74. $body = new Twig_Node(array($import));
  75. $extends = new Twig_Node_Expression_Constant('layout.twig', 0);
  76. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, $filename);
  77. $tests[] = array($node, <<<EOF
  78. <?php
  79. /* foo.twig */
  80. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  81. {
  82. protected function doGetParent(array \$context)
  83. {
  84. return "layout.twig";
  85. }
  86. protected function doDisplay(array \$context, array \$blocks = array())
  87. {
  88. \$context["macro"] = \$this->env->loadTemplate("foo.twig");
  89. \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
  90. }
  91. public function getTemplateName()
  92. {
  93. return "foo.twig";
  94. }
  95. public function isTraitable()
  96. {
  97. return false;
  98. }
  99. }
  100. EOF
  101. , $twig);
  102. $body = new Twig_Node();
  103. $extends = new Twig_Node_Expression_Conditional(
  104. new Twig_Node_Expression_Constant(true, 0),
  105. new Twig_Node_Expression_Constant('foo', 0),
  106. new Twig_Node_Expression_Constant('foo', 0),
  107. 0
  108. );
  109. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, $filename);
  110. $tests[] = array($node, <<<EOF
  111. <?php
  112. /* foo.twig */
  113. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  114. {
  115. protected function doGetParent(array \$context)
  116. {
  117. return \$this->env->resolveTemplate(((true) ? ("foo") : ("foo")));
  118. }
  119. protected function doDisplay(array \$context, array \$blocks = array())
  120. {
  121. \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
  122. }
  123. public function getTemplateName()
  124. {
  125. return "foo.twig";
  126. }
  127. public function isTraitable()
  128. {
  129. return false;
  130. }
  131. }
  132. EOF
  133. , $twig);
  134. return $tests;
  135. }
  136. }