From 177890222005bd31a51a50b91cbecda292be0ec0 Mon Sep 17 00:00:00 2001 From: Todd Burry Date: Wed, 15 Nov 2017 13:09:51 -0500 Subject: [PATCH 1/2] Fix bug when reusing cached expressions The expression language caches compiled expressions, but since the data that goes into the expressions is contextual then this cache should not be used. --- src/ExpressionLanguage.php | 3 ++- tests/AbstractTest.php | 9 +++++++++ tests/TemplateTest.php | 16 ++++++++++++++++ tests/fixtures/d.html | 1 + tests/fixtures/repro01.html | 6 ++++++ 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/d.html create mode 100644 tests/fixtures/repro01.html diff --git a/src/ExpressionLanguage.php b/src/ExpressionLanguage.php index 8b693ea..94d8bbf 100644 --- a/src/ExpressionLanguage.php +++ b/src/ExpressionLanguage.php @@ -7,13 +7,14 @@ namespace Ebi; +use Symfony\Component\Cache\Adapter\NullAdapter; use Symfony\Component\ExpressionLanguage\Node\ConstantNode; use Symfony\Component\ExpressionLanguage\Node\GetAttrNode; use Symfony\Component\ExpressionLanguage\Node\NameNode; class ExpressionLanguage extends \Symfony\Component\ExpressionLanguage\ExpressionLanguage { public function __construct() { - parent::__construct(); + parent::__construct(new NullAdapter()); $this->registerNodeFunction(GetAttrNode::class, function (\Symfony\Component\ExpressionLanguage\Compiler $compiler, GetAttrNode $node) { switch ($node->attributes['type']) { diff --git a/tests/AbstractTest.php b/tests/AbstractTest.php index a959407..696379c 100644 --- a/tests/AbstractTest.php +++ b/tests/AbstractTest.php @@ -68,6 +68,15 @@ public function renderFixture($component, $data = []) { $ebi->defineFunction('concat', function (...$args) { return implode('', $args); }); + $ebi->defineFunction('ident', function (...$args) { + if (count($args) === 1) { + return $args[0]; + } elseif (empty($args)) { + return null; + } else { + return $args; + } + }); $rendered = $ebi->render($component, $data); diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index 3d875d5..c27d143 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -221,6 +221,22 @@ public function testEachSpecial() { $this->assertHtmlEquals($expected, $r); } + /** + * Test a bug from production that resulted from odd caching behavior. + */ + public function testRepro01() { + $r = $this->renderFixture('repro01', ['data' => [1, 2]]); + + $expected = <<assertEquals($expected, $r); + } + /** * */ diff --git a/tests/fixtures/d.html b/tests/fixtures/d.html new file mode 100644 index 0000000..bd3f624 --- /dev/null +++ b/tests/fixtures/d.html @@ -0,0 +1 @@ +{unescape(json_encode(this))} diff --git a/tests/fixtures/repro01.html b/tests/fixtures/repro01.html new file mode 100644 index 0000000..fee1ab9 --- /dev/null +++ b/tests/fixtures/repro01.html @@ -0,0 +1,6 @@ + + + + + + From 08e777eff993031b345c1a5d40a7f378ed8e4f2c Mon Sep 17 00:00:00 2001 From: Todd Burry Date: Wed, 15 Nov 2017 13:18:27 -0500 Subject: [PATCH 2/2] Update for spacing --- tests/TemplateTest.php | 5 +---- tests/fixtures/repro01.html | 7 +------ 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index c27d143..eca1ea8 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -228,10 +228,7 @@ public function testRepro01() { $r = $this->renderFixture('repro01', ['data' => [1, 2]]); $expected = <<assertEquals($expected, $r); diff --git a/tests/fixtures/repro01.html b/tests/fixtures/repro01.html index fee1ab9..0b48be6 100644 --- a/tests/fixtures/repro01.html +++ b/tests/fixtures/repro01.html @@ -1,6 +1 @@ - - - - - - +