PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/php/tests.php

#
PHP | 232 lines | 201 code | 25 blank | 6 comment | 66 complexity | e3c6191036b01690427b16e7c31a5b37 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <?php
  2. // do we have true global vars or just GETSET functions?
  3. // Used to filter out get/set global functions to fake vars...
  4. define(GETSET,1);
  5. $_original_functions=get_defined_functions();
  6. $_original_globals=1;
  7. $_original_classes=get_declared_classes();
  8. $_original_globals=array_keys($GLOBALS);
  9. class check {
  10. function get_extra_classes($ref=FALSE) {
  11. static $extra;
  12. global $_original_classes;
  13. if ($ref===FALSE) $f=$_original_classes;
  14. if (! is_array($extra)) {
  15. $df=array_flip(get_declared_classes());
  16. foreach($_original_classes as $class) unset($df[$class]);
  17. $extra=array_keys($df);
  18. }
  19. return $extra;
  20. }
  21. function get_extra_functions($ref=FALSE,$gs=false) {
  22. static $extra;
  23. static $extrags; // for get/setters
  24. global $_original_functions;
  25. if ($ref===FALSE) $f=$_original_functions;
  26. if (! is_array($extra) || $gs) {
  27. $extra=array();
  28. $extrags=array();
  29. $df=get_defined_functions();
  30. $df=array_flip($df[internal]);
  31. foreach($_original_functions[internal] as $func) unset($df[$func]);
  32. // Now chop out any get/set accessors
  33. foreach(array_keys($df) as $func)
  34. if ((GETSET && ereg('_[gs]et$',$func)) || ereg('^new_', $func)
  35. || ereg('_(alter|get)_newobject$', $func))
  36. $extrags[]=$func;
  37. else $extra[]=$func;
  38. // $extra=array_keys($df);
  39. }
  40. if ($gs) return $extrags;
  41. return $extra;
  42. }
  43. function get_extra_globals($ref=FALSE) {
  44. static $extra;
  45. global $_original_globals;
  46. if (! is_array($extra)) {
  47. if (GETSET) {
  48. $_extra=array();
  49. foreach(check::get_extra_functions(false,1) as $global) {
  50. if (ereg('^(.*)_[sg]et$',$global,$match)) $_extra[$match[1]]=1;
  51. }
  52. $extra=array_keys($_extra);
  53. } else {
  54. if ($ref===FALSE) $ref=$_original_globals;
  55. if (! is_array($extra)) {
  56. $df=array_flip(array_keys($GLOBALS));
  57. foreach($_original_globals as $func) unset($df[$func]);
  58. // MASK xxxx_LOADED__ variables
  59. foreach(array_keys($df) as $func) if (ereg('_LOADED__$',$func)) unset($df[$func]);
  60. $extra=array_keys($df);
  61. }
  62. }
  63. }
  64. return $extra;
  65. }
  66. function classname($string,$object) {
  67. if (!is_object($object))
  68. return check::fail("The second argument is a " . gettype($object) . ", not an object.");
  69. if (strtolower($string)!=strtolower($classname=get_class($object))) return check::fail("Object: \$object is of class %s not class %s",$classname,$string);
  70. return TRUE;
  71. }
  72. function classmethods($classname,$methods) {
  73. if (is_object($classname)) $classname=get_class($classname);
  74. $classmethods=array_flip(get_class_methods($classname));
  75. $missing=array();
  76. $extra=array();
  77. foreach($methods as $method) {
  78. if (! isset($classmethods[$method])) $missing[]=$method;
  79. else unset($classmethods[$method]);
  80. }
  81. $extra=array_keys($classmethods);
  82. if ($missing) $message[]="does not have these methods:\n ".join(",",$missing);
  83. if ($message) {
  84. return check::fail("Class %s %s\nFull class list:\n %s\n",$classname,join("\nbut ",$message),join("\n ",get_class_methods($classname)));
  85. }
  86. if ($extra) $message[]="Class ".$classname." has these extra methods:\n ".join(",",$extra);
  87. if ($message) return check::warn(join("\n ",$message));
  88. return TRUE;
  89. }
  90. function set($var,$value) {
  91. $func=$var."_set";
  92. if (GETSET) $func($value);
  93. else $_GLOBALS[$var]=$value;
  94. }
  95. function &get($var) {
  96. $func=$var."_get";
  97. if (GETSET) return $func();
  98. else return $_GLOBALS[$var];
  99. }
  100. function is_a($a,$b) {
  101. if (is_object($a)) $a=strtolower(get_class($a));
  102. if (is_object($b)) $a=strtolower(get_class($b));
  103. $parents=array();
  104. $c=$a;
  105. while($c!=$b && $c) {
  106. $parents[]=$c;
  107. $c=strtolower(get_parent_class($c));
  108. }
  109. if ($c!=$b) return check::fail("Class $a does not inherit from class $b\nHierachy:\n %s\n",join("\n ",$parents));
  110. return TRUE;
  111. }
  112. function classparent($a,$b) {
  113. if (is_object($a)) $a=get_class($a);
  114. if (is_object($b)) $a=get_class($b);
  115. $parent=get_parent_class($a);
  116. if ($parent!=$b) return check::fail("Class $a parent not actually $b but $parent");
  117. return TRUE;
  118. }
  119. function classes($classes) {
  120. if (! is_array($classes)) $classes=array($classes);
  121. $message=array();
  122. $missing=array();
  123. $extra=array_flip(check::get_extra_classes());
  124. foreach($classes as $class) {
  125. if (! class_exists($class)) $missing[]=$class;
  126. else unset($extra[$class]);
  127. }
  128. if ($missing) $message[]=sprintf("Classes missing: %s",join(",",$missing));
  129. if ($message) return check::fail(join("\n ",$message));
  130. if ($extra) $message[]=sprintf("These extra classes are defined: %s",join(",",array_keys($extra)));
  131. if ($message) return check::warn(join("\n ",$message));
  132. return TRUE;
  133. }
  134. function functions($functions) {
  135. if (! is_array($functions)) $functions=array($functions);
  136. $message=array();
  137. $missing=array();
  138. $extra=array_flip(check::get_extra_functions());
  139. foreach ($functions as $func) {
  140. if (! function_exists($func)) $missing[]=$func;
  141. else unset($extra[$func]);
  142. }
  143. if ($missing) $message[]=sprintf("Functions missing: %s",join(",",$missing));
  144. if ($message) return check::fail(join("\n ",$message));
  145. if ($extra) $message[]=sprintf("These extra functions are defined: %s",join(",",array_keys($extra)));
  146. if ($message) return check::warn(join("\n ",$message));
  147. return TRUE;
  148. }
  149. function globals($globals) {
  150. if (! is_array($globals)) $globals=array($globals);
  151. $message=array();
  152. $missing=array();
  153. $extra=array_flip(check::get_extra_globals());
  154. foreach ($globals as $glob) {
  155. if (GETSET) {
  156. if (! isset($extra[$glob])) $missing[]=$glob;
  157. else unset($extra[$glob]);
  158. } else {
  159. if (! isset($GLOBALS[$glob])) $missing[]=$glob;
  160. else unset($extra[$glob]);
  161. }
  162. }
  163. if ($missing) $message[]=sprintf("Globals missing: %s",join(",",$missing));
  164. if ($message) return check::fail(join("\n ",$message));
  165. if ($extra) $message[]=sprintf("These extra globals are defined: %s",join(",",array_keys($extra)));
  166. if ($message) return check::warn(join("\n ",$message));
  167. return TRUE;
  168. }
  169. function functionref($a,$type,$message) {
  170. if (! eregi("^_[a-f0-9]+$type$",$a)) return check::fail($message);
  171. return TRUE;
  172. }
  173. function equal($a,$b,$message) {
  174. if (! ($a===$b)) return check::fail($message . ": '$a'!=='$b'");
  175. return TRUE;
  176. }
  177. function resource($a,$b,$message) {
  178. $resource=trim(check::var_dump($a));
  179. if (! eregi("^resource\([0-9]+\) of type \($b\)",$resource)) return check::fail($message);
  180. return TRUE;
  181. }
  182. function isnull($a,$message) {
  183. $value=trim(check::var_dump($a));
  184. return check::equal($value,"NULL",$message);
  185. }
  186. function var_dump($arg) {
  187. ob_start();
  188. var_dump($arg);
  189. $result=ob_get_contents();
  190. ob_end_clean();
  191. return $result;
  192. }
  193. function fail($pattern) {
  194. $args=func_get_args();
  195. print("Failed on: ".call_user_func_array("sprintf",$args)."\n");
  196. exit(1);
  197. }
  198. function warn($pattern) {
  199. $args=func_get_args();
  200. print("Warning on: ".call_user_func_array("sprintf",$args)."\n");
  201. return FALSE;
  202. }
  203. function done() {
  204. # print $_SERVER[argv][0]." ok\n";
  205. }
  206. }
  207. ?>