ModuleTest.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. class Twig_Tests_Node_ModuleTest extends Twig_Test_NodeTestCase
  11. {
  12. /**
  13. * @covers Twig_Node_Module::__construct
  14. */
  15. public function testConstructor()
  16. {
  17. $body = new Twig_Node_Text('foo', 1);
  18. $parent = new Twig_Node_Expression_Constant('layout.twig', 1);
  19. $blocks = new Twig_Node();
  20. $macros = new Twig_Node();
  21. $traits = new Twig_Node();
  22. $filename = 'foo.twig';
  23. $node = new Twig_Node_Module($body, $parent, $blocks, $macros, $traits, new Twig_Node(array()), $filename);
  24. $this->assertEquals($body, $node->getNode('body'));
  25. $this->assertEquals($blocks, $node->getNode('blocks'));
  26. $this->assertEquals($macros, $node->getNode('macros'));
  27. $this->assertEquals($parent, $node->getNode('parent'));
  28. $this->assertEquals($filename, $node->getAttribute('filename'));
  29. }
  30. /**
  31. * @covers Twig_Node_Module::compile
  32. * @covers Twig_Node_Module::compileTemplate
  33. * @covers Twig_Node_Module::compileMacros
  34. * @covers Twig_Node_Module::compileClassHeader
  35. * @covers Twig_Node_Module::compileDisplayHeader
  36. * @covers Twig_Node_Module::compileDisplayBody
  37. * @covers Twig_Node_Module::compileDisplayFooter
  38. * @covers Twig_Node_Module::compileClassFooter
  39. * @dataProvider getTests
  40. */
  41. public function testCompile($node, $source, $environment = null)
  42. {
  43. parent::testCompile($node, $source, $environment);
  44. }
  45. public function getTests()
  46. {
  47. $twig = new Twig_Environment(new Twig_Loader_String());
  48. $tests = array();
  49. $body = new Twig_Node_Text('foo', 1);
  50. $extends = null;
  51. $blocks = new Twig_Node();
  52. $macros = new Twig_Node();
  53. $traits = new Twig_Node();
  54. $filename = 'foo.twig';
  55. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, new Twig_Node(array()), $filename);
  56. $tests[] = array($node, <<<EOF
  57. <?php
  58. /* foo.twig */
  59. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  60. {
  61. public function __construct(Twig_Environment \$env)
  62. {
  63. parent::__construct(\$env);
  64. \$this->parent = false;
  65. \$this->blocks = array(
  66. );
  67. }
  68. protected function doDisplay(array \$context, array \$blocks = array())
  69. {
  70. // line 1
  71. echo "foo";
  72. }
  73. public function getTemplateName()
  74. {
  75. return "foo.twig";
  76. }
  77. public function getDebugInfo()
  78. {
  79. return array ( 17 => 1,);
  80. }
  81. }
  82. EOF
  83. , $twig);
  84. $import = new Twig_Node_Import(new Twig_Node_Expression_Constant('foo.twig', 1), new Twig_Node_Expression_AssignName('macro', 1), 1);
  85. $body = new Twig_Node(array($import));
  86. $extends = new Twig_Node_Expression_Constant('layout.twig', 1);
  87. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, new Twig_Node(array()), $filename);
  88. $tests[] = array($node, <<<EOF
  89. <?php
  90. /* foo.twig */
  91. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  92. {
  93. public function __construct(Twig_Environment \$env)
  94. {
  95. parent::__construct(\$env);
  96. \$this->parent = \$this->env->loadTemplate("layout.twig");
  97. \$this->blocks = array(
  98. );
  99. }
  100. protected function doGetParent(array \$context)
  101. {
  102. return "layout.twig";
  103. }
  104. protected function doDisplay(array \$context, array \$blocks = array())
  105. {
  106. // line 1
  107. \$context["macro"] = \$this->env->loadTemplate("foo.twig");
  108. \$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks));
  109. }
  110. public function getTemplateName()
  111. {
  112. return "foo.twig";
  113. }
  114. public function isTraitable()
  115. {
  116. return false;
  117. }
  118. public function getDebugInfo()
  119. {
  120. return array ( 22 => 1,);
  121. }
  122. }
  123. EOF
  124. , $twig);
  125. $body = new Twig_Node();
  126. $extends = new Twig_Node_Expression_Conditional(
  127. new Twig_Node_Expression_Constant(true, 1),
  128. new Twig_Node_Expression_Constant('foo', 1),
  129. new Twig_Node_Expression_Constant('foo', 1),
  130. 0
  131. );
  132. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, new Twig_Node(array()), $filename);
  133. $tests[] = array($node, <<<EOF
  134. <?php
  135. /* foo.twig */
  136. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  137. {
  138. protected function doGetParent(array \$context)
  139. {
  140. return \$this->env->resolveTemplate(((true) ? ("foo") : ("foo")));
  141. }
  142. protected function doDisplay(array \$context, array \$blocks = array())
  143. {
  144. \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
  145. }
  146. public function getTemplateName()
  147. {
  148. return "foo.twig";
  149. }
  150. public function isTraitable()
  151. {
  152. return false;
  153. }
  154. public function getDebugInfo()
  155. {
  156. return array ();
  157. }
  158. }
  159. EOF
  160. , $twig);
  161. return $tests;
  162. }
  163. }