mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-02 17:34:26 +02:00
unit tests
This commit is contained in:
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Smarty PHPunit tests of modifier
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class for modifier tests
|
||||||
|
*
|
||||||
|
* @runTestsInSeparateProcess
|
||||||
|
* @preserveGlobalState disabled
|
||||||
|
* @backupStaticAttributes enabled
|
||||||
|
*/
|
||||||
|
class PluginModifierImplodeTest extends PHPUnit_Smarty
|
||||||
|
{
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->setUpSmarty(__DIR__);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDefault()
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{""|implode:$v}');
|
||||||
|
$tpl->assign("v", ["1", "2"]);
|
||||||
|
$this->assertEquals("12", $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
public function testWithSeparator()
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{","|implode:$v}');
|
||||||
|
$tpl->assign("v", ["a", "b"]);
|
||||||
|
$this->assertEquals("a,b", $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
public function testInConditional()
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{if implode("", $v) == "abc"}good{else}bad{/if}');
|
||||||
|
$tpl->assign("v", ['a','b','c']);
|
||||||
|
$this->assertEquals("good", $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
public function testInConditionalWithSeparator()
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{if implode("-", $v) == "a-b-c"}good{else}bad{/if}');
|
||||||
|
$tpl->assign("v", ['a','b','c']);
|
||||||
|
$this->assertEquals("good", $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Smarty PHPunit tests of modifier
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class for modifier tests
|
||||||
|
*
|
||||||
|
* @runTestsInSeparateProcess
|
||||||
|
* @preserveGlobalState disabled
|
||||||
|
* @backupStaticAttributes enabled
|
||||||
|
*/
|
||||||
|
class PluginModifierJsonEncodeTest extends PHPUnit_Smarty
|
||||||
|
{
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->setUpSmarty(__DIR__);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataForDefault
|
||||||
|
*/
|
||||||
|
public function testDefault($value, $expected)
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{$v|json_encode}');
|
||||||
|
$tpl->assign("v", $value);
|
||||||
|
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataForDefault
|
||||||
|
*/
|
||||||
|
public function testDefaultAsFunction($value, $expected)
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{json_encode($v)}');
|
||||||
|
$tpl->assign("v", $value);
|
||||||
|
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dataForDefault() {
|
||||||
|
return [
|
||||||
|
["abc", '"abc"'],
|
||||||
|
[["abc"], '["abc"]'],
|
||||||
|
[["abc",["a"=>2]], '["abc",{"a":2}]'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataForForceObject
|
||||||
|
*/
|
||||||
|
public function testForceObject($value, $expected)
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{$v|json_encode:16}');
|
||||||
|
$tpl->assign("v", $value);
|
||||||
|
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataForForceObject
|
||||||
|
*/
|
||||||
|
public function testForceObjectAsFunction($value, $expected)
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{json_encode($v,16)}');
|
||||||
|
$tpl->assign("v", $value);
|
||||||
|
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dataForForceObject() {
|
||||||
|
return [
|
||||||
|
["abc", '"abc"'],
|
||||||
|
[["abc"], '{"0":"abc"}'],
|
||||||
|
[["abc",["a"=>2]], '{"0":"abc","1":{"a":2}}'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Smarty PHPunit tests of modifier
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class for modifier tests
|
||||||
|
*
|
||||||
|
* @runTestsInSeparateProcess
|
||||||
|
* @preserveGlobalState disabled
|
||||||
|
* @backupStaticAttributes enabled
|
||||||
|
*/
|
||||||
|
class PluginModifierSubstrTest extends PHPUnit_Smarty
|
||||||
|
{
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->setUpSmarty(__DIR__);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDefault()
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{$v|substr:1}');
|
||||||
|
$tpl->assign("v", "abc");
|
||||||
|
$this->assertEquals("bc", $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTwoArguments()
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{$v|substr:1:1}');
|
||||||
|
$tpl->assign("v", "abc");
|
||||||
|
$this->assertEquals("b", $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNegativeOffset()
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{$v|substr:-1}');
|
||||||
|
$tpl->assign("v", "abc");
|
||||||
|
$this->assertEquals("c", $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInConditional()
|
||||||
|
{
|
||||||
|
$tpl = $this->smarty->createTemplate('string:{if substr($v, -1) == "c"}good{else}bad{/if}');
|
||||||
|
$tpl->assign("v", "abc");
|
||||||
|
$this->assertEquals("good", $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user