Skip to content

Commit

Permalink
CompileCheck test and extra note on how it works in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wisskid committed Feb 23, 2023
1 parent 09d2657 commit 801d186
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/programmers/api-variables/variable-compile-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ changing), the compile check step is no longer needed. Be sure to set
change this to FALSE and a template file is changed, you will \*not\*
see the change since the template will not get recompiled.

Note that up to Smarty 4.x, Smarty will check for the existence of
the source template even if `$compile_check` is disabled.

If [`$caching`](#variable.caching) is enabled and `$compile_check` is
enabled, then the cache files will get regenerated if an involved
template file or config file was updated.
Expand Down
123 changes: 123 additions & 0 deletions tests/UnitTests/SmartyMethodsTests/CompileCheck/CompileCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Smarty PHPunit tests for compile check
*/
class CompileCheckTest extends PHPUnit_Smarty
{
public $methodName = null;
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
$this->smarty->addTemplateDir('./templates_tmp');
$this->cleanDirs();
}

/**
* generate templates
*/
protected function makeFiles()
{
file_put_contents('./templates_tmp/t1.tpl', 'TPL1');
file_put_contents('./templates_tmp/t2.tpl', 'TPL2');
file_put_contents('./templates_tmp/base.tpl', '{include file="t1.tpl"}{include file="t2.tpl"}');
}
/**
* remove generated templates
*/
protected function removeFiles()
{
unlink('./templates_tmp/t1.tpl');
unlink('./templates_tmp/t2.tpl');
unlink('./templates_tmp/base.tpl');
}

/**
* reset, but leave the files alone
* @return void
*/
private function softResetSmarty() {
$this->smarty = new Smarty();
$this->smarty->addTemplateDir('./templates_tmp');
}

/**
* @group slow
*/
public function testCompileCheckOn0()
{
$this->makeFiles();
$this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl'));

$this->softResetSmarty();
$this->smarty->setCompileCheck(Smarty::COMPILECHECK_ON);

$this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl'));
}

/**
* @group slow
*/
public function testCompileCheckOn1()
{
$this->makeFiles();
$this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl'));

$this->softResetSmarty();
$this->smarty->setCompileCheck(Smarty::COMPILECHECK_ON);

unlink('./templates_tmp/base.tpl');
sleep(1);

$this->expectException(Exception::class);
$this->smarty->fetch('base.tpl');
}

/**
* @group slow
*/
public function testCompileCheckOn2()
{
$this->makeFiles();
$this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl'));

$this->softResetSmarty();
$this->smarty->setCompileCheck(Smarty::COMPILECHECK_ON);

sleep(1);
file_put_contents('./templates_tmp/base.tpl', 'hello');

$this->assertEquals('hello', $this->smarty->fetch('base.tpl'));
}

/**
* @group slow
*/
public function testCompileCheckOff0()
{
$this->makeFiles();
$this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl'));

$this->softResetSmarty();
$this->smarty->setCompileCheck(Smarty::COMPILECHECK_OFF);

$this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl'));
}

/**
* @group slow
*/
public function testCompileCheckOff2()
{
$this->makeFiles();
$this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl'));

$this->softResetSmarty();
$this->smarty->setCompileCheck(Smarty::COMPILECHECK_OFF);

sleep(1);
file_put_contents('./templates_tmp/base.tpl', 'hello');

$this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl'));
}

}

0 comments on commit 801d186

Please sign in to comment.