/tags/rel-1-3-29/SWIG/Examples/test-suite/php4/tests.php4

# · PHP · 227 lines · 196 code · 25 blank · 6 comment · 63 complexity · 82edd0555f4bdcc96c230394eb858f52 MD5 · raw file

  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) if (GETSET && ereg('_[gs]et$',$func)) $extrags[]=$func;
  34. else $extra[]=$func;
  35. // $extra=array_keys($df);
  36. }
  37. if ($gs) return $extrags;
  38. return $extra;
  39. }
  40. function get_extra_globals($ref=FALSE) {
  41. static $extra;
  42. global $_original_globals;
  43. if (! is_array($extra)) {
  44. if (GETSET) {
  45. $_extra=array();
  46. foreach(check::get_extra_functions(false,1) as $global) {
  47. if (ereg('^(.*)_[sg]et$',$global,$match)) $_extra[$match[1]]=1;
  48. }
  49. $extra=array_keys($_extra);
  50. } else {
  51. if ($ref===FALSE) $ref=$_original_globals;
  52. if (! is_array($extra)) {
  53. $df=array_flip(array_keys($GLOBALS));
  54. foreach($_original_globals as $func) unset($df[$func]);
  55. // MASK xxxx_LOADED__ variables
  56. foreach(array_keys($df) as $func) if (ereg('_LOADED__$',$func)) unset($df[$func]);
  57. $extra=array_keys($df);
  58. }
  59. }
  60. }
  61. return $extra;
  62. }
  63. function classname($string,$object) {
  64. if ($string!=($classname=get_class($object))) return check::fail("Object: $object is of class %s not class %s",$classname,$string);
  65. return TRUE;
  66. }
  67. function classmethods($classname,$methods) {
  68. if (is_object($classname)) $classname=get_class($classname);
  69. $classmethods=array_flip(get_class_methods($classname));
  70. $missing=array();
  71. $extra=array();
  72. foreach($methods as $method) {
  73. if (! isset($classmethods[$method])) $missing[]=$method;
  74. else unset($classmethods[$method]);
  75. }
  76. $extra=array_keys($classmethods);
  77. if ($missing) $message[]="does not have these methods:\n ".join(",",$missing);
  78. if ($message) {
  79. return check::fail("Class %s %s\nFull class list:\n %s\n",$classname,join("\nbut ",$message),join("\n ",get_class_methods($classname)));
  80. }
  81. if ($extra) $message[]="Class ".$classname." has these extra methods:\n ".join(",",$extra);
  82. if ($message) return check::warn(join("\n ",$message));
  83. return TRUE;
  84. }
  85. function set($var,$value) {
  86. $func=$var."_set";
  87. if (GETSET) $func($value);
  88. else $_GLOBALS[$var]=$value;
  89. }
  90. function &get($var) {
  91. $func=$var."_get";
  92. if (GETSET) return $func();
  93. else return $_GLOBALS[$var];
  94. }
  95. function is_a($a,$b) {
  96. if (is_object($a)) $a=strtolower(get_class($a));
  97. if (is_object($b)) $a=strtolower(get_class($b));
  98. $parents=array();
  99. $c=$a;
  100. while($c!=$b && $c) {
  101. $parents[]=$c;
  102. $c=strtolower(get_parent_class($c));
  103. }
  104. if ($c!=$b) return check::fail("Class $a does not inherit from class $b\nHierachy:\n %s\n",join("\n ",$parents));
  105. return TRUE;
  106. }
  107. function classparent($a,$b) {
  108. if (is_object($a)) $a=get_class($a);
  109. if (is_object($b)) $a=get_class($b);
  110. $parent=get_parent_class($a);
  111. if ($parent!=$b) return check::fail("Class $a parent not actually $b but $parent");
  112. return TRUE;
  113. }
  114. function classes($classes) {
  115. if (! is_array($classes)) $classes=array($classes);
  116. $message=array();
  117. $missing=array();
  118. $extra=array_flip(check::get_extra_classes());
  119. foreach($classes as $class) {
  120. if (! class_exists($class)) $missing[]=$class;
  121. else unset($extra[$class]);
  122. }
  123. if ($missing) $message[]=sprintf("Classes missing: %s",join(",",$missing));
  124. if ($message) return check::fail(join("\n ",$message));
  125. if ($extra) $message[]=sprintf("These extra classes are defined: %s",join(",",array_keys($extra)));
  126. if ($message) return check::warn(join("\n ",$message));
  127. return TRUE;
  128. }
  129. function functions($functions) {
  130. if (! is_array($functions)) $functions=array($functions);
  131. $message=array();
  132. $missing=array();
  133. $extra=array_flip(check::get_extra_functions());
  134. foreach ($functions as $func) {
  135. if (! function_exists($func)) $missing[]=$func;
  136. else unset($extra[$func]);
  137. }
  138. if ($missing) $message[]=sprintf("Functions missing: %s",join(",",$missing));
  139. if ($message) return check::fail(join("\n ",$message));
  140. if ($extra) $message[]=sprintf("These extra functions are defined: %s",join(",",array_keys($extra)));
  141. if ($message) return check::warn(join("\n ",$message));
  142. return TRUE;
  143. }
  144. function globals($globals) {
  145. if (! is_array($globals)) $globals=array($globals);
  146. $message=array();
  147. $missing=array();
  148. $extra=array_flip(check::get_extra_globals());
  149. foreach ($globals as $glob) {
  150. if (GETSET) {
  151. if (! isset($extra[$glob])) $missing[]=$glob;
  152. else unset($extra[$glob]);
  153. } else {
  154. if (! isset($GLOBALS[$glob])) $missing[]=$glob;
  155. else unset($extra[$glob]);
  156. }
  157. }
  158. if ($missing) $message[]=sprintf("Globals missing: %s",join(",",$missing));
  159. if ($message) return check::fail(join("\n ",$message));
  160. if ($extra) $message[]=sprintf("These extra globals are defined: %s",join(",",array_keys($extra)));
  161. if ($message) return check::warn(join("\n ",$message));
  162. return TRUE;
  163. }
  164. function functionref($a,$type,$message) {
  165. if (! eregi("^_[a-f0-9]+$type$",$a)) return check::fail($message);
  166. return TRUE;
  167. }
  168. function equal($a,$b,$message) {
  169. if (! ($a===$b)) return check::fail($message);
  170. return TRUE;
  171. }
  172. function resource($a,$b,$message) {
  173. $resource=trim(check::var_dump($a));
  174. if (! eregi("^resource\([0-9]+\) of type \($b\)",$resource)) return check::fail($message);
  175. return TRUE;
  176. }
  177. function isnull($a,$message) {
  178. $value=trim(check::var_dump($a));
  179. return check::equal($value,"NULL",$message);
  180. }
  181. function var_dump($arg) {
  182. ob_start();
  183. var_dump($arg);
  184. $result=ob_get_contents();
  185. ob_end_clean();
  186. return $result;
  187. }
  188. function fail($pattern) {
  189. $args=func_get_args();
  190. print("Failed on: ".call_user_func_array("sprintf",$args)."\n");
  191. exit(1);
  192. }
  193. function warn($pattern) {
  194. $args=func_get_args();
  195. print("Warning on: ".call_user_func_array("sprintf",$args)."\n");
  196. return FALSE;
  197. }
  198. function done() {
  199. # print $_SERVER[argv][0]." ok\n";
  200. }
  201. }
  202. ?>