GetAttrTest.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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_Expression_GetAttrTest extends Twig_Test_NodeTestCase
  11. {
  12. /**
  13. * @covers Twig_Node_Expression_GetAttr::__construct
  14. */
  15. public function testConstructor()
  16. {
  17. $expr = new Twig_Node_Expression_Name('foo', 1);
  18. $attr = new Twig_Node_Expression_Constant('bar', 1);
  19. $args = new Twig_Node_Expression_Array(array(), 1);
  20. $args->addElement(new Twig_Node_Expression_Name('foo', 1));
  21. $args->addElement(new Twig_Node_Expression_Constant('bar', 1));
  22. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 1);
  23. $this->assertEquals($expr, $node->getNode('node'));
  24. $this->assertEquals($attr, $node->getNode('attribute'));
  25. $this->assertEquals($args, $node->getNode('arguments'));
  26. $this->assertEquals(Twig_TemplateInterface::ARRAY_CALL, $node->getAttribute('type'));
  27. }
  28. /**
  29. * @covers Twig_Node_Expression_GetAttr::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_Name('foo', 1);
  40. $attr = new Twig_Node_Expression_Constant('bar', 1);
  41. $args = new Twig_Node_Expression_Array(array(), 1);
  42. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ANY_CALL, 1);
  43. $tests[] = array($node, sprintf('%s%s, "bar")', $this->getAttributeGetter(), $this->getVariableGetter('foo')));
  44. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 1);
  45. $tests[] = array($node, sprintf('%s%s, "bar", array(), "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo')));
  46. $args = new Twig_Node_Expression_Array(array(), 1);
  47. $args->addElement(new Twig_Node_Expression_Name('foo', 1));
  48. $args->addElement(new Twig_Node_Expression_Constant('bar', 1));
  49. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::METHOD_CALL, 1);
  50. $tests[] = array($node, sprintf('%s%s, "bar", array(0 => %s, 1 => "bar"), "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo'), $this->getVariableGetter('foo')));
  51. return $tests;
  52. }
  53. }