IfTest.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_IfTest extends Twig_Tests_Node_TestCase
  12. {
  13. /**
  14. * @covers Twig_Node_If::__construct
  15. */
  16. public function testConstructor()
  17. {
  18. $t = new Twig_Node(array(
  19. new Twig_Node_Expression_Constant(true, 0),
  20. new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0),
  21. ), array(), 0);
  22. $else = null;
  23. $node = new Twig_Node_If($t, $else, 0);
  24. $this->assertEquals($t, $node->getNode('tests'));
  25. $this->assertEquals(null, $node->getNode('else'));
  26. $else = new Twig_Node_Print(new Twig_Node_Expression_Name('bar', 0), 0);
  27. $node = new Twig_Node_If($t, $else, 0);
  28. $this->assertEquals($else, $node->getNode('else'));
  29. }
  30. /**
  31. * @covers Twig_Node_If::compile
  32. * @dataProvider getTests
  33. */
  34. public function testCompile($node, $source, $environment = null)
  35. {
  36. parent::testCompile($node, $source, $environment);
  37. }
  38. public function getTests()
  39. {
  40. $tests = array();
  41. $t = new Twig_Node(array(
  42. new Twig_Node_Expression_Constant(true, 0),
  43. new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0),
  44. ), array(), 0);
  45. $else = null;
  46. $node = new Twig_Node_If($t, $else, 0);
  47. $tests[] = array($node, <<<EOF
  48. if (true) {
  49. echo {$this->getVariableGetter('foo')};
  50. }
  51. EOF
  52. );
  53. $t = new Twig_Node(array(
  54. new Twig_Node_Expression_Constant(true, 0),
  55. new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0),
  56. new Twig_Node_Expression_Constant(false, 0),
  57. new Twig_Node_Print(new Twig_Node_Expression_Name('bar', 0), 0),
  58. ), array(), 0);
  59. $else = null;
  60. $node = new Twig_Node_If($t, $else, 0);
  61. $tests[] = array($node, <<<EOF
  62. if (true) {
  63. echo {$this->getVariableGetter('foo')};
  64. } elseif (false) {
  65. echo {$this->getVariableGetter('bar')};
  66. }
  67. EOF
  68. );
  69. $t = new Twig_Node(array(
  70. new Twig_Node_Expression_Constant(true, 0),
  71. new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0),
  72. ), array(), 0);
  73. $else = new Twig_Node_Print(new Twig_Node_Expression_Name('bar', 0), 0);
  74. $node = new Twig_Node_If($t, $else, 0);
  75. $tests[] = array($node, <<<EOF
  76. if (true) {
  77. echo {$this->getVariableGetter('foo')};
  78. } else {
  79. echo {$this->getVariableGetter('bar')};
  80. }
  81. EOF
  82. );
  83. return $tests;
  84. }
  85. }