IncludeTest.php 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_IncludeTest extends Twig_Test_NodeTestCase
  11. {
  12. /**
  13. * @covers Twig_Node_Include::__construct
  14. */
  15. public function testConstructor()
  16. {
  17. $expr = new Twig_Node_Expression_Constant('foo.twig', 1);
  18. $node = new Twig_Node_Include($expr, null, false, false, 1);
  19. $this->assertEquals(null, $node->getNode('variables'));
  20. $this->assertEquals($expr, $node->getNode('expr'));
  21. $this->assertFalse($node->getAttribute('only'));
  22. $vars = new Twig_Node_Expression_Array(array(new Twig_Node_Expression_Constant('foo', 1), new Twig_Node_Expression_Constant(true, 1)), 1);
  23. $node = new Twig_Node_Include($expr, $vars, true, false, 1);
  24. $this->assertEquals($vars, $node->getNode('variables'));
  25. $this->assertTrue($node->getAttribute('only'));
  26. }
  27. /**
  28. * @covers Twig_Node_Include::compile
  29. * @dataProvider getTests
  30. */
  31. public function testCompile($node, $source, $environment = null)
  32. {
  33. parent::testCompile($node, $source, $environment);
  34. }
  35. public function getTests()
  36. {
  37. $tests = array();
  38. $expr = new Twig_Node_Expression_Constant('foo.twig', 1);
  39. $node = new Twig_Node_Include($expr, null, false, false, 1);
  40. $tests[] = array($node, <<<EOF
  41. // line 1
  42. \$this->env->loadTemplate("foo.twig")->display(\$context);
  43. EOF
  44. );
  45. $expr = new Twig_Node_Expression_Conditional(
  46. new Twig_Node_Expression_Constant(true, 1),
  47. new Twig_Node_Expression_Constant('foo', 1),
  48. new Twig_Node_Expression_Constant('foo', 1),
  49. 0
  50. );
  51. $node = new Twig_Node_Include($expr, null, false, false, 1);
  52. $tests[] = array($node, <<<EOF
  53. // line 1
  54. \$template = \$this->env->resolveTemplate(((true) ? ("foo") : ("foo")));
  55. \$template->display(\$context);
  56. EOF
  57. );
  58. $expr = new Twig_Node_Expression_Constant('foo.twig', 1);
  59. $vars = new Twig_Node_Expression_Array(array(new Twig_Node_Expression_Constant('foo', 1), new Twig_Node_Expression_Constant(true, 1)), 1);
  60. $node = new Twig_Node_Include($expr, $vars, false, false, 1);
  61. $tests[] = array($node, <<<EOF
  62. // line 1
  63. \$this->env->loadTemplate("foo.twig")->display(array_merge(\$context, array("foo" => true)));
  64. EOF
  65. );
  66. $node = new Twig_Node_Include($expr, $vars, true, false, 1);
  67. $tests[] = array($node, <<<EOF
  68. // line 1
  69. \$this->env->loadTemplate("foo.twig")->display(array("foo" => true));
  70. EOF
  71. );
  72. $node = new Twig_Node_Include($expr, $vars, true, true, 1);
  73. $tests[] = array($node, <<<EOF
  74. // line 1
  75. try {
  76. \$this->env->loadTemplate("foo.twig")->display(array("foo" => true));
  77. } catch (Twig_Error_Loader \$e) {
  78. // ignore missing template
  79. }
  80. EOF
  81. );
  82. return $tests;
  83. }
  84. }