added tests for exceptions thrown in __call()
authorFabien Potencier <fabien.potencier@gmail.com>
Tue, 3 Dec 2013 12:58:35 +0000 (13:58 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Tue, 3 Dec 2013 13:14:58 +0000 (14:14 +0100)
CHANGELOG
lib/Twig/Template.php
test/Twig/Tests/TemplateTest.php

index 5fd88cd..48838a3 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,6 @@
 * 1.15.0 (2013-XX-XX)
 
+ * made ignoreStrictCheck in Template::getAttribute() works with __call() methods throwing BadMethodCallException
  * added the round filter
  * fixed a bug that prevented the optimizers to be enabled/disabled selectively
  * fixed first and last filters for UTF-8 strings
index 8573d34..4ae5968 100644 (file)
@@ -417,6 +417,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
             self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object)));
         }
 
+        $call = false;
         $lcItem = strtolower($item);
         if (isset(self::$cache[$class]['methods'][$lcItem])) {
             $method = (string) $item;
@@ -426,6 +427,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
             $method = 'is'.$item;
         } elseif (isset(self::$cache[$class]['methods']['__call'])) {
             $method = (string) $item;
+            $call = true;
         } else {
             if ($isDefinedTest) {
                 return false;
@@ -450,8 +452,8 @@ abstract class Twig_Template implements Twig_TemplateInterface
         // to call is not supported. If ignoreStrictCheck is true, we should return null.
         try {
             $ret = call_user_func_array(array($object, $method), $arguments);
-        } catch (\BadMethodCallException $e) {
-            if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
+        } catch (BadMethodCallException $e) {
+            if ($call && ($ignoreStrictCheck || !$this->env->isStrictVariables())) {
                 return null;
             }
             throw $e;
index cc3b2a2..25c7a4a 100644 (file)
@@ -236,6 +236,18 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
         $this->assertEquals($defined, $template->getAttribute($object, $item, $arguments, $type, true));
     }
 
+    /**
+     * @dataProvider getTestsDependingOnExtensionAvailability
+     */
+    public function testGetAttributeCallExceptions($useExt = false)
+    {
+        $template = new Twig_TemplateTest(new Twig_Environment(), $useExt);
+
+        $object = new Twig_TemplateMagicMethodExceptionObject();
+
+        $this->assertEquals(null, $template->getAttribute($object, 'foo'));
+    }
+
     public function getGetAttributeTests()
     {
         $array = array(
@@ -603,6 +615,14 @@ class Twig_TemplateMagicMethodObject
     }
 }
 
+class Twig_TemplateMagicMethodExceptionObject
+{
+    public function __call($method, $arguments)
+    {
+        throw new BadMethodCallException(sprintf('Unkown method %s', $method));
+    }
+}
+
 class CExtDisablingNodeVisitor implements Twig_NodeVisitorInterface
 {
     public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)