mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-12 09:59:53 +01:00
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.
138 lines
3.3 KiB
PHP
138 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* Smarty PHPunit tests stream variables
|
|
*
|
|
* @package PHPunit
|
|
* @author Uwe Tews
|
|
*/
|
|
|
|
/**
|
|
* class for stream variables tests
|
|
*
|
|
* @runTestsInSeparateProcess
|
|
* @preserveGlobalState disabled
|
|
* @backupStaticAttributes enabled
|
|
*/
|
|
class StreamVariableTest extends PHPUnit_Smarty
|
|
{
|
|
public function setUp(): void
|
|
{
|
|
$this->setUpSmarty(dirname(__FILE__));
|
|
|
|
stream_wrapper_register("var", "VariableStream")
|
|
or die("Failed to register protocol");
|
|
$fp = fopen("var://foo", "r+");
|
|
fwrite($fp, 'hello world');
|
|
fclose($fp);
|
|
}
|
|
|
|
public function testInit()
|
|
{
|
|
$this->cleanDirs();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
parent::tearDown();
|
|
stream_wrapper_unregister("var");
|
|
}
|
|
|
|
/**
|
|
* test stream variable
|
|
*/
|
|
public function testStreamVariable1()
|
|
{
|
|
$tpl = $this->smarty->createTemplate('eval:{$var:foo}', null, null, $this->smarty);
|
|
$this->assertEquals('hello world', $this->smarty->fetch($tpl));
|
|
}
|
|
/*
|
|
public function testStreamVariable2()
|
|
{
|
|
$tpl = $this->smarty->createTemplate('eval:{var:\'foo\'}', null, null, $this->smarty);
|
|
$this->assertEquals('hello world', $this->smarty->fetch($tpl));
|
|
}
|
|
|
|
public function testStreamVariable3()
|
|
{
|
|
$tpl = $this->smarty->createTemplate('eval:{var:"foo"}', null, null, $this->smarty);
|
|
$this->assertEquals('hello world', $this->smarty->fetch($tpl));
|
|
}
|
|
*/
|
|
/**
|
|
* test no existent stream variable
|
|
*/
|
|
// public function testStreamVariable2()
|
|
// {
|
|
// $tpl = $this->smarty->createTemplate('eval:{$var:bar}', null, null, $this->smarty);
|
|
// $this->assertEquals('', $this->smarty->fetch($tpl));
|
|
// }
|
|
}
|
|
|
|
class VariableStream
|
|
{
|
|
private $position;
|
|
private $varname;
|
|
|
|
public function stream_open($path, $mode, $options, &$opened_path)
|
|
{
|
|
$url = parse_url($path);
|
|
$this->varname = $url["host"];
|
|
$this->position = 0;
|
|
|
|
return true;
|
|
}
|
|
|
|
public function stream_read($count)
|
|
{
|
|
$p = &$this->position;
|
|
$ret = substr($GLOBALS[$this->varname], $p, $count);
|
|
$p += strlen($ret);
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public function stream_write($data)
|
|
{
|
|
$v = &$GLOBALS[$this->varname];
|
|
$l = strlen($data);
|
|
$p = &$this->position;
|
|
$v = substr($v, 0, $p) . $data . substr($v, $p += $l);
|
|
|
|
return $l;
|
|
}
|
|
|
|
public function stream_tell()
|
|
{
|
|
return $this->position;
|
|
}
|
|
|
|
public function stream_eof()
|
|
{
|
|
return $this->position >= strlen($GLOBALS[$this->varname]);
|
|
}
|
|
|
|
public function stream_seek($offset, $whence)
|
|
{
|
|
$l = strlen($GLOBALS[$this->varname]);
|
|
$p = &$this->position;
|
|
switch ($whence) {
|
|
case SEEK_SET:
|
|
$newPos = $offset;
|
|
break;
|
|
case SEEK_CUR:
|
|
$newPos = $p + $offset;
|
|
break;
|
|
case SEEK_END:
|
|
$newPos = $l + $offset;
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
$ret = ($newPos >= 0 && $newPos <= $l);
|
|
if ($ret) {
|
|
$p = $newPos;
|
|
}
|
|
return $ret;
|
|
}
|
|
}
|