-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests to prove that new json_encode modifier works with spec…
…ial characters in both UTF-8 and other encodings
- Loading branch information
Showing
2 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
...s/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeCp1252Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}'], | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters