From 9ab290bdb235fbca801f90dc52a630b237e98ab2 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 3 Dec 2013 13:58:35 +0100 Subject: [PATCH] added tests for exceptions thrown in __call() --- CHANGELOG | 1 + lib/Twig/Template.php | 6 ++++-- test/Twig/Tests/TemplateTest.php | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 5fd88cd..48838a3 100644 --- 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 diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 8573d34..4ae5968 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -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; diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index cc3b2a2..25c7a4a 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -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) -- 1.7.2.5