From 82e3ecc6da7990af630b88997305993186334528 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 21 Nov 2011 14:31:08 +0100 Subject: [PATCH] added more unit tests --- lib/Twig/Template.php | 12 ++++------ test/Twig/Tests/TemplateTest.php | 44 ++++++++++++++++++++++++++++++++----- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 3207269..188ceb5 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -356,17 +356,15 @@ abstract class Twig_Template implements Twig_TemplateInterface // object property if (Twig_TemplateInterface::METHOD_CALL !== $type) { + /* apparently, this is not needed as this is already covered by the array_key_exists() call below if (!isset(self::$cache[$class]['properties'])) { - $props = get_object_vars($object); - foreach ($props as &$v) { - $v = true; + foreach (get_object_vars($object) as $k => $v) { + self::$cache[$class]['properties'][$k] = true; } - self::$cache[$class]['properties'] = $props; } + */ - if (isset(self::$cache[$class]['properties'][$item]) - || isset($object->$item) || array_key_exists($item, $object) - ) { + if (isset($object->$item) || array_key_exists($item, $object)) { if ($isDefinedTest) { return true; } diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 874f4da..475fac5 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -71,11 +71,13 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase '1' => 1, ); - $objectArray = new Twig_TemplateArrayAccessObject; + $objectArray = new Twig_TemplateArrayAccessObject(); $stdObject = (object) $array; - $magicPropertyObject = new Twig_TemplateMagicPropertyObject; - $methodObject = new Twig_TemplateMethodObject; - $magicMethodObject = new Twig_TemplateMagicMethodObject; + $magicPropertyObject = new Twig_TemplateMagicPropertyObject(); + $propertyObject = new Twig_TemplatePropertyObject(); + $propertyObject1 = new Twig_TemplatePropertyObjectAndIterator(); + $methodObject = new Twig_TemplateMethodObject(); + $magicMethodObject = new Twig_TemplateMagicMethodObject(); $anyType = Twig_TemplateInterface::ANY_CALL; $methodType = Twig_TemplateInterface::METHOD_CALL; @@ -85,6 +87,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase // array(defined, value, property to fetch) array(true, 'defined', 'defined'), array(false, null, 'undefined'), + array(false, null, 'protected'), array(true, 0, 'zero'), array(true, 1, 1), array(true, 1, 1.0), @@ -97,13 +100,15 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase array($stdObject, $anyType), array($magicPropertyObject, $anyType), array($methodObject, $methodType), + array($propertyObject, $anyType), + array($propertyObject1, $anyType), ); $tests = array(); foreach ($testObjects as $testObject) { foreach ($basicTests as $test) { // properties cannot be numbers - if ($testObject[0] instanceof stdClass && is_numeric($test[2])) { + if (($testObject[0] instanceof stdClass || $testObject[0] instanceof Twig_TemplatePropertyObject) && is_numeric($test[2])) { continue; } @@ -158,6 +163,8 @@ class Twig_TemplateTest extends Twig_Template class Twig_TemplateArrayAccessObject implements ArrayAccess { + protected $protected = 'protected'; + public $attributes = array( 'defined' => 'defined', 'zero' => 0, @@ -186,13 +193,16 @@ class Twig_TemplateArrayAccessObject implements ArrayAccess class Twig_TemplateMagicPropertyObject { + public $defined = 'defined'; + public $attributes = array( - 'defined' => 'defined', 'zero' => 0, 'null' => null, '1' => 1, ); + protected $protected = 'protected'; + public function __isset($name) { return array_key_exists($name, $this->attributes); @@ -204,6 +214,23 @@ class Twig_TemplateMagicPropertyObject } } +class Twig_TemplatePropertyObject +{ + public $defined = 'defined'; + public $zero = 0; + public $null = null; + + protected $protected = 'protected'; +} + +class Twig_TemplatePropertyObjectAndIterator extends Twig_TemplatePropertyObject implements IteratorAggregate +{ + public function getIterator() + { + return new ArrayIterator(array('foo', 'bar')); + } +} + class Twig_TemplateMethodObject { public function getDefined() @@ -226,6 +253,11 @@ class Twig_TemplateMethodObject return null; } + protected function getProtected() + { + return 'protected'; + } + static public function getStatic() { return 'static'; -- 1.7.2.5