mirror of
https://github.com/smarty-php/smarty.git
synced 2026-07-06 00:10:45 +02:00
ff2ef3b0cb
* 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>
382 lines
17 KiB
PHP
382 lines
17 KiB
PHP
<?php
|
|
/**
|
|
* Smarty PHPunit tests for cache resource file
|
|
*
|
|
|
|
* @author Uwe Tews
|
|
*/
|
|
|
|
include_once __DIR__ . '/../_shared/CacheResourceTestCommon.php';
|
|
|
|
/**
|
|
* class for cache resource file tests
|
|
*/
|
|
class CacheResourceFileTest extends CacheResourceTestCommon
|
|
{
|
|
|
|
private $directorySeparator;
|
|
|
|
public function setUp(): void
|
|
{
|
|
$this->setUpSmarty(__DIR__);
|
|
parent::setUp();
|
|
$this->directorySeparator = preg_quote(DIRECTORY_SEPARATOR, '/');
|
|
$this->smarty->setCachingType('filetest');
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* test getCachedFilepath with configuration['useSubDirs'] enabled
|
|
*/
|
|
public function testGetCachedFilepathSubDirs()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(true);
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl');
|
|
|
|
$pattern = '/.*' . $this->directorySeparator . '([a-f0-9]{2}' . $this->directorySeparator . '){3}.*\.php/';
|
|
$this->assertRegExp($pattern, $tpl->getCached()->filepath);
|
|
}
|
|
|
|
/**
|
|
* test getCachedFilepath with cache_id
|
|
*/
|
|
public function testGetCachedFilepathCacheId()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(true);
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar');
|
|
|
|
$pattern = '/.*' . $this->directorySeparator . 'foo' . $this->directorySeparator . 'bar' . $this->directorySeparator . '([a-f0-9]{2}' . $this->directorySeparator . '){3}.*\.php/';
|
|
$this->assertRegExp($pattern, $tpl->getCached()->filepath);
|
|
}
|
|
|
|
/**
|
|
* test getCachedFilepath with compile_id
|
|
*/
|
|
public function testGetCachedFilepathCompileId()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(true);
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', null, 'blar');
|
|
|
|
$pattern = '/.*' . $this->directorySeparator . 'blar' . $this->directorySeparator . '([a-f0-9]{2}' . $this->directorySeparator . '){3}.*\.php/';
|
|
$this->assertRegExp($pattern, $tpl->getCached()->filepath);
|
|
}
|
|
|
|
/**
|
|
* test getCachedFilepath with cache_id and compile_id
|
|
*/
|
|
public function testGetCachedFilepathCacheIdCompileId()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(true);
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
|
|
|
|
$pattern = '/.*' . $this->directorySeparator . 'foo' . $this->directorySeparator . 'bar' . $this->directorySeparator . 'blar' . $this->directorySeparator . '([a-f0-9]{2}' . $this->directorySeparator . '){3}.*\.php/';
|
|
$this->assertRegExp($pattern, $tpl->getCached()->filepath);
|
|
}
|
|
|
|
/**
|
|
* test cache->clear_all with cache_id and compile_id
|
|
*/
|
|
public function testClearCacheAllCacheIdCompileId()
|
|
{
|
|
$this->cleanCacheDir();
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(true);
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertEquals(1, $this->smarty->clearAllCache());
|
|
}
|
|
|
|
/**
|
|
* test cache->clear with cache_id and compile_id
|
|
*/
|
|
public function testClearCacheCacheIdCompileId()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->cleanCacheDir();
|
|
$this->smarty->setUseSubDirs(true);
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl);
|
|
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar2', 'blar');
|
|
$this->writeCachedContent($tpl2);
|
|
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl3);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertEquals(2, $this->smarty->clearCache(null, 'foo|bar'));
|
|
$this->assertFalse(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl3->getCached()->filepath));
|
|
}
|
|
|
|
public function testClearCacheCacheIdCompileId2()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(false);
|
|
$this->cleanCacheDir();
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl);
|
|
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar2', 'blar');
|
|
$this->writeCachedContent($tpl2);
|
|
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl3);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertEquals(2, $this->smarty->clearCache('helloworld.tpl'));
|
|
$this->assertFalse(file_exists($tpl->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
}
|
|
|
|
public function testClearCacheCacheIdCompileId2Sub()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(true);
|
|
$this->cleanCacheDir();
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl);
|
|
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar2', 'blar');
|
|
$this->writeCachedContent($tpl2);
|
|
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl3);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertEquals(2, $this->smarty->clearCache('helloworld.tpl'));
|
|
$this->assertFalse(file_exists($tpl->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
}
|
|
|
|
public function testClearCacheCacheIdCompileId3()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->cleanCacheDir();
|
|
$this->smarty->setUseSubDirs(false);
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl);
|
|
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
|
|
$this->writeCachedContent($tpl2);
|
|
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl3);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertEquals(1, $this->smarty->clearCache('helloworld.tpl', null, 'blar2'));
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
}
|
|
|
|
public function testClearCacheCacheIdCompileId3Sub()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->cleanCacheDir();
|
|
$this->smarty->setUseSubDirs(true);
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl);
|
|
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
|
|
$this->writeCachedContent($tpl2);
|
|
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl3);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertEquals(1, $this->smarty->clearCache('helloworld.tpl', null, 'blar2'));
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
}
|
|
|
|
public function testClearCacheCacheIdCompileId4()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(false);
|
|
$this->cleanCacheDir();
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl);
|
|
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
|
|
$this->writeCachedContent($tpl2);
|
|
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl3);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertEquals(1, $this->smarty->clearCache('helloworld.tpl', null, 'blar2'));
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
}
|
|
|
|
public function testClearCacheCacheIdCompileId4Sub()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(true);
|
|
$this->cleanCacheDir();
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl);
|
|
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
|
|
$this->writeCachedContent($tpl2);
|
|
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl3);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertEquals(1, $this->smarty->clearCache('helloworld.tpl', null, 'blar2'));
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
}
|
|
|
|
public function testClearCacheCacheIdCompileId5()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(false);
|
|
$this->cleanCacheDir();
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl);
|
|
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
|
|
$this->writeCachedContent($tpl2);
|
|
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl3);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertEquals(2, $this->smarty->clearCache(null, null, 'blar'));
|
|
$this->assertFalse(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl3->getCached()->filepath));
|
|
}
|
|
|
|
public function testClearCacheCacheIdCompileId5Sub()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(true);
|
|
$this->cleanCacheDir();
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl);
|
|
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', 'foo|bar', 'blar2');
|
|
$this->writeCachedContent($tpl2);
|
|
$tpl3 = $this->smarty->createTemplate('helloworld2.tpl', 'foo|bar', 'blar');
|
|
$this->writeCachedContent($tpl3);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertEquals(2, $this->smarty->clearCache(null, null, 'blar'));
|
|
$this->assertFalse(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl3->getCached()->filepath));
|
|
}
|
|
|
|
public function testClearCacheCacheFile()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(false);
|
|
$this->cleanCacheDir();
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl');
|
|
$this->writeCachedContent($tpl);
|
|
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', null, 'bar');
|
|
$this->writeCachedContent($tpl2);
|
|
$tpl3 = $this->smarty->createTemplate('helloworld.tpl', 'buh|blar');
|
|
$this->writeCachedContent($tpl3);
|
|
$tpl4 = $this->smarty->createTemplate('helloworld2.tpl');
|
|
$this->writeCachedContent($tpl4);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl4->getCached()->filepath));
|
|
$this->assertEquals(3, $this->smarty->clearCache('helloworld.tpl'));
|
|
$this->assertFalse(file_exists($tpl->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl4->getCached()->filepath));
|
|
}
|
|
|
|
public function testClearCacheCacheFileSub()
|
|
{
|
|
$this->smarty->caching = true;
|
|
$this->smarty->cache_lifetime = 1000;
|
|
$this->smarty->setUseSubDirs(true);
|
|
$this->cleanCacheDir();
|
|
$tpl = $this->smarty->createTemplate('helloworld.tpl');
|
|
$this->writeCachedContent($tpl);
|
|
$tpl2 = $this->smarty->createTemplate('helloworld.tpl', null, 'bar');
|
|
$this->writeCachedContent($tpl2);
|
|
$tpl3 = $this->smarty->createTemplate('helloworld.tpl', 'buh|blar');
|
|
$this->writeCachedContent($tpl3);
|
|
$tpl4 = $this->smarty->createTemplate('helloworld2.tpl');
|
|
$this->writeCachedContent($tpl4);
|
|
$this->assertTrue(file_exists($tpl->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl4->getCached()->filepath));
|
|
$this->assertEquals(3, $this->smarty->clearCache('helloworld.tpl'));
|
|
$this->assertFalse(file_exists($tpl->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl2->getCached()->filepath));
|
|
$this->assertFalse(file_exists($tpl3->getCached()->filepath));
|
|
$this->assertTrue(file_exists($tpl4->getCached()->filepath));
|
|
}
|
|
|
|
/**
|
|
* test cache->clear_all method
|
|
*/
|
|
public function testClearCacheAll()
|
|
{
|
|
$this->cleanCacheDir();
|
|
file_put_contents($this->smarty->getCacheDir() . 'dummy.php', 'test');
|
|
$this->assertEquals(1, $this->smarty->clearAllCache());
|
|
}
|
|
|
|
/**
|
|
* test cache->clear_all method not expired
|
|
*/
|
|
public function testClearCacheAllNotExpired()
|
|
{
|
|
$this->cleanCacheDir();
|
|
file_put_contents($this->smarty->getCacheDir() . 'dummy.php', 'test');
|
|
touch($this->smarty->getCacheDir() . 'dummy.php', time() - 1000);
|
|
clearstatcache();
|
|
$this->assertEquals(0, $this->smarty->clearAllCache(2000));
|
|
}
|
|
|
|
/**
|
|
* test cache->clear_all method expired
|
|
*/
|
|
public function testClearCacheAllExpired()
|
|
{
|
|
$this->cleanCacheDir();
|
|
file_put_contents($this->smarty->getCacheDir() . 'dummy.php', 'test');
|
|
touch($this->smarty->getCacheDir() . 'dummy.php', time() - 1000);
|
|
clearstatcache();
|
|
$this->assertEquals(1, $this->smarty->clearAllCache(500));
|
|
}
|
|
|
|
|
|
private function writeCachedContent($tpl)
|
|
{
|
|
$tpl->writeCachedContent("echo 'hello world';\n");
|
|
}
|
|
}
|