From 8444ff7d61c3a1f4c7164858f696f431c2634593 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Fri, 8 Jan 2021 17:20:56 +0100 Subject: [PATCH] Improved php7-style default modifier that does not trigger custom error handlers. Fixes #617 --- libs/plugins/modifiercompiler.default.php | 2 +- .../MuteExpectedErrorsTest.php | 22 +++++++------- .../PluginModifierDefaultTest.php | 30 +++++++++++++++++++ 3 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierDefaultTest.php diff --git a/libs/plugins/modifiercompiler.default.php b/libs/plugins/modifiercompiler.default.php index ae886c4b..3846c134 100644 --- a/libs/plugins/modifiercompiler.default.php +++ b/libs/plugins/modifiercompiler.default.php @@ -26,7 +26,7 @@ function smarty_modifiercompiler_default($params) } array_shift($params); foreach ($params as $param) { - $output = '(($tmp = @' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)'; + $output = '(($tmp = ' . $output . ' ?? null)===null||$tmp===\'\' ? ' . $param . ' ?? null : $tmp)'; } return $output; } diff --git a/tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php b/tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php index 40bc4392..d05b9f38 100644 --- a/tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php +++ b/tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php @@ -20,8 +20,13 @@ class MuteExpectedErrorsTest extends PHPUnit_Smarty public function setUp(): void { $this->setUpSmarty(dirname(__FILE__)); + set_error_handler(array($this, 'error_handler')); } + public function tearDown(): void { + restore_error_handler(); + parent::tearDown(); + } public function testInit() { @@ -34,7 +39,7 @@ class MuteExpectedErrorsTest extends PHPUnit_Smarty public function testMuted() { - set_error_handler(array($this, 'error_handler')); + Smarty::muteExpectedErrors(); $this->smarty->clearCache('default.tpl'); @@ -48,7 +53,6 @@ class MuteExpectedErrorsTest extends PHPUnit_Smarty $this->assertEquals($this->_errors, $error); Smarty::unmuteExpectedErrors(); - restore_error_handler(); } /** @@ -58,18 +62,16 @@ class MuteExpectedErrorsTest extends PHPUnit_Smarty */ public function testUnmuted() { - set_error_handler(array($this, 'error_handler')); $this->smarty->clearCache('default.tpl'); $this->smarty->clearCompiledTemplate('default.tpl'); $this->smarty->fetch('default.tpl'); - $this->assertEquals(Smarty::$_IS_WINDOWS ? 2 : 2, count($this->_errors)); + $this->assertEquals($this->_errors, array()); @filemtime('ckxladanwijicajscaslyxck'); - $this->assertEquals(Smarty::$_IS_WINDOWS ? 3 : 3, count($this->_errors)); + $this->assertEquals(1, count($this->_errors)); - restore_error_handler(); } /** @@ -80,7 +82,6 @@ class MuteExpectedErrorsTest extends PHPUnit_Smarty */ public function testMutedCaching() { - set_error_handler(array($this, 'error_handler')); Smarty::muteExpectedErrors(); $this->smarty->caching = true; @@ -95,7 +96,6 @@ class MuteExpectedErrorsTest extends PHPUnit_Smarty $this->assertEquals($error, $this->_errors); Smarty::unmuteExpectedErrors(); - restore_error_handler(); } /** @@ -105,18 +105,16 @@ class MuteExpectedErrorsTest extends PHPUnit_Smarty */ public function testUnmutedCaching() { - set_error_handler(array($this, 'error_handler')); $this->smarty->caching = true; $this->smarty->clearCache('default.tpl'); $this->smarty->clearCompiledTemplate('default.tpl'); $this->smarty->fetch('default.tpl'); - $this->assertEquals(Smarty::$_IS_WINDOWS ? 2 : 2, count($this->_errors)); + $this->assertEquals($this->_errors, array()); @filemtime('ckxladanwijicajscaslyxck'); - $error = array(__FILE__ . ' line ' . (__LINE__ - 1)); - $this->assertEquals(Smarty::$_IS_WINDOWS ? 3 : 3, count($this->_errors)); + $this->assertEquals(1, count($this->_errors)); restore_error_handler(); } diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierDefaultTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierDefaultTest.php new file mode 100644 index 00000000..cfcd5251 --- /dev/null +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierDefaultTest.php @@ -0,0 +1,30 @@ +setUpSmarty(dirname(__FILE__)); + } + + public function testDefault() + { + $tpl = $this->smarty->createTemplate('string:{$array.a.b|default:$array.c:\'defaultval\'}'); + + $this->smarty->assign('array', []); + $this->assertEquals('defaultval', $this->smarty->fetch($tpl)); + } +}