ModuleTest.php 4.6KB

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