improved error messages

This commit is contained in:
Leon van der Ree
2010-12-27 14:16:09 +01:00
committed by Fabien Potencier
parent 4c237b2253
commit 024b9367eb
3 changed files with 29 additions and 2 deletions
+1 -1
View File
@@ -344,7 +344,7 @@ class Twig_ExpressionParser
while (true) {
$token = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, null, 'Only variables can be assigned to');
if (in_array($token->getValue(), array('true', 'false', 'none'))) {
throw new Twig_Error_Syntax($token->getValue() . ' cannot be assigned to');
throw new Twig_Error_Syntax(sprintf('You cannot assign a value to "%s"', $token->getValue()));
}
$targets[] = new Twig_Node_Expression_AssignName($token->getValue(), $token->getLine());
+6 -1
View File
@@ -128,7 +128,12 @@ abstract class Twig_Template implements Twig_TemplateInterface
return null;
}
throw new Twig_Error_Runtime(sprintf('Key "%s" for array "%s" does not exist', $item, $object), -1, $this->getTemplateName());
if (is_object($object)) {
throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)), -1, $this->getTemplateName());
// array
} else {
throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))), -1, $this->getTemplateName());
}
}
}
+22
View File
@@ -10,6 +10,28 @@
*/
class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
{
public function getUnkownPropertyOnArrayTests()
{
$tests = array(
array(array('foo' => 'foo', 'bar' => 'value')),
array(new Twig_TemplateObjectArrayAccess()),
);
return $tests;
}
/**
* @dataProvider getUnkownPropertyOnArrayTests
* @expectedException Twig_Error_Runtime
*/
public function testUnkownPropertyOnArray($array)
{
$env = new Twig_Environment(null, array('strict_variables' => true));
$template = new Twig_TemplateTest($env);
$template->getAttribute($array, 'unknown', array(), Twig_Node_Expression_GetAttr::TYPE_ARRAY);
}
/**
* @dataProvider getGetAttributeTests
*/