From 42c7fc1c8d9b3c58b00b4b11f10b9a5e2d7a4fca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Wed, 30 Jul 2014 15:19:31 +0200 Subject: [PATCH] Revert "Testing dol_json_encode is not interesting now as it uses a core function" --- test/phpunit/JsonLibTest.php | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/test/phpunit/JsonLibTest.php b/test/phpunit/JsonLibTest.php index 67485c118e2..caceb6d0dc3 100755 --- a/test/phpunit/JsonLibTest.php +++ b/test/phpunit/JsonLibTest.php @@ -116,4 +116,57 @@ class JsonLibTest extends PHPUnit_Framework_TestCase print __METHOD__."\n"; } + /** + * testJsonEncode + * + * @return void + */ + public function testJsonEncode() + { + //$this->sharedFixture + global $conf,$user,$langs,$db; + $this->savconf=$conf; + $this->savuser=$user; + $this->savlangs=$langs; + $this->savdb=$db; + + // Do a test with an array starting with 0 + $arraytotest=array(0=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "')); + $arrayencodedexpected='[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]'; + + $encoded=json_encode($arraytotest); + $this->assertEquals($arrayencodedexpected,$encoded); + $decoded=json_decode($encoded,true); + $this->assertEquals($arraytotest,$decoded,'test for json_xxx'); + + $encoded=dol_json_encode($arraytotest); + $this->assertEquals($arrayencodedexpected,$encoded); + $decoded=dol_json_decode($encoded,true); + $this->assertEquals($arraytotest,$decoded,'test for dol_json_xxx'); + + // Same test but array start with 2 instead of 0 + $arraytotest=array(2=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "')); + $arrayencodedexpected='{"2":{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}}'; + + $encoded=json_encode($arraytotest); + $this->assertEquals($arrayencodedexpected,$encoded); + $decoded=json_decode($encoded,true); + $this->assertEquals($arraytotest,$decoded,'test for json_xxx'); + + $encoded=dol_json_encode($arraytotest); + $this->assertEquals($arrayencodedexpected,$encoded); + $decoded=dol_json_decode($encoded,true); + $this->assertEquals($arraytotest,$decoded,'test for dol_json_xxx'); + + // Test with object + $now=gmmktime(12,0,0,1,1,1970); + $objecttotest=new stdClass(); + $objecttotest->property1='abc'; + $objecttotest->property2=1234; + $objecttotest->property3=$now; + $encoded=dol_json_encode($objecttotest); + $this->assertEquals('{"property1":"abc","property2":1234,"property3":43200}',$encoded); + + } + }