Cleaned up the unit test

This commit is contained in:
Simon Wisselink
2024-03-18 14:39:33 +01:00
parent eadec6a3d7
commit 04c247e0d1

View File

@@ -231,79 +231,70 @@ class PhpFunctionTest extends PHPUnit_Smarty
return 'hi ' . $value;
}
/**
* Tests that each function that will still be supported in Smarty 5 does NOT throw an E_USER_DEPRECATED notice.
* @dataProvider dataSupportedInSmarty5
*/
public function testSupportedInSmarty5($strTemplateSource, $expected) {
$this->cleanDirs();
$this->smarty->setErrorReporting(E_ALL);
$this->smarty->assign('a', 'a');
$this->smarty->assign('ar', [1,2]);
$this->smarty->assign('f', 3.14);
$output = $this->smarty->fetch('string:' . $strTemplateSource);
$this->assertEquals($expected, $output);
$this->assertStringNotContainsString('Deprecated', $output);
}
public function dataSupportedInSmarty5()
{
return [
['{if empty($a)}{else}b{/if}', 'b'],
['{json_encode($a)}', '"a"'],
['{nl2br($a)}', 'a'],
['{$a|nl2br}', 'a'],
['{round($f, 1)}', '3.1'],
['{$f|round}', '3'],
['{str_repeat($a, 2)}', 'aa'],
['{$a|str_repeat:3}', 'aaa'],
['{$a|strip_tags}', 'a'],
['{$a|strlen}', '1'],
['{$a|substr:-1}', 'a'],
['{$f|substr:-1}', '4'],
['{$ar|count}', '2'],
['{foreach "."|explode:$f as $n}{$n}{/foreach}', '314'],
['{"-"|implode:$ar}', '1-2'],
['{"-"|join:$ar}', '1-2'],
['{$f|wordwrap:2:"k":true}', "3.k14"],
['{$f|number_format:1:","}', "3,1"],
['{if in_array(1, $ar)}yes{/if}', "yes"],
['{if is_array($ar)}yes{/if}', "yes"],
['{if time() gt 0}yes{/if}', "yes"],
];
}
/**
* Tests that each function that will not be supported in Smarty 5 does throw an E_USER_DEPRECATED notice.
* @dataProvider dataNotSupportedInSmarty5
* @dataProvider dataDeprecationNoticesForSmarty5
* @deprecated
*/
public function testNotSupportedInSmarty5($strTemplateSource) {
$this->cleanDirs();
$this->smarty->setErrorReporting(E_ALL);
public function testDeprecationNoticesForSmarty5($strTemplateSource, $expected = '', $shouldTriggerDeprecationNotice = false) {
// this overrides the error reporting level set in \PHPUnit_Smarty::setUpSmarty
$previousErrorReporting = $this->smarty->error_reporting;
$this->smarty->setErrorReporting($previousErrorReporting | E_USER_DEPRECATED);
$this->smarty->assign('a', 'a');
$this->smarty->assign('ar', [1,2]);
$this->smarty->assign('f', 3.14);
$errorMessage = '';
try {
$this->smarty->fetch('string:' . $strTemplateSource);
$this->assertTrue(false); // we should not reach this
$output = $this->smarty->fetch('string:' . $strTemplateSource);
} catch (Exception $e) {
$this->assertStringContainsString('Using unregistered function', $e->getMessage());
$errorMessage = $e->getMessage();
}
if ($shouldTriggerDeprecationNotice) {
$this->assertStringContainsString('Using unregistered function', $errorMessage);
} else {
$this->assertEquals($expected, $output);
$this->assertEquals('', $errorMessage);
}
public function dataNotSupportedInSmarty5()
$this->smarty->setErrorReporting($previousErrorReporting);
}
public function dataDeprecationNoticesForSmarty5()
{
return [
['{if array_chunk($ar, 2)}x{else}y{/if}'],
['{$a|addslashes}'],
['{$a|sha1}'],
['{$a|get_parent_class}'],
['{if empty($a)}{else}b{/if}', 'b', false],
['{json_encode($a)}', '"a"', false],
['{nl2br($a)}', 'a', false],
['{$a|nl2br}', 'a', false],
['{round($f, 1)}', '3.1', false],
['{$f|round}', '3', false],
['{str_repeat($a, 2)}', 'aa', false],
['{$a|str_repeat:3}', 'aaa', false],
['{$a|strip_tags}', 'a', false],
['{$a|strlen}', '1', false],
['{$a|substr:-1}', 'a', false],
['{$f|substr:-1}', '4', false],
['{$ar|count}', '2', false],
['{foreach "."|explode:$f as $n}{$n}{/foreach}', '314', false],
['{"-"|implode:$ar}', '1-2', false],
['{"-"|join:$ar}', '1-2', false],
['{$f|wordwrap:2:"k":true}', "3.k14", false],
['{$f|number_format:1:","}', "3,1", false],
['{if in_array(1, $ar)}yes{/if}', "yes", false],
['{if is_array($ar)}yes{/if}', "yes", false],
['{if time() gt 0}yes{/if}', "yes", false],
['{if array_chunk($ar, 2)}x{else}y{/if}', '', true],
['{$a|addslashes}', '', true],
['{$a|sha1}', '', true],
['{$a|get_parent_class}', '', true],
];
}