From 17af31b5b42c382ed7ed2e1f1d618b450b556fb7 Mon Sep 17 00:00:00 2001 From: nikic <+@ni-po.com> Date: Thu, 28 Apr 2011 13:17:49 +0200 Subject: [PATCH] Use a type strict null check for GetAttr defined tests. Fixes #305 --- lib/Twig/Node/Expression/Test.php | 5 ++++- test/Twig/Tests/Fixtures/tests/defined.test | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/Twig/Node/Expression/Test.php b/lib/Twig/Node/Expression/Test.php index 4995bc7..2a69e8c 100644 --- a/lib/Twig/Node/Expression/Test.php +++ b/lib/Twig/Node/Expression/Test.php @@ -32,7 +32,10 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression ; } elseif ($this->getNode('node') instanceof Twig_Node_Expression_GetAttr) { $this->getNode('node')->setAttribute('is_defined_test', true); - $compiler->subcompile($this->getNode('node')); + $compiler + ->raw('null !== ') + ->subcompile($this->getNode('node')) + ; } else { throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine()); } diff --git a/test/Twig/Tests/Fixtures/tests/defined.test b/test/Twig/Tests/Fixtures/tests/defined.test index a401b5e..e4159fd 100644 --- a/test/Twig/Tests/Fixtures/tests/defined.test +++ b/test/Twig/Tests/Fixtures/tests/defined.test @@ -6,11 +6,13 @@ {{ foobar is not defined ? 'ok' : 'ko' }} {{ nested.foo is defined ? 'ok' : 'ko' }} {{ nested.bar is not defined ? 'ok' : 'ko' }} +{{ nested.zero is defined ? 'ok' : 'ko' }} --DATA-- -return array('foo' => 'bar', 'bar' => null, 'nested' => array('foo' => 'foo')); +return array('foo' => 'bar', 'bar' => null, 'nested' => array('foo' => 'foo', 'zero' => 0)); --EXPECT-- ok ok ok ok ok +ok -- 1.7.2.5