SetTest.php 2.6KB

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