Skip to content

Commit

Permalink
Add unit tests to prove that new json_encode modifier works with spec…
Browse files Browse the repository at this point in the history
…ial characters in both UTF-8 and other encodings
  • Loading branch information
cmanley committed May 16, 2024
1 parent 2f40db9 commit 52878fe
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Smarty PHPunit tests of modifier.
* This file must be saved in Windows-1252 encoding!
*/

namespace UnitTests\TemplateSource\TagTests\PluginModifier;
use PHPUnit_Smarty;

class PluginModifierJsonEncodeTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
\Smarty\Smarty::$_CHARSET = 'cp1252';
}

/**
* @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}]'],
[['€uro',['Schlüssel'=>'Straße']], '["\u20acuro",{"Schl\u00fcssel":"Stra\u00dfe"}]'], # € = x80 in cp1252; ü = xFC in cp1252; ß = xDF in cp1252;
];
}

/**
* @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}}'],
[['€uro'], '{"0":"\u20acuro"}'],
];
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* Smarty PHPunit tests of modifier
* Smarty PHPunit tests of modifier.
* This file must be saved in UTF-8 encoding!
*/

namespace UnitTests\TemplateSource\TagTests\PluginModifier;
Expand Down Expand Up @@ -38,6 +39,7 @@ public function dataForDefault() {
["abc", '"abc"'],
[["abc"], '["abc"]'],
[["abc",["a"=>2]], '["abc",{"a":2}]'],
[['€uro',['Schlüssel'=>'Straße']], '["\u20acuro",{"Schl\u00fcssel":"Stra\u00dfe"}]'], # € = x80 in cp1252; ü = xFC in cp1252; ß = xDF in cp1252;
];
}

Expand Down Expand Up @@ -66,6 +68,7 @@ public function dataForForceObject() {
["abc", '"abc"'],
[["abc"], '{"0":"abc"}'],
[["abc",["a"=>2]], '{"0":"abc","1":{"a":2}}'],
[['€uro'], '{"0":"\u20acuro"}'],
];
}

Expand Down

0 comments on commit 52878fe

Please sign in to comment.