IncludeTest.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_IncludeTest extends Twig_Tests_Node_TestCase
  12. {
  13. /**
  14. * @covers Twig_Node_Include::__construct
  15. */
  16. public function testConstructor()
  17. {
  18. $expr = new Twig_Node_Expression_Constant('foo.twig', 0);
  19. $node = new Twig_Node_Include($expr, null, false, 0);
  20. $this->assertEquals(null, $node->getNode('variables'));
  21. $this->assertEquals($expr, $node->getNode('expr'));
  22. $this->assertFalse($node->getAttribute('only'));
  23. $vars = new Twig_Node_Expression_Array(array('foo' => new Twig_Node_Expression_Constant(true, 0)), 0);
  24. $node = new Twig_Node_Include($expr, $vars, true, 0);
  25. $this->assertEquals($vars, $node->getNode('variables'));
  26. $this->assertTrue($node->getAttribute('only'));
  27. }
  28. /**
  29. * @covers Twig_Node_Include::compile
  30. * @dataProvider getTests
  31. */
  32. public function testCompile($node, $source, $environment = null)
  33. {
  34. parent::testCompile($node, $source, $environment);
  35. }
  36. public function getTests()
  37. {
  38. $tests = array();
  39. $expr = new Twig_Node_Expression_Constant('foo.twig', 0);
  40. $node = new Twig_Node_Include($expr, null, false, 0);
  41. $tests[] = array($node, '$this->env->loadTemplate("foo.twig")->display($context);');
  42. $expr = new Twig_Node_Expression_Conditional(
  43. new Twig_Node_Expression_Constant(true, 0),
  44. new Twig_Node_Expression_Constant('foo', 0),
  45. new Twig_Node_Expression_Constant('foo', 0),
  46. 0
  47. );
  48. $node = new Twig_Node_Include($expr, null, false, 0);
  49. $tests[] = array($node, <<<EOF
  50. \$template = ((true) ? ("foo") : ("foo"));
  51. if (!\$template instanceof Twig_Template) {
  52. \$template = \$this->env->loadTemplate(\$template);
  53. }
  54. \$template->display(\$context);
  55. EOF
  56. );
  57. $expr = new Twig_Node_Expression_Constant('foo.twig', 0);
  58. $vars = new Twig_Node_Expression_Array(array('foo' => new Twig_Node_Expression_Constant(true, 0)), 0);
  59. $node = new Twig_Node_Include($expr, $vars, false, 0);
  60. $tests[] = array($node, '$this->env->loadTemplate("foo.twig")->display(array_merge($context, array("foo" => true)));');
  61. $node = new Twig_Node_Include($expr, $vars, true, 0);
  62. $tests[] = array($node, '$this->env->loadTemplate("foo.twig")->display(array("foo" => true));');
  63. return $tests;
  64. }
  65. }