PageRenderTime 54ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/Demo/gb_test.php

https://bitbucket.org/gbouthenot/gbphpdb
PHP | 186 lines | 128 code | 44 blank | 14 comment | 0 complexity | 026f1c6c932dc2e95141fd2905e84458 MD5 | raw file
  1. <?php
  2. //require_once("Zend/Exception.php");
  3. require_once("Zend/Loader.php");
  4. require_once("../Gb/Util.php");
  5. Gb_Util::$debug=1;
  6. Gb_Response::$noFooterEscape=1;
  7. Gb_Log::$loglevel_showuser=Gb_Log::LOG_ALL;
  8. Gb_Log::$loglevel_file=Gb_Log::LOG_NOTICE;
  9. Gb_Log::$loglevel_footer=Gb_Log::LOG_ALL;
  10. Gb_Log::$logFilename="demo.log";
  11. Gb_Util::startup();
  12. function main()
  13. {
  14. Gb_Response::$head["author"]=array("name", "Gilles Bouthenot");
  15. Gb_Response::send_headers(1);
  16. echo "this is main\n";
  17. echo "url is: ".$_SERVER['PHP_SELF'].Gb_Util::url_debug("&")."\n";
  18. Gb_Log::log(Gb_Log::LOG_DEBUG, "*** FOOTER ***:");
  19. echo "Connexion avec Gb_Db --> Pdo_Mysql\n";
  20. $dbProt=new Gb_Db(array("type"=>"Pdo_Mysql", "host"=>"pollux3" , "name"=>"test", "user"=>"test", "pass"=>"testneon"));
  21. $date=$dbProt->retrieve_one("SELECT NOW()");
  22. print_r($date);
  23. //print_r($dbProt->getTablesName());
  24. echo "Fin de connexion\n";
  25. echo "Connexion avec Gb_Db --> Pdo_Oci\n";
  26. $dbProt=new Gb_Db(array("type"=>"Pdo_Oci", "host"=>"", "name"=>"PROTHEE99", "user"=>"gbouthenot", "pass"=>"angel"));
  27. $date=$dbProt->retrieve_one("SELECT SYSDATE FROM DUAL");
  28. print_r($date);
  29. print_r($dbProt->getTables());
  30. echo "Fin de connexion\n";
  31. $c=array(0=>"a", "b"=>array(0=>"c", "d"=>"e"));
  32. $d=array("a", "b", "c", 3=>"d", 5=>"e");
  33. //demo de dump:
  34. Gb_Log::log(Gb_Log::LOG_DEBUG, "début des tests : dump");
  35. $ret=Gb_Log::dump($d); echo ($ret=="array('a', 'b', 'c', 'd', 5=>'e')" ?"OK":"ERROR")." dump array: $ret\n";
  36. $ret=Gb_Log::dump($c); echo ($ret=="array('a', b=>array('c', d=>'e'))" ?"OK":"ERROR")." dump array of array: $ret\n";
  37. $ret=Gb_Log::dump("toto"); echo ($ret=="'toto'" ?"OK":"ERROR")." dump string: $ret\n";
  38. //demo de call
  39. Gb_Log::log(Gb_Log::LOG_DEBUG, "début des tests : call");
  40. $ret=Gb_Log::log_function(Gb_Log::LOG_DEBUG, "", array("Gb_Log","dump"), array($d)); echo ($ret=="array('a', 'b', 'c', 'd', 5=>'e')" ?"OK":"ERROR")." call of static function: $ret\n";
  41. $b="myB"; $c=array(5=>'my5', 'toto'=>'myToto');
  42. $ret=Gb_Log::log_function(Gb_Log::LOG_DEBUG, "", "testfunction", array("myA", $b, &$c)); echo ($ret=="a=myA b=myB c[5]=my5 c[toto]=myToto"?"OK":"ERROR")." call: $ret\n";
  43. echo ($b=='myB'&&$c[5]=="new5" ?"OK":"ERROR")." args passed by reference: b=$b c[5]=".$c[5]."\n";
  44. $catched=0;
  45. try
  46. {
  47. $c=array(5=>'my5', 'toto'=>'myToto');
  48. Gb_Log::log_function(Gb_Log::LOG_DEBUG, "", "testfunction3", array("myA", "myB", &$c));
  49. echo "b=$b c[5]=".$c[5]."\n";
  50. }catch (Gb_Exception $e)
  51. { $catched=1;
  52. }
  53. echo ($catched==1 ?"OK":"ERROR")." exception thrown\n";
  54. //demo de Gb_Timer
  55. Gb_Log::log(Gb_Log::LOG_DEBUG, "début des tests : Gb_Timer");
  56. $t1=new Gb_Timer("Timer A");
  57. $t2=new Gb_Timer();
  58. $t3=new Gb_Timer();
  59. $ret1=$t1->get();$ret2=$t2->get();$ret3=$t3->get(null); echo ($ret1>0&&$ret1<1&&$ret2>0&&$ret2<1&&$ret3>0&&$ret3<1?"OK":"ERROR")." t1: $ret1 t2: $t2 t3: $t3\n";
  60. $t1->logTimer();
  61. $t3->pause();
  62. sleep(1);
  63. $ret1=$t1->get();$ret2=$t2->get();$ret3=$t3->get(null); echo ($ret1>1&&$ret1<2&&$ret2>1&&$ret2<2&&$ret3>0&&$ret3<1?"OK":"ERROR")." t3->pause t1: $t1 t2: $t2 t3: $ret3\n";
  64. $t2->logTimer();
  65. $t2->reset();
  66. $t3->resume();
  67. $ret1=$t1->get();$ret2=$t2->get();$ret3=$t3->get(null); echo ($ret1>1&&$ret1<2&&$ret2>0&&$ret2<1&&$ret3>0&&$ret3<1?"OK":"ERROR")." t2->reset, t3->resume t1: $ret1 t2: $ret2 t3: $t3\n";
  68. //démo de Gb_Util::roundCeil
  69. Gb_Log::log(Gb_Log::LOG_DEBUG, "début des tests : roundCeil");
  70. $ret=Gb_Util::roundCeil(120000 ,2); echo ("$ret"==="120000" ?"OK":"ERROR")." $ret\n";
  71. $ret=Gb_Util::roundCeil(120000.1,2); echo ("$ret"==="130000" ?"OK":"ERROR")." $ret\n";
  72. $ret=Gb_Util::roundCeil(129999.9,2); echo ("$ret"==="130000" ?"OK":"ERROR")." $ret\n";
  73. $ret=Gb_Util::roundCeil(120000 ,3); echo ("$ret"==="120000" ?"OK":"ERROR")." $ret\n";
  74. $ret=Gb_Util::roundCeil(120000.1,3); echo ("$ret"==="121000" ?"OK":"ERROR")." $ret\n";
  75. $ret=Gb_Util::roundCeil(129999.9,3); echo ("$ret"==="130000" ?"OK":"ERROR")." $ret\n";
  76. $ret=Gb_Util::roundCeil(1400,2); echo ("$ret"==="1400" ?"OK":"ERROR")." $ret\n";
  77. $ret=Gb_Util::roundCeil(1400.01,2); echo ("$ret"==="1500" ?"OK":"ERROR")." $ret\n";
  78. $ret=Gb_Util::roundCeil(1400,3); echo ("$ret"==="1400" ?"OK":"ERROR")." $ret\n";
  79. $ret=Gb_Util::roundCeil(1400.01,3); echo ("$ret"==="1410" ?"OK":"ERROR")." $ret\n";
  80. $ret=Gb_Util::roundCeil(140,2); echo ("$ret"==="140" ?"OK":"ERROR")." $ret\n";
  81. $ret=Gb_Util::roundCeil(140.01,2); echo ("$ret"==="150" ?"OK":"ERROR")." $ret\n";
  82. $ret=Gb_Util::roundCeil(140,3); echo ("$ret"==="140" ?"OK":"ERROR")." $ret\n";
  83. $ret=Gb_Util::roundCeil(140.01,3); echo ("$ret"==="141" ?"OK":"ERROR")." $ret\n";
  84. $ret=Gb_Util::roundCeil(14,2); echo ("$ret"==="14" ?"OK":"ERROR")." $ret\n";
  85. $ret=Gb_Util::roundCeil(14.01,2); echo ("$ret"==="15" ?"OK":"ERROR")." $ret\n";
  86. $ret=Gb_Util::roundCeil(14,3); echo ("$ret"==="14" ?"OK":"ERROR")." $ret\n";
  87. $ret=Gb_Util::roundCeil(14.01,3); echo ("$ret"==="14.1" ?"OK":"ERROR")." $ret\n";
  88. $ret=Gb_Util::roundCeil(1.4,2); echo ("$ret"==="1.4" ?"OK":"ERROR")." $ret\n";
  89. $ret=Gb_Util::roundCeil(1.401,2); echo ("$ret"==="1.5" ?"OK":"ERROR")." $ret\n";
  90. $ret=Gb_Util::roundCeil(1.4,3); echo ("$ret"==="1.4" ?"OK":"ERROR")." $ret\n";
  91. $ret=Gb_Util::roundCeil(1.401,3); echo ("$ret"==="1.41" ?"OK":"ERROR")." $ret\n";
  92. $ret=Gb_Util::roundCeil(.14,2); echo ("$ret"==="0.14" ?"OK":"ERROR")." $ret\n";
  93. $ret=Gb_Util::roundCeil(.1401,2); echo ("$ret"==="0.15" ?"OK":"ERROR")." $ret\n";
  94. $ret=Gb_Util::roundCeil(.14,3); echo ("$ret"==="0.14" ?"OK":"ERROR")." $ret\n";
  95. $ret=Gb_Util::roundCeil(.1401,3); echo ("$ret"==="0.141" ?"OK":"ERROR")." $ret\n";
  96. $ret=Gb_Util::roundCeil(.014,2); echo ("$ret"==="0.014" ?"OK":"ERROR")." $ret\n";
  97. $ret=Gb_Util::roundCeil(.01401,2); echo ("$ret"==="0.015" ?"OK":"ERROR")." $ret\n";
  98. $ret=Gb_Util::roundCeil(.014,3); echo ("$ret"==="0.014" ?"OK":"ERROR")." $ret\n";
  99. $ret=Gb_Util::roundCeil(.01401,3); echo ("$ret"==="0.0141" ?"OK":"ERROR")." $ret\n";
  100. $ret=Gb_Util::roundCeil(.0014,2); echo ("$ret"==="0.0014" ?"OK":"ERROR")." $ret\n";
  101. $ret=Gb_Util::roundCeil(.001401,2); echo ("$ret"==="0.0015" ?"OK":"ERROR")." $ret\n";
  102. $ret=Gb_Util::roundCeil(.0014,3); echo ("$ret"==="0.0014" ?"OK":"ERROR")." $ret\n";
  103. $ret=Gb_Util::roundCeil(.001401,3); echo ("$ret"==="0.00141" ?"OK":"ERROR")." $ret\n";
  104. $ret=Gb_Util::roundCeil(.00014,2); echo ("$ret"==="0.00014" ?"OK":"ERROR")." $ret\n";
  105. $ret=Gb_Util::roundCeil(.0001401,2); echo ("$ret"==="0.00015" ?"OK":"ERROR")." $ret\n";
  106. $ret=Gb_Util::roundCeil(.00014,3); echo ("$ret"==="0.00014" ?"OK":"ERROR")." $ret\n";
  107. $ret=Gb_Util::roundCeil(.0001401,3); echo ("$ret"==="0.000141"?"OK":"ERROR")." $ret\n";
  108. $t3->reset();
  109. $t3->logTimer(Gb_Log::LOG_DEBUG, "reset");
  110. Gb_Log::log(Gb_Log::LOG_DEBUG, "début des tests : Gb_Db");
  111. $dbGE=new Gb_Db(array("driver"=>"Pdo_Mysql", "host"=>"5.157.252.127", "user"=>"test", "pass"=>"testneon", "name"=>"horde"));
  112. $t3->logTimer(Gb_Log::LOG_DEBUG, "new Gb_Db");
  113. $dbGE->beginTransaction();
  114. // $dbGE->delete("horde_prefs", array());
  115. // $dbGE->insert("horde_prefs", array("pref_uid"=>"root", "pref_scope"=>"scope1", "pref_name"=>"name1", "pref_value"=>"value1"));
  116. // $dbGE->update("horde_prefs", array("pref_value"=>"toto"), array());
  117. //todo:escape
  118. // $dbGE->update("horde_prefs", array("pref_value"=>"toto"), array("pref_name=".$dbGE->quote("last_login));
  119. // Doit générer une exception si on n'a pas fait delete:
  120. // $dbGE->replace("horde_prefs", array("pref_value"=>"toto"), array($dbGE->quoteInto("pref_name=?","last_login")));
  121. // $dbGE->replace("horde_prefs", array("pref_value"=>"toto", "pref_scope"=>"scope2"), array("pref_uid='root'", "pref_name='last_login'"));
  122. $sql="select * FROM horde_prefs WHERE pref_uid=?";
  123. $sqlparam=array('root');
  124. $dbGE->retrieve_all($sql, $sqlparam);
  125. $t3->logTimer(Gb_Log::LOG_DEBUG, "retrieve_all");
  126. $r=Gb_Log::log_function(Gb_Log::LOG_DEBUG, "", array($dbGE, "retrieve_all"), array($sql, $sqlparam));
  127. echo "<pre>".print_r($r, true)."</pre>";
  128. $r=Gb_Log::log_function(Gb_Log::LOG_DEBUG, "", array($dbGE, "retrieve_all"), array($sql, $sqlparam, "pref_name"));
  129. echo "<pre>".print_r($r, true)."</pre>";
  130. $r=Gb_Log::log_function(Gb_Log::LOG_DEBUG, "", array($dbGE, "retrieve_all"), array($sql, $sqlparam, "pref_name", "pref_value"));
  131. echo "<pre>".print_r($r, true)."</pre>";
  132. $dbGE->rollBack();
  133. return "OK";
  134. }
  135. echo "Toujours là !";
  136. function testfunction($a='', $b='', &$c='')
  137. {
  138. $ret="a=$a b=$b c[5]=".$c[5]." c[toto]=".$c['toto'];
  139. $b="newB";
  140. $c[5]="new5";
  141. return $ret;
  142. }