2020-04-13 15:30:52 +02:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Smarty PHPUnit tests default config handler
|
|
|
|
|
*
|
|
|
|
|
* @package PHPunit
|
|
|
|
|
* @author Uwe Tews
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-02 00:49:38 +01:00
|
|
|
use Smarty\Smarty;
|
|
|
|
|
|
2020-04-13 15:30:52 +02:00
|
|
|
/**
|
|
|
|
|
* class for default config handler test
|
|
|
|
|
*
|
|
|
|
|
* @runTestsInSeparateProcess
|
|
|
|
|
* @preserveGlobalState disabled
|
|
|
|
|
* @backupStaticAttributes enabled
|
|
|
|
|
*/
|
|
|
|
|
class DefaultConfigHandlerTest extends PHPUnit_Smarty
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets up the fixture
|
|
|
|
|
* This method is called before a test is executed.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2021-10-13 12:15:17 +02:00
|
|
|
public function setUp(): void
|
2020-04-13 15:30:52 +02:00
|
|
|
{
|
2022-09-27 13:03:34 +03:00
|
|
|
$this->setUpSmarty(__DIR__);
|
2020-04-13 15:30:52 +02:00
|
|
|
$this->smarty->setForceCompile(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test unknown config file
|
|
|
|
|
*/
|
|
|
|
|
public function testUnknownConfigFile()
|
|
|
|
|
{
|
2022-12-22 21:50:01 +01:00
|
|
|
$this->expectException(\Smarty\Exception::class);
|
2021-10-13 12:15:17 +02:00
|
|
|
$this->expectExceptionMessage('Unable to load config \'file:foo.conf\'');
|
2020-04-13 15:30:52 +02:00
|
|
|
$this->smarty->configLoad('foo.conf');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test register unknown default config handler
|
|
|
|
|
*/
|
|
|
|
|
public function testRegisterUnknownDefaultConfigHandler()
|
|
|
|
|
{
|
2022-12-22 21:50:01 +01:00
|
|
|
$this->expectException(\Smarty\Exception::class);
|
2021-10-13 12:15:17 +02:00
|
|
|
$this->expectExceptionMessage('Default config handler');
|
|
|
|
|
$this->expectExceptionMessage('not callable');
|
2020-04-13 15:30:52 +02:00
|
|
|
$this->smarty->registerDefaultConfigHandler('foo');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test default config handler replacement (config data)
|
|
|
|
|
*
|
|
|
|
|
* @throws \Exception
|
2022-12-22 21:50:01 +01:00
|
|
|
* @throws \Smarty\Exception
|
2020-04-13 15:30:52 +02:00
|
|
|
*/
|
|
|
|
|
public function testDefaultConfigHandlerReplacement()
|
|
|
|
|
{
|
|
|
|
|
$this->smarty->registerDefaultConfigHandler('configHandlerData');
|
|
|
|
|
$this->smarty->configLoad('foo.conf');
|
|
|
|
|
$this->assertEquals("bar", $this->smarty->fetch('foo.tpl'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test default config handler replacement (other config file)
|
|
|
|
|
*
|
|
|
|
|
* @throws \Exception
|
2022-12-22 21:50:01 +01:00
|
|
|
* @throws \Smarty\Exception
|
2020-04-13 15:30:52 +02:00
|
|
|
*/
|
|
|
|
|
public function testDefaultConfigHandlerReplacementByConfigFile()
|
|
|
|
|
{
|
|
|
|
|
$this->smarty->registerDefaultConfigHandler('configHandlerFile');
|
|
|
|
|
$this->smarty->configLoad('foo.conf');
|
|
|
|
|
$this->assertEquals("123.4", $this->smarty->fetch('number.tpl'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDefaultConfigHandlerReplacementByConfigFileFail()
|
|
|
|
|
{
|
2022-12-22 21:50:01 +01:00
|
|
|
$this->expectException(\Smarty\Exception::class);
|
2021-10-13 12:15:17 +02:00
|
|
|
$this->expectExceptionMessage("Unable to load config default file 'no.conf' for 'file:fo.conf'");
|
2020-04-13 15:30:52 +02:00
|
|
|
$this->smarty->registerDefaultConfigHandler('configHandlerFile');
|
|
|
|
|
$this->smarty->configLoad('fo.conf');
|
|
|
|
|
$this->assertEquals("123.4", $this->smarty->fetch('number.tpl'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test default config handler replacement (return false)
|
|
|
|
|
*/
|
|
|
|
|
public function testDefaultConfigHandlerReplacementReturningFalse()
|
|
|
|
|
{
|
2022-12-22 21:50:01 +01:00
|
|
|
$this->expectException(\Smarty\Exception::class);
|
2021-10-13 12:15:17 +02:00
|
|
|
$this->expectExceptionMessage('Unable to load config \'file:foo.conf\'');
|
2020-04-13 15:30:52 +02:00
|
|
|
$this->smarty->configLoad('foo.conf');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test default config handler replacement (return false)
|
|
|
|
|
*/
|
|
|
|
|
public function testDefaultConfigHandlerReplacementReturningFalse1()
|
|
|
|
|
{
|
2022-12-22 21:50:01 +01:00
|
|
|
$this->expectException(\Smarty\Exception::class);
|
2021-10-13 12:15:17 +02:00
|
|
|
$this->expectExceptionMessage('No config default content for \'file:bla.conf\'');
|
2020-04-13 15:30:52 +02:00
|
|
|
$this->smarty->registerDefaultConfigHandler('configHandlerData');
|
|
|
|
|
$this->smarty->configLoad('bla.conf');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* config handler returning config data
|
|
|
|
|
*
|
|
|
|
|
* @param $resource_type
|
|
|
|
|
* @param $resource_name
|
|
|
|
|
* @param $config_source
|
|
|
|
|
* @param $config_timestamp
|
|
|
|
|
* @param \Smarty $smarty
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-01-02 00:49:38 +01:00
|
|
|
function configHandlerData($resource_type, $resource_name, &$config_source, &$config_timestamp, \Smarty\Smarty $smarty)
|
2020-04-13 15:30:52 +02:00
|
|
|
{
|
|
|
|
|
if ($resource_name !== 'foo.conf') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$output = "foo = 'bar'\n";
|
|
|
|
|
$config_source = $output;
|
|
|
|
|
$config_timestamp = time();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* config handler returning config file
|
|
|
|
|
*
|
|
|
|
|
* @param $resource_type
|
|
|
|
|
* @param $resource_name
|
|
|
|
|
* @param $config_source
|
|
|
|
|
* @param $config_timestamp
|
|
|
|
|
* @param \Smarty $smarty
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2023-01-02 00:49:38 +01:00
|
|
|
function configHandlerFile($resource_type, $resource_name, &$config_source, &$config_timestamp, \Smarty\Smarty $smarty)
|
2020-04-13 15:30:52 +02:00
|
|
|
{
|
|
|
|
|
if ($resource_name !== 'foo.conf') {
|
|
|
|
|
return 'no.conf';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $smarty->getConfigDir(0) . 'test.conf';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* config handler returning false
|
|
|
|
|
*
|
|
|
|
|
* @param $resource_type
|
|
|
|
|
* @param $resource_name
|
|
|
|
|
* @param $config_source
|
|
|
|
|
* @param $config_timestamp
|
|
|
|
|
* @param \Smarty $smarty
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-01-02 00:49:38 +01:00
|
|
|
function configHandlerFalse($resource_type, $resource_name, &$config_source, &$config_timestamp, \Smarty\Smarty $smarty)
|
2020-04-13 15:30:52 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|