IncludeTest.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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, 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, false, 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, 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, false, 0);
  49. $tests[] = array($node, <<<EOF
  50. \$template = \$this->env->resolveTemplate(((true) ? ("foo") : ("foo")));
  51. \$template->display(\$context);
  52. EOF
  53. );
  54. $expr = new Twig_Node_Expression_Constant('foo.twig', 0);
  55. $vars = new Twig_Node_Expression_Array(array('foo' => new Twig_Node_Expression_Constant(true, 0)), 0);
  56. $node = new Twig_Node_Include($expr, $vars, false, false, 0);
  57. $tests[] = array($node, '$this->env->loadTemplate("foo.twig")->display(array_merge($context, array("foo" => true)));');
  58. $node = new Twig_Node_Include($expr, $vars, true, false, 0);
  59. $tests[] = array($node, '$this->env->loadTemplate("foo.twig")->display(array("foo" => true));');
  60. $node = new Twig_Node_Include($expr, $vars, true, true, 0);
  61. $tests[] = array($node, <<<EOF
  62. try {
  63. \$this->env->loadTemplate("foo.twig")->display(array("foo" => true));
  64. } catch (Twig_Error_Loader \$e) {
  65. // ignore missing template
  66. }
  67. EOF
  68. );
  69. return $tests;
  70. }
  71. }