Files
smarty/tests/UnitTests/TemplateSource/StaticClass/StaticClassAccessTest.php
Simon Wisselink ff2ef3b0cb Redirect test temp dirs to system temp directory
* Redirect test temp dirs to system temp directory. Fixes #1178

Move all test-generated output (compiled templates, cache files, and
temporary template sources) from per-test-directory folders inside the
working tree to a parallel structure under sys_get_temp_dir()/smarty-tests/.

This removes 215 boilerplate .gitignore files from the repo and ensures
running the test suite leaves zero uncommitted files in the working tree.

All 2296 tests continue to pass with identical behavior.

* Isolate each test class in a unique temp directory

getTempDir() now appends a per-class uniqid token to the temp path, so
concurrent or sequential test runs never share compiled/cached output.
The token is generated lazily on first use and reset in
tearDownAfterClass(), giving every test class a fresh isolated directory.

As a result, the Bootstrap.php pre-run cleanup of smarty-tests/ is no
longer needed for correctness (stale paths are unreachable) and was
harmful to concurrent runs, so it has been removed.

* Remove individualFolders dead code and spurious assertTrue from cleanDirs()

- Remove the never-active individualFolders code path from setUpSmarty()
  (the constant was always true, making the branch unreachable)
- Remove define('individualFolders') from Config.php and the constructor
- Remove $this->assertTrue(true) from cleanDirs(): it existed solely to
  make testInit() count as a passing test; now that cleanDirs() is called
  from setUpSmarty() and from test methods directly, the assertion was
  spuriously inflating assertion counts
- Add tests/**/templates_c/, cache/, templates_tmp/ to .gitignore to
  prevent stale test output from appearing as untracked files

* Clean up each test class's unique temp dir in tearDownAfterClass()

Add a private static removeDir() helper and call it from
tearDownAfterClass() to recursively delete the per-class unique temp
directory after each test class finishes. Cleanup failures are silently
ignored (@ suppression) so they never cause test failures.

Set KEEP_SMARTY_TEST_ARTIFACTS=1 in the environment to skip cleanup and
keep the artifacts on disk for debugging.

* cleanup of unused template files, non-shared files stored in __shared folder, no longer required calls to add template folders et cetera

* fixed the unit tests

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* remove useless resetting of static properties in tearDownAfterClass

* changed an incorrect doc and formatted some code.

* add changelog

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-04-13 21:36:33 +02:00

151 lines
4.1 KiB
PHP

<?php
/**
* Smarty PHPunit tests static class access to constants, variables and methods
*
* @author Uwe Tews
*/
/**
* class for static class access to constants, variables and methods tests
*
*
*
*
*/
class StaticClassAccessTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
$this->smarty->disableSecurity();
}
/**
* test static class variable
*/
public function testStaticClassVariable()
{
$tpl = $this->smarty->createTemplate('eval:{mystaticclass::$static_var}');
$this->assertEquals('5', $this->smarty->fetch($tpl));
}
/**
* test registered static class variable
*/
public function testStaticRegisteredClassVariable()
{
$this->smarty->registerClass('registeredclass', 'mystaticclass');
$tpl = $this->smarty->createTemplate('eval:{registeredclass::$static_var}');
$this->assertEquals('5', $this->smarty->fetch($tpl));
}
/**
* test static class constant
*/
public function testStaticClassConstant()
{
$tpl = $this->smarty->createTemplate('eval:{mystaticclass::STATIC_CONSTANT_VALUE}');
$this->assertEquals('3', $this->smarty->fetch($tpl));
}
/**
* test static class constant
*/
public function testRegisteredStaticClassConstant()
{
$this->smarty->registerClass('registeredclass', 'mystaticclass');
$tpl = $this->smarty->createTemplate('eval:{registeredclass::STATIC_CONSTANT_VALUE}');
$this->assertEquals('3', $this->smarty->fetch($tpl));
}
/**
* test static class constant chain
*/
public function testRegisteredBackedEnum()
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Enums only available after PHP >= 8.1');
return;
} else {
$this->smarty->registerClass('RegisteredBackedEnum', MyBackedEnum::class);
$tpl = $this->smarty->createTemplate('eval:{RegisteredBackedEnum::A->value}');
$this->assertEquals('3', $this->smarty->fetch($tpl));
}
}
/**
* test static class method
*/
public function testStaticClassmethod()
{
$tpl = $this->smarty->createTemplate('eval:{mystaticclass::square(5)}');
$this->assertEquals('25', $this->smarty->fetch($tpl));
}
/**
* test static class method
*/
public function testRegisteredStaticClassmethod()
{
$this->smarty->registerClass('registeredclass', 'mystaticclass');
$tpl = $this->smarty->createTemplate('eval:{registeredclass::square(5)}');
$this->assertEquals('25', $this->smarty->fetch($tpl));
}
/**
* test static class variable method
*/
public function testStaticClassVariablemethod()
{
$tpl = $this->smarty->createTemplate('eval:{$foo=\'square\'}{mystaticclass::$foo(5)}');
$this->assertEquals('25', $this->smarty->fetch($tpl));
}
/**
* test registered static class variable method
*/
public function testRegisteredStaticClassVariablemethod()
{
$this->smarty->registerClass('registeredclass', 'mystaticclass');
$tpl = $this->smarty->createTemplate('eval:{$foo=\'square\'}{registeredclass::$foo(5)}');
$this->assertEquals('25', $this->smarty->fetch($tpl));
}
/**
* test static class variable method
*/
public function testStaticClassVariablemethod2()
{
$tpl = $this->smarty->createTemplate('eval:{mystaticclass::$foo(5)}');
$tpl->assign('foo', 'square');
$this->assertEquals('25', $this->smarty->fetch($tpl));
}
/**
* test registered static class variable method
*/
public function testRegisteredStaticClassVariablemethod2()
{
$this->smarty->registerClass('registeredclass', 'mystaticclass');
$tpl = $this->smarty->createTemplate('eval:{registeredclass::$foo(5)}');
$tpl->assign('foo', 'square');
$this->assertEquals('25', $this->smarty->fetch($tpl));
}
}
class mystaticclass
{
const STATIC_CONSTANT_VALUE = 3;
static $static_var = 5;
static function square($i)
{
return $i * $i;
}
}
if (PHP_VERSION_ID >= 80100) {
eval('enum MyBackedEnum: int { case A = 3; }');
}