ModuleTest.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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, new Twig_Node(array()), $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, new Twig_Node(array()), $filename);
  57. $tests[] = array($node, <<<EOF
  58. <?php
  59. /* foo.twig */
  60. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  61. {
  62. public function __construct(Twig_Environment \$env)
  63. {
  64. parent::__construct(\$env);
  65. \$this->parent = false;
  66. \$this->blocks = array(
  67. );
  68. }
  69. protected function doDisplay(array \$context, array \$blocks = array())
  70. {
  71. echo "foo";
  72. }
  73. public function getTemplateName()
  74. {
  75. return "foo.twig";
  76. }
  77. public function getDebugInfo()
  78. {
  79. return array ();
  80. }
  81. }
  82. EOF
  83. , $twig);
  84. $import = new Twig_Node_Import(new Twig_Node_Expression_Constant('foo.twig', 0), new Twig_Node_Expression_AssignName('macro', 0), 0);
  85. $body = new Twig_Node(array($import));
  86. $extends = new Twig_Node_Expression_Constant('layout.twig', 0);
  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. \$context["macro"] = \$this->env->loadTemplate("foo.twig");
  107. \$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks));
  108. }
  109. public function getTemplateName()
  110. {
  111. return "foo.twig";
  112. }
  113. public function isTraitable()
  114. {
  115. return false;
  116. }
  117. public function getDebugInfo()
  118. {
  119. return array ();
  120. }
  121. }
  122. EOF
  123. , $twig);
  124. $body = new Twig_Node();
  125. $extends = new Twig_Node_Expression_Conditional(
  126. new Twig_Node_Expression_Constant(true, 0),
  127. new Twig_Node_Expression_Constant('foo', 0),
  128. new Twig_Node_Expression_Constant('foo', 0),
  129. 0
  130. );
  131. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, new Twig_Node(array()), $filename);
  132. $tests[] = array($node, <<<EOF
  133. <?php
  134. /* foo.twig */
  135. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  136. {
  137. protected function doGetParent(array \$context)
  138. {
  139. return \$this->env->resolveTemplate(((true) ? ("foo") : ("foo")));
  140. }
  141. protected function doDisplay(array \$context, array \$blocks = array())
  142. {
  143. \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
  144. }
  145. public function getTemplateName()
  146. {
  147. return "foo.twig";
  148. }
  149. public function isTraitable()
  150. {
  151. return false;
  152. }
  153. public function getDebugInfo()
  154. {
  155. return array ();
  156. }
  157. }
  158. EOF
  159. , $twig);
  160. return $tests;
  161. }
  162. }