added tests for exceptions thrown in __call()

This commit is contained in:
Fabien Potencier
2013-12-03 13:58:35 +01:00
parent c711d37f1d
commit 9ab290bdb2
3 changed files with 25 additions and 2 deletions
+1
View File
@@ -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
+4 -2
View 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;
+20
View 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)