Rework tests.php

Previously this relied on getting all known classes/functions/etc
when it was loaded, and then again after the PHP module being
tested was loaded.  This approach no longer works now we've
stopped loading modules using dl(), so use ReflectionExtension
instead to get information about a specific extension.

This is likely also faster than wading through lists including
everything predefined by PHP.
diff --git a/Examples/test-suite/php/arrays_global_runme.php b/Examples/test-suite/php/arrays_global_runme.php
index 8f28919..d4caff3 100644
--- a/Examples/test-suite/php/arrays_global_runme.php
+++ b/Examples/test-suite/php/arrays_global_runme.php
@@ -4,7 +4,7 @@
 
 check::functions(array('test_a','test_b'));
 check::classes(array('arrays_global','SimpleStruct','Material'));
-check::globals(array('array_c','array_sc','array_uc','array_s','array_us','array_i','array_ui','array_l','array_ul','array_ll','array_f','array_d','array_struct','array_structpointers','array_ipointers','array_enum','array_enumpointers','array_const_i','beginstring_fix44a','beginstring_fix44b','beginstring_fix44c','beginstring_fix44d','beginstring_fix44e','beginstring_fix44f','chitmat','hitmat_val','hitmat'));
+heck::globals(array('array_c','array_sc','array_uc','array_s','array_us','array_i','array_ui','array_l','array_ul','array_ll','array_f','array_d','array_struct','array_structpointers','array_ipointers','array_enum','array_enumpointers','array_const_i','BeginString_FIX44a','BeginString_FIX44b','BeginString_FIX44c','BeginString_FIX44d','BeginString_FIX44e','BeginString_FIX44f','chitMat','hitMat_val','hitMat'));
 
 // The size of array_c is 2, but the last byte is \0, so we can only store a
 // single byte string in it.
diff --git a/Examples/test-suite/php/arrays_global_twodim_runme.php b/Examples/test-suite/php/arrays_global_twodim_runme.php
index ca166fe..e6fbf52 100644
--- a/Examples/test-suite/php/arrays_global_twodim_runme.php
+++ b/Examples/test-suite/php/arrays_global_twodim_runme.php
@@ -4,7 +4,7 @@
 
 check::functions(array('fn_taking_arrays','get_2d_array',));
 check::classes(array('arrays_global_twodim','SimpleStruct','Material'));
-check::globals(array('array_c','array_sc','array_uc','array_s','array_us','array_i','array_ui','array_l','array_ul','array_ll','array_f','array_d','array_struct','array_structpointers','array_ipointers','array_enum','array_enumpointers','array_const_i','chitmat','hitmat_val','hitmat'));
+check::globals(array('array_c','array_sc','array_uc','array_s','array_us','array_i','array_ui','array_l','array_ul','array_ll','array_f','array_d','array_struct','array_structpointers','array_ipointers','array_enum','array_enumpointers','array_const_i','chitMat','hitMat_val','hitMat'));
 
 $a1=array(10,11,12,13);
 $a2=array(14,15,16,17);
diff --git a/Examples/test-suite/php/director_finalizer_runme.php b/Examples/test-suite/php/director_finalizer_runme.php
index ad00bf5..f3a0c2c 100644
--- a/Examples/test-suite/php/director_finalizer_runme.php
+++ b/Examples/test-suite/php/director_finalizer_runme.php
@@ -3,7 +3,7 @@
 require "tests.php";
 
 // New functions
-check::functions(array('deletefoo','getstatus','launder','resetstatus'));
+check::functions(array('deleteFoo','getStatus','launder','resetStatus'));
 // New classes
 check::classes(array('director_finalizer','Foo'));
 // No new vars
diff --git a/Examples/test-suite/php/li_carrays_cpp_runme.php b/Examples/test-suite/php/li_carrays_cpp_runme.php
index aab4543..8f88447 100644
--- a/Examples/test-suite/php/li_carrays_cpp_runme.php
+++ b/Examples/test-suite/php/li_carrays_cpp_runme.php
@@ -3,14 +3,14 @@
 require "tests.php";
 
 // Check functions.
-check::functions(array('new_intarray','delete_intarray','intarray_getitem','intarray_setitem','new_abarray','delete_abarray','abarray_getitem','abarray_setitem','sum_array'));
+check::functions(array('new_intArray','delete_intArray','intArray_getitem','intArray_setitem','new_ABArray','delete_ABArray','ABArray_getitem','ABArray_setitem','sum_Array'));
 
 // Check classes.
 // NB An "li_carrays_cpp" class is created as a mock namespace.
 check::classes(array('li_carrays_cpp','doubleArray','AB','XY','XYArray','shortArray'));
 
 // Check global variables.
-check::globals(array('globalxyarray','globalabarray'));
+check::globals(array('globalXYArray','globalABArray'));
 
 $d = new doubleArray(10);
 
diff --git a/Examples/test-suite/php/li_carrays_runme.php b/Examples/test-suite/php/li_carrays_runme.php
index 9af74c0..a0032cf 100644
--- a/Examples/test-suite/php/li_carrays_runme.php
+++ b/Examples/test-suite/php/li_carrays_runme.php
@@ -3,14 +3,14 @@
 require "tests.php";
 
 // Check functions.
-check::functions(array('new_intarray','delete_intarray','intarray_getitem','intarray_setitem','new_abarray','delete_abarray','abarray_getitem','abarray_setitem','sum_array'));
+check::functions(array('new_intArray','delete_intArray','intArray_getitem','intArray_setitem','new_ABArray','delete_ABArray','ABArray_getitem','ABArray_setitem','sum_Array'));
 
 // Check classes.
 // NB An "li_carrays" class is created as a mock namespace.
 check::classes(array('li_carrays','doubleArray','AB','XY','XYArray','shortArray'));
 
 // Check global variables.
-check::globals(array('globalxyarray','globalabarray'));
+check::globals(array('globalXYArray','globalABArray'));
 
 $d = new doubleArray(10);
 
diff --git a/Examples/test-suite/php/tests.php b/Examples/test-suite/php/tests.php
index 7491bf1..cec6e58 100644
--- a/Examples/test-suite/php/tests.php
+++ b/Examples/test-suite/php/tests.php
@@ -1,77 +1,24 @@
 <?php
 
-$_original_functions=get_defined_functions();
-$_original_globals=1;
-$_original_classes=get_declared_classes();
-$_original_globals=array_keys($GLOBALS);
-
 class check {
   // do we have true global vars or just GETSET functions?
   // Used to filter out get/set global functions to fake vars...
   const GETSET = 1;
 
-  static function get_extra_classes($ref=FALSE) {
-    static $extra;
-    global $_original_classes;
-    if ($ref===FALSE) $f=$_original_classes;
-    if (! is_array($extra)) {
-      $df=array_flip(get_declared_classes());
-      foreach($_original_classes as $class) unset($df[$class]);
-      // Filter out pointer wrappers such as SWIG/_p_int.
-      foreach(array_keys($df) as $class) {
-	if (preg_match('/^SWIG\\\\/', $class)) unset($df[$class]);
-      }
-      $extra=array_keys($df);
-    }
-    return $extra;
-  }
+  private static $_extension = null;
 
-  static function get_extra_functions($ref=FALSE,$gs=false) {
-    static $extra;
-    static $extrags; // for get/setters
-    global $_original_functions;
-    if ($ref===FALSE) $f=$_original_functions;
-    if (! is_array($extra) || $gs) {
-      $extra=array();
-      $extrags=array();
-      $df=get_defined_functions();
-      $df=array_flip($df['internal']);
-      foreach($_original_functions['internal'] as $func) unset($df[$func]);
-      // Now chop out any get/set accessors
-      foreach(array_keys($df) as $func)
-        if ((self::GETSET && preg_match('/_[gs]et$/', $func)))
-          $extrags[]=$func;
-        else $extra[]=$func;
-//      $extra=array_keys($df);
+  // This is called automatically at the end of this file.
+  static function init() {
+    foreach(get_included_files() as $f) {
+      $module_name = preg_filter('/.*\/([^\/]+)_runme\.php$/', '\1', $f);
+      if ($module_name !== null) break;
     }
-    if ($gs) return $extrags;
-    return $extra;
-  }
+    if ($module_name === null) {
+      print("Failed to determine module name from get_included_files()\n");
+      exit(1);
+    }
 
-  static function get_extra_globals($ref=FALSE) {
-    static $extra;
-    global $_original_globals;
-    if (! is_array($extra)) {
-      if (self::GETSET) {
-        $_extra=array();
-        foreach(check::get_extra_functions(false,1) as $global) {
-          if (preg_match('/^(.*)_[sg]et$/', $global, $match))
-            $_extra[$match[1]] = 1;
-        }
-        $extra=array_keys($_extra);
-      } else {
-        if ($ref===FALSE) $ref=$_original_globals;
-        if (! is_array($extra)) {
-          $df=array_flip(array_keys($GLOBALS));
-          foreach($_original_globals as $func) unset($df[$func]);
-          // MASK xxxx_LOADED__ variables
-          foreach(array_keys($df) as $func)
-            if (preg_match('/_LOADED__$/', $func)) unset($df[$func]);
-          $extra=array_keys($df);
-        }
-      }
-    }
-    return $extra;
+    self::$_extension = new ReflectionExtension($module_name);
   }
 
   static function classname($string,$object) {
@@ -139,7 +86,8 @@
     if (! is_array($classes)) $classes=array($classes);
     $message=array();
     $missing=array();
-    $extra=array_flip(check::get_extra_classes());
+    $extra = array_flip(array_filter(self::$_extension->getClassNames(),
+				     function ($e) { return !preg_match('/^SWIG\\\\/', $e); }));
     foreach($classes as $class) {
       if (! class_exists($class)) $missing[]=$class;
       else unset($extra[$class]);
@@ -155,15 +103,16 @@
     if (! is_array($functions)) $functions=array($functions);
     $message=array();
     $missing=array();
-    $extra=array_flip(check::get_extra_functions());
-
+    $extra = self::$_extension->getFunctions();
     foreach ($functions as $func) {
       if (! function_exists($func)) $missing[]=$func;
       else unset($extra[$func]);
     }
+    $extra = array_filter(array_keys($extra),
+			  function ($e) { return !preg_match('/_[gs]et$/', $e); });
     if ($missing) $message[]=sprintf("Functions missing: %s",join(",",$missing));
     if ($message) return check::fail(join("\n  ",$message));
-    if ($extra) $message[]=sprintf("These extra functions are defined: %s",join(",",array_keys($extra)));
+    if ($extra) $message[]=sprintf("These extra functions are defined: %s",join(",",$extra));
     if ($message) return check::warn(join("\n  ",$message));
     return TRUE;    
   }
@@ -172,19 +121,19 @@
     if (! is_array($globals)) $globals=array($globals);
     $message=array();
     $missing=array();
-    $extra=array_flip(check::get_extra_globals());
+    $extra = self::$_extension->getFunctions();
     foreach ($globals as $glob) {
-      if (self::GETSET) {
-        if (! function_exists($glob . "_get") && ! function_exists($glob . "_set")) $missing[]=$glob;
-        else unset($extra[$glob]);
-      } else {
-        if (! isset($GLOBALS[$glob])) $missing[]=$glob;
-        else unset($extra[$glob]);
+      if (! function_exists($glob . "_get") && ! function_exists($glob . "_set")) $missing[]=$glob;
+      else {
+        unset($extra[$glob . "_get"]);
+        unset($extra[$glob . "_set"]);
       }
     }
+    $extra = array_filter(array_keys($extra),
+			  function ($e) { return preg_match('/_[gs]et$/', $e); });
     if ($missing) $message[]=sprintf("Globals missing: %s",join(",",$missing));
     if ($message) return check::fail(join("\n  ",$message));
-    if ($extra) $message[]=sprintf("These extra globals are defined: %s",join(",",array_keys($extra)));
+    if ($extra) $message[]=sprintf("These extra globals are defined: %s",join(",",$extra));
     if ($message) return check::warn(join("\n  ",$message));
     return TRUE;    
 
@@ -226,3 +175,5 @@
 #    print $_SERVER[argv][0]." ok\n";
   }
 }
+
+check::init();
diff --git a/Examples/test-suite/php/valuewrapper_base_runme.php b/Examples/test-suite/php/valuewrapper_base_runme.php
index 29aade4..3f4c38e 100644
--- a/Examples/test-suite/php/valuewrapper_base_runme.php
+++ b/Examples/test-suite/php/valuewrapper_base_runme.php
@@ -3,7 +3,7 @@
 require "tests.php";
 
 check::classes(array("valuewrapper_base","Base","Interface_BP"));
-check::functions("make_interface_bp");
+check::functions("make_Interface_BP");
 
 $ibp=valuewrapper_base::make_interface_bp();
 check::classname("interface_bp",$ibp);
diff --git a/Examples/test-suite/php/wrapmacro_runme.php b/Examples/test-suite/php/wrapmacro_runme.php
index cd4f916..18b2ef1 100644
--- a/Examples/test-suite/php/wrapmacro_runme.php
+++ b/Examples/test-suite/php/wrapmacro_runme.php
@@ -2,7 +2,7 @@
 
 require "tests.php";
 
-check::functions(array('guint16_swap_le_be_constant', 'maximum'));
+check::functions(array('GUINT16_SWAP_LE_BE_CONSTANT', 'maximum'));
 
 check::equal(maximum(2.3, 2.4), 2.4, "maximum() doesn't work");
 check::equal(guint16_swap_le_be_constant(0x1234), 0x3412, "GUINT16_SWAP_LE_BE_CONSTANT() doesn't work");