Permalink
Browse files

test twig_template_get_attributes

  • Loading branch information...
1 parent fa71922 commit 7e566b84621633715c27dd5f41e94225c740847f @arnaud-lb arnaud-lb committed Nov 23, 2011
Showing with 82 additions and 9 deletions.
  1. +82 −9 test/Twig/Tests/TemplateTest.php
@@ -13,20 +13,24 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
/**
* @dataProvider getGetAttributeTests
*/
- public function testGetAttribute($defined, $value, $object, $item, $arguments, $type)
+ public function testGetAttribute($defined, $value, $object, $item, $arguments, $type, $useExt = false)
{
- $template = new Twig_TemplateTest(new Twig_Environment());
+ $template = new Twig_TemplateTest(
+ new Twig_Environment(),
+ $useExt
+ );
$this->assertEquals($value, $template->getAttribute($object, $item, $arguments, $type));
}
/**
* @dataProvider getGetAttributeTests
*/
- public function testGetAttributeStrict($defined, $value, $object, $item, $arguments, $type)
+ public function testGetAttributeStrict($defined, $value, $object, $item, $arguments, $type, $useExt = false)
{
$template = new Twig_TemplateTest(
- new Twig_Environment(null, array('strict_variables' => true))
+ new Twig_Environment(null, array('strict_variables' => true)),
+ $useExt
);
if ($defined) {
@@ -43,20 +47,24 @@ public function testGetAttributeStrict($defined, $value, $object, $item, $argume
/**
* @dataProvider getGetAttributeTests
*/
- public function testGetAttributeDefined($defined, $value, $object, $item, $arguments, $type)
+ public function testGetAttributeDefined($defined, $value, $object, $item, $arguments, $type, $useExt = false)
{
- $template = new Twig_TemplateTest(new Twig_Environment());
+ $template = new Twig_TemplateTest(
+ new Twig_Environment(),
+ $useExt
+ );
$this->assertEquals($defined, $template->getAttribute($object, $item, $arguments, $type, true));
}
/**
* @dataProvider getGetAttributeTests
*/
- public function testGetAttributeDefinedStrict($defined, $value, $object, $item, $arguments, $type)
+ public function testGetAttributeDefinedStrict($defined, $value, $object, $item, $arguments, $type, $useExt = false)
{
$template = new Twig_TemplateTest(
- new Twig_Environment(null, array('strict_variables' => true))
+ new Twig_Environment(null, array('strict_variables' => true)),
+ $useExt
);
$this->assertEquals($defined, $template->getAttribute($object, $item, $arguments, $type, true));
@@ -100,6 +108,7 @@ public function getGetAttributeTests()
array($stdObject, $anyType),
array($magicPropertyObject, $anyType),
array($methodObject, $methodType),
+ array($methodObject, $anyType),
array($propertyObject, $anyType),
array($propertyObject1, $anyType),
);
@@ -137,12 +146,54 @@ public function getGetAttributeTests()
}
}
+ $methodAndPropObject = new Twig_TemplateMethodAndPropObject;
+
+ // additional method tests
+ $tests = array_merge($tests, array(
+ array(true, 'a', $methodAndPropObject, 'a', array(), $anyType),
+ array(true, 'a', $methodAndPropObject, 'a', array(), $methodType),
+ array(false, null, $methodAndPropObject, 'a', array(), $arrayType),
+
+ array(true, 'b_prop', $methodAndPropObject, 'b', array(), $anyType),
+ array(true, 'b', $methodAndPropObject, 'B', array(), $anyType),
+ array(true, 'b', $methodAndPropObject, 'b', array(), $methodType),
+ array(true, 'b', $methodAndPropObject, 'B', array(), $methodType),
+ array(false, null, $methodAndPropObject, 'b', array(), $arrayType),
+
+ array(false, null, $methodAndPropObject, 'c', array(), $anyType),
+ array(false, null, $methodAndPropObject, 'c', array(), $methodType),
+ array(false, null, $methodAndPropObject, 'c', array(), $arrayType),
+
+ ));
+
+ // add twig_template_get_attributes tests
+
+ if (function_exists('twig_template_get_attributes')) {
+ foreach(array_slice($tests, 0) as $test) {
+ $test[] = true;
+ $tests[] = $test;
+ }
+ }
+
return $tests;
}
+
+ public function useExtGetAttribute()
+ {
+ return false;
+ }
}
class Twig_TemplateTest extends Twig_Template
{
+ protected $useExtGetAttribute = false;
+
+ public function __construct(Twig_Environment $env, $useExtGetAttribute = false)
+ {
+ parent::__construct($env);
+ $this->useExtGetAttribute = $useExtGetAttribute;
+ }
+
public function getTemplateName()
{
}
@@ -157,7 +208,11 @@ protected function doDisplay(array $context, array $blocks = array())
public function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
{
- return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck);
+ if ($this->useExtGetAttribute) {
+ return twig_template_get_attributes($this, $object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck);
+ } else {
+ return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck);
+ }
}
}
@@ -264,6 +319,24 @@ static public function getStatic()
}
}
+class Twig_TemplateMethodAndPropObject
+{
+ private $a = 'a_prop';
+ public function getA() {
+ return 'a';
+ }
+
+ public $b = 'b_prop';
+ public function getB() {
+ return 'b';
+ }
+
+ private $c = 'c_prop';
+ private function getC() {
+ return 'c';
+ }
+}
+
class Twig_TemplateMagicMethodObject
{
public function __call($method, $arguments) {

0 comments on commit 7e566b8

Please sign in to comment.