Files
smarty/tests/UnitTests/ResourceTests/Php/PhpResourceTest.php
Simon Wisselink 39b69f0142 Feature/php8 support (#629)
Adds support for PHP8.0, dropping support for PHP7.0 and below.

Backwards incompatible changes:
- Dropped support for php asp tags in templates (removed from php since php7.0)
- Dropped deprecated API calls that where only accessible through SmartyBC
- Dropped support for {php} and {include_php} tags and embedded PHP in templates. Embedded PHP will now be passed through as is.
- Removed all PHP_VERSION_ID and compare_version checks and conditional code blocks that are now no longer required
- Dropped deprecated SMARTY_RESOURCE_CHAR_SET and SMARTY_RESOURCE_DATE_FORMAT constants
- Dropped deprecated Smarty::muteExpectedErrors and Smarty::unmuteExpectedErrors API methods
- Dropped deprecated $smarty->getVariable() method. Use $smarty->getTemplateVars() instead.
- $smarty->registerResource() no longer accepts an array of callback functions

See the changelog for more details.

Switched CI from Travis to Github CI.
2021-10-13 12:15:17 +02:00

302 lines
8.7 KiB
PHP

<?php
/**
* Smarty PHPunit tests for PHP resources
*
* @package PHPunit
* @author Uwe Tews
*/
/**
* class for PHP resource tests
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class PhpResourceTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(dirname(__FILE__));
}
public function testInit()
{
$this->cleanDirs();
}
protected function relative($path)
{
$path = str_replace(str_replace("\\", "/", dirname(__FILE__)), '.', str_replace("\\", "/", $path));
return $path;
}
/**
* test getTemplateFilepath
*/
public function testGetTemplateFilepath()
{
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertEquals($this->normalizePath("./templates/phphelloworld.php"), $tpl->source->filepath);
}
/**
* test getTemplateTimestamp
*/
public function testGetTemplateTimestamp()
{
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertTrue(is_integer($tpl->source->getTimeStamp()));
$this->assertEquals(10, strlen($tpl->source->getTimeStamp()));
}
/**
* test getTemplateSource
*-/
* public function testGetTemplateSource()
* {
* $tpl = $this->smarty->createTemplate('php:phphelloworld.php');
* $this->assertStringContainsString('php hello world', $tpl->source->getContent());
* }
* /**
* test usesCompiler
*/
public function testUsesCompiler()
{
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertTrue($tpl->source->handler->uncompiled);
}
/**
* test isEvaluated
*/
public function testIsEvaluated()
{
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertFalse($tpl->source->handler->recompiled);
}
/**
* test mustCompile
*/
public function testMustCompile()
{
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertFalse($tpl->mustCompile());
}
/**
* test getCachedFilepath
*/
public function testGetCachedFilepath()
{
$this->smarty->setAllowPhpTemplates(true);
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$expected = $this->buildCachedPath($tpl, false, null, null, 'phphelloworld.php', 'php',
$this->smarty->getTemplateDir(0), 'file');
$this->assertEquals($expected, $tpl->cached->filepath);
}
/**
* test create cache file used by the following tests
*/
public function testCreateCacheFile()
{
// create dummy cache file
$this->smarty->setAllowPhpTemplates(true);
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertStringContainsString('php hello world', $this->smarty->fetch($tpl));
}
/**
* test getCachedTimestamp caching enabled
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testGetCachedTimestamp()
{
$this->smarty->setAllowPhpTemplates(true);
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertTrue(is_integer($tpl->cached->timestamp));
$this->assertEquals(10, strlen($tpl->cached->timestamp));
}
/**
* test isCached
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testIsCached()
{
$this->smarty->setAllowPhpTemplates(true);
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 10000;
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertTrue($tpl->isCached());
}
/**
* test isCached caching disabled
*/
public function testIsCachedCachingDisabled()
{
$this->smarty->setAllowPhpTemplates(true);
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertFalse($tpl->isCached());
}
/**
* test isCached on touched source
*
* @runInSeparateProcess
* @preserveGlobalState disabled
* @doesNotPerformAssertions
*/
public function testIsCachedTouchedSourcePrepare()
{
$this->smarty->setAllowPhpTemplates(true);
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
sleep(2);
touch($tpl->source->filepath);
}
/**
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testIsCachedTouchedSource()
{
$this->smarty->setAllowPhpTemplates(true);
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertFalse($tpl->isCached());
}
/**
* test is cache file is written
*/
public function testWriteCachedContent()
{
$this->smarty->setAllowPhpTemplates(true);
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$this->cleanCacheDir();
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->smarty->fetch($tpl);
$this->assertTrue(file_exists($tpl->cached->filepath));
}
/**
* test getRenderedTemplate
*/
public function testGetRenderedTemplate()
{
$this->smarty->setAllowPhpTemplates(true);
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertStringContainsString('php hello world', $tpl->fetch());
}
/**
* test $smarty->is_cached
* @doesNotPerformAssertions
*/
public function testSmartyIsCachedPrepare()
{
// clean up for next tests
$this->cleanCacheDir();
$this->smarty->setAllowPhpTemplates(true);
// prepare files for next test
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->smarty->fetch($tpl);
}
/**
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testSmartyIsCached()
{
$this->smarty->setAllowPhpTemplates(true);
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertTrue($this->smarty->isCached($tpl));
}
/**
* test $smarty->is_cached caching disabled
*/
public function testSmartyIsCachedCachingDisabled()
{
$this->smarty->setAllowPhpTemplates(true);
$tpl = $this->smarty->createTemplate('php:phphelloworld.php');
$this->assertFalse($this->smarty->isCached($tpl));
}
public function testGetTemplateFilepathName()
{
$this->smarty->addTemplateDir('./templates_2', 'foo');
$tpl = $this->smarty->createTemplate('php:[foo]helloworld.php');
$this->assertEquals('./templates_2/helloworld.php', $this->relative($tpl->source->filepath));
}
public function testGetCachedFilepathName()
{
$this->smarty->addTemplateDir('./templates_2', 'foo');
$this->smarty->caching = true;
$this->smarty->cache_lifetime = 1000;
$tpl = $this->smarty->createTemplate('php:[foo]helloworld.php');
$path = $tpl->cached->filepath;
$expected = $this->buildCachedPath($tpl, false, null, null, 'helloworld.php', 'php',
$this->smarty->getTemplateDir('foo'), 'file');
$this->assertEquals($expected, $path);
}
/**
* test {include} php resource
*/
public function testIncludePhpTemplate()
{
$this->smarty->setAllowPhpTemplates(true);
$this->assertStringContainsString('php hello world', $this->smarty->fetch('includephp.tpl'));
}
/**
* test {include} php resource caching
*/
public function testIncludePhpTemplateCaching()
{
$this->smarty->caching = true;
$this->smarty->setAllowPhpTemplates(true);
$this->assertStringContainsString('php hello world', $this->smarty->fetch('includephp.tpl'));
}
/**
* test clearCompiledTemplate()
*/
public function testClearCompiled()
{
$this->smarty->setAllowPhpTemplates(true);
$this->assertEquals(0, $this->smarty->clearCompiledTemplate('php:phphelloworld.php'));
}
}