GetAttrTest.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_Expression_GetAttrTest extends Twig_Tests_Node_TestCase
  12. {
  13. /**
  14. * @covers Twig_Node_Expression_GetAttr::__construct
  15. */
  16. public function testConstructor()
  17. {
  18. $expr = new Twig_Node_Expression_Name('foo', 0);
  19. $attr = new Twig_Node_Expression_Constant('bar', 0);
  20. $args = new Twig_Node_Expression_Array(array(), 0);
  21. $args->addElement(new Twig_Node_Expression_Name('foo', 0));
  22. $args->addElement(new Twig_Node_Expression_Constant('bar', 0));
  23. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 0);
  24. $this->assertEquals($expr, $node->getNode('node'));
  25. $this->assertEquals($attr, $node->getNode('attribute'));
  26. $this->assertEquals($args, $node->getNode('arguments'));
  27. $this->assertEquals(Twig_TemplateInterface::ARRAY_CALL, $node->getAttribute('type'));
  28. }
  29. /**
  30. * @covers Twig_Node_Expression_GetAttr::compile
  31. * @dataProvider getTests
  32. */
  33. public function testCompile($node, $source, $environment = null)
  34. {
  35. parent::testCompile($node, $source, $environment);
  36. }
  37. public function getTests()
  38. {
  39. $tests = array();
  40. $expr = new Twig_Node_Expression_Name('foo', 0);
  41. $attr = new Twig_Node_Expression_Constant('bar', 0);
  42. $args = new Twig_Node_Expression_Array(array(), 0);
  43. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ANY_CALL, 0);
  44. $tests[] = array($node, sprintf('%s%s, "bar")', $this->getAttributeGetter(), $this->getVariableGetter('foo')));
  45. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 0);
  46. $tests[] = array($node, sprintf('%s%s, "bar", array(), "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo')));
  47. $args = new Twig_Node_Expression_Array(array(), 0);
  48. $args->addElement(new Twig_Node_Expression_Name('foo', 0));
  49. $args->addElement(new Twig_Node_Expression_Constant('bar', 0));
  50. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::METHOD_CALL, 0);
  51. $tests[] = array($node, sprintf('%s%s, "bar", array(0 => %s, 1 => "bar"), "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo'), $this->getVariableGetter('foo')));
  52. return $tests;
  53. }
  54. }