mirror of
https://github.com/smarty-php/smarty.git
synced 2026-07-11 02:40:49 +02:00
7408c18cdc
Removed $smarty->_current_file and $smarty->allow_ambiguous_resources properties, both unused. Removed public Source::filepath property.
Cached an Compiled files (and Exceptions) no longer rely on the filepath being set. Removed explicit tests for cached and compiled filenames. The exact implementation is not important. Added tests for compile_check property, fixing a file_exists check that would always be done on source template files, even when compile_check was true. Remove code duplication between Source en Config classes. Added a local $_smarty_current_dir to the generated code files for backwards compatability for {$smarty.current_dir}.
378 lines
16 KiB
PHP
378 lines
16 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
|
|
{
|
|
|
|
public function setUp(): void
|
|
{
|
|
$this->setUpSmarty(__DIR__);
|
|
parent::setUp();
|
|
$this->smarty->setCachingType('filetest');
|
|
}
|
|
|
|
|
|
public function testInit()
|
|
|
|
{
|
|
$this->cleanDirs();
|
|
}
|
|
|
|
/**
|
|
* 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');
|
|
|
|
$this->assertRegExp('/.*\/([a-f0-9]{2}\/){3}.*.php/', $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');
|
|
|
|
$this->assertRegExp('/.*\/foo\/bar\/([a-f0-9]{2}\/){3}.*.php/', $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');
|
|
$this->assertRegExp('/.*\/blar\/([a-f0-9]{2}\/){3}.*.php/', $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');
|
|
$this->assertRegExp('/.*\/foo\/bar\/blar\\/([a-f0-9]{2}\/){3}.*.php/', $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(false);
|
|
$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");
|
|
}
|
|
}
|