ModuleTest.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. \$context = array_merge(\$this->env->getGlobals(), \$context);
  65. echo "foo";
  66. }
  67. public function getTemplateName()
  68. {
  69. return "foo.twig";
  70. }
  71. public function isTraitable()
  72. {
  73. return false;
  74. }
  75. }
  76. EOF
  77. , $twig);
  78. $import = new Twig_Node_Import(new Twig_Node_Expression_Constant('foo.twig', 0), new Twig_Node_Expression_AssignName('macro', 0), 0);
  79. $body = new Twig_Node(array($import));
  80. $extends = new Twig_Node_Expression_Constant('layout.twig', 0);
  81. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, $filename);
  82. $tests[] = array($node, <<<EOF
  83. <?php
  84. /* foo.twig */
  85. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  86. {
  87. protected \$parent;
  88. public function getParent(array \$context)
  89. {
  90. \$parent = "layout.twig";
  91. if (\$parent instanceof Twig_Template) {
  92. \$name = \$parent->getTemplateName();
  93. \$this->parent[\$name] = \$parent;
  94. \$parent = \$name;
  95. } elseif (!isset(\$this->parent[\$parent])) {
  96. \$this->parent[\$parent] = \$this->env->loadTemplate(\$parent);
  97. }
  98. return \$this->parent[\$parent];
  99. }
  100. protected function doDisplay(array \$context, array \$blocks = array())
  101. {
  102. \$context = array_merge(\$this->env->getGlobals(), \$context);
  103. \$context['macro'] = \$this->env->loadTemplate("foo.twig");
  104. \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
  105. }
  106. public function getTemplateName()
  107. {
  108. return "foo.twig";
  109. }
  110. public function isTraitable()
  111. {
  112. return false;
  113. }
  114. }
  115. EOF
  116. , $twig);
  117. $body = new Twig_Node();
  118. $extends = new Twig_Node_Expression_Conditional(
  119. new Twig_Node_Expression_Constant(true, 0),
  120. new Twig_Node_Expression_Constant('foo', 0),
  121. new Twig_Node_Expression_Constant('foo', 0),
  122. 0
  123. );
  124. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, $filename);
  125. $tests[] = array($node, <<<EOF
  126. <?php
  127. /* foo.twig */
  128. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  129. {
  130. protected \$parent;
  131. public function getParent(array \$context)
  132. {
  133. \$parent = ((true) ? ("foo") : ("foo"));
  134. if (\$parent instanceof Twig_Template) {
  135. \$name = \$parent->getTemplateName();
  136. \$this->parent[\$name] = \$parent;
  137. \$parent = \$name;
  138. } elseif (!isset(\$this->parent[\$parent])) {
  139. \$this->parent[\$parent] = \$this->env->loadTemplate(\$parent);
  140. }
  141. return \$this->parent[\$parent];
  142. }
  143. protected function doDisplay(array \$context, array \$blocks = array())
  144. {
  145. \$context = array_merge(\$this->env->getGlobals(), \$context);
  146. \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
  147. }
  148. public function getTemplateName()
  149. {
  150. return "foo.twig";
  151. }
  152. public function isTraitable()
  153. {
  154. return false;
  155. }
  156. }
  157. EOF
  158. , $twig);
  159. return $tests;
  160. }
  161. }