GetAttrTest.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(array(
  21. new Twig_Node_Expression_Name('foo', 0),
  22. new Twig_Node_Expression_Constant('bar', 0),
  23. ));
  24. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 0);
  25. $this->assertEquals($expr, $node->getNode('node'));
  26. $this->assertEquals($attr, $node->getNode('attribute'));
  27. $this->assertEquals($args, $node->getNode('arguments'));
  28. $this->assertEquals(Twig_TemplateInterface::ARRAY_CALL, $node->getAttribute('type'));
  29. }
  30. /**
  31. * @covers Twig_Node_Expression_GetAttr::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. $expr = new Twig_Node_Expression_Name('foo', 0);
  42. $attr = new Twig_Node_Expression_Constant('bar', 0);
  43. $args = new Twig_Node();
  44. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ANY_CALL, 0);
  45. $tests[] = array($node, sprintf('%s%s, "bar")', $this->getAttributeGetter(), $this->getVariableGetter('foo')));
  46. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 0);
  47. $tests[] = array($node, sprintf('%s%s, "bar", array(), "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo')));
  48. $args = new Twig_Node(array(
  49. new Twig_Node_Expression_Name('foo', 0),
  50. new Twig_Node_Expression_Constant('bar', 0),
  51. ));
  52. $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::METHOD_CALL, 0);
  53. $tests[] = array($node, sprintf('%s%s, "bar", array(%s, "bar", ), "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo'), $this->getVariableGetter('foo')));
  54. return $tests;
  55. }
  56. }