JsonLibTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /* Copyright (C) 2010-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/JsonLibTest.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. //require_once 'PHPUnit/Autoload.php';
  27. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1');
  29. if (! defined('NOREQUIREDB')) define('NOREQUIREDB', '1');
  30. if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
  31. if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1');
  32. if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1');
  33. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1');
  34. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1'); // If there is no menu to show
  35. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  36. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
  37. if (! defined("NOLOGIN")) define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  38. /**
  39. * Class for PHPUnit tests
  40. *
  41. * @backupGlobals disabled
  42. * @backupStaticAttributes enabled
  43. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  44. */
  45. class JsonLibTest extends PHPUnit\Framework\TestCase
  46. {
  47. protected $savconf;
  48. protected $savuser;
  49. protected $savlangs;
  50. protected $savdb;
  51. /**
  52. * Constructor
  53. * We save global variables into local variables
  54. *
  55. * @return CoreTest
  56. */
  57. public function __construct()
  58. {
  59. parent::__construct();
  60. //$this->sharedFixture
  61. global $conf,$user,$langs,$db;
  62. $this->savconf=$conf;
  63. $this->savuser=$user;
  64. $this->savlangs=$langs;
  65. $this->savdb=$db;
  66. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  67. //print " - db ".$db->db;
  68. print "\n";
  69. }
  70. /**
  71. * setUpBeforeClass
  72. *
  73. * @return void
  74. */
  75. public static function setUpBeforeClass()
  76. {
  77. global $conf,$user,$langs,$db;
  78. //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  79. print __METHOD__."\n";
  80. }
  81. /**
  82. * tearDownAfterClass
  83. *
  84. * @return void
  85. */
  86. public static function tearDownAfterClass()
  87. {
  88. global $conf,$user,$langs,$db;
  89. //$db->rollback();
  90. print __METHOD__."\n";
  91. }
  92. /**
  93. * Init phpunit tests
  94. *
  95. * @return void
  96. */
  97. protected function setUp()
  98. {
  99. global $conf,$user,$langs,$db;
  100. $conf=$this->savconf;
  101. $user=$this->savuser;
  102. $langs=$this->savlangs;
  103. $db=$this->savdb;
  104. print __METHOD__."\n";
  105. }
  106. /**
  107. * End phpunit tests
  108. *
  109. * @return void
  110. */
  111. protected function tearDown()
  112. {
  113. print __METHOD__."\n";
  114. }
  115. /**
  116. * testJsonEncode
  117. *
  118. * @return void
  119. */
  120. public function testJsonEncode()
  121. {
  122. //$this->sharedFixture
  123. global $conf,$user,$langs,$db;
  124. $this->savconf=$conf;
  125. $this->savuser=$user;
  126. $this->savlangs=$langs;
  127. $this->savdb=$db;
  128. // Do a test with an array starting with 0
  129. $arraytotest=array(0=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "'));
  130. $arrayencodedexpected='[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]';
  131. $encoded=json_encode($arraytotest);
  132. $this->assertEquals($arrayencodedexpected, $encoded);
  133. $decoded=json_decode($encoded, true);
  134. $this->assertEquals($arraytotest, $decoded, 'test for json_xxx');
  135. $encoded=dol_json_encode($arraytotest);
  136. $this->assertEquals($arrayencodedexpected, $encoded);
  137. $decoded=dol_json_decode($encoded, true);
  138. $this->assertEquals($arraytotest, $decoded, 'test for dol_json_xxx');
  139. // Same test but array start with 2 instead of 0
  140. $arraytotest=array(2=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "'));
  141. $arrayencodedexpected='{"2":{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}}';
  142. $encoded=json_encode($arraytotest);
  143. $this->assertEquals($arrayencodedexpected, $encoded);
  144. $decoded=json_decode($encoded, true);
  145. $this->assertEquals($arraytotest, $decoded, 'test for json_xxx');
  146. $encoded=dol_json_encode($arraytotest);
  147. $this->assertEquals($arrayencodedexpected, $encoded);
  148. $decoded=dol_json_decode($encoded, true);
  149. $this->assertEquals($arraytotest, $decoded, 'test for dol_json_xxx');
  150. // Test with object
  151. $now=gmmktime(12, 0, 0, 1, 1, 1970);
  152. $objecttotest=new stdClass();
  153. $objecttotest->property1='abc';
  154. $objecttotest->property2=1234;
  155. $objecttotest->property3=$now;
  156. $encoded=dol_json_encode($objecttotest);
  157. $this->assertEquals('{"property1":"abc","property2":1234,"property3":43200}', $encoded);
  158. }
  159. }