SetTest.php 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_SetTest extends Twig_Test_NodeTestCase
  11. {
  12. /**
  13. * @covers Twig_Node_Set::__construct
  14. */
  15. public function testConstructor()
  16. {
  17. $names = new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 1)), array(), 1);
  18. $values = new Twig_Node(array(new Twig_Node_Expression_Constant('foo', 1)), array(), 1);
  19. $node = new Twig_Node_Set(false, $names, $values, 1);
  20. $this->assertEquals($names, $node->getNode('names'));
  21. $this->assertEquals($values, $node->getNode('values'));
  22. $this->assertEquals(false, $node->getAttribute('capture'));
  23. }
  24. /**
  25. * @covers Twig_Node_Set::compile
  26. * @dataProvider getTests
  27. */
  28. public function testCompile($node, $source, $environment = null)
  29. {
  30. parent::testCompile($node, $source, $environment);
  31. }
  32. public function getTests()
  33. {
  34. $tests = array();
  35. $names = new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 1)), array(), 1);
  36. $values = new Twig_Node(array(new Twig_Node_Expression_Constant('foo', 1)), array(), 1);
  37. $node = new Twig_Node_Set(false, $names, $values, 1);
  38. $tests[] = array($node, <<<EOF
  39. // line 1
  40. \$context["foo"] = "foo";
  41. EOF
  42. );
  43. $names = new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 1)), array(), 1);
  44. $values = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Constant('foo', 1), 1)), array(), 1);
  45. $node = new Twig_Node_Set(true, $names, $values, 1);
  46. $tests[] = array($node, <<<EOF
  47. // line 1
  48. ob_start();
  49. echo "foo";
  50. \$context["foo"] = ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());
  51. EOF
  52. );
  53. $names = new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 1)), array(), 1);
  54. $values = new Twig_Node_Text('foo', 1);
  55. $node = new Twig_Node_Set(true, $names, $values, 1);
  56. $tests[] = array($node, <<<EOF
  57. // line 1
  58. \$context["foo"] = ('' === \$tmp = "foo") ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());
  59. EOF
  60. );
  61. $names = new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 1), new Twig_Node_Expression_AssignName('bar', 1)), array(), 1);
  62. $values = new Twig_Node(array(new Twig_Node_Expression_Constant('foo', 1), new Twig_Node_Expression_Name('bar', 1)), array(), 1);
  63. $node = new Twig_Node_Set(false, $names, $values, 1);
  64. $tests[] = array($node, <<<EOF
  65. // line 1
  66. list(\$context["foo"], \$context["bar"]) = array("foo", {$this->getVariableGetter('bar')});
  67. EOF
  68. );
  69. return $tests;
  70. }
  71. }