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