5 * @author Geoffrey Sneddon <geoffers@gmail.com>
6 * @version $Id: unit_test.php 6 2007-04-23 15:15:40Z gsnedders $
7 * @license http://www.opensource.org/licenses/zlib-license.php zlib/libpng license
8 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
9 * @copyright Copyright © 2007, Geoffrey Sneddon
16 var $success_callback;
19 function Unit_Test($success, $fail)
21 $this->success_callback = $success;
22 $this->fail_callback = $fail;
25 function do_test($callback, $dir, $vars = 'data')
27 $files = $this->get_files($dir);
28 foreach ($files as $file)
35 $args = compact($vars);
36 $result = call_user_func_array($callback, $args);
37 $this->run_test($file, $result === $expected);
40 var_dump($file, $args, $result, $expected);
46 function run_test($file, $success)
51 call_user_func($this->success_callback, $file);
56 call_user_func($this->fail_callback, $file);
72 return $this->passed + $this->failed;
75 function get_files($dir)
77 static $extension = null;
80 $extension = pathinfo(__FILE__, PATHINFO_EXTENSION);
83 if ($dh = opendir($dir))
85 while (($file = readdir($dh)) !== false)
87 if (substr($file, 0, 1) != '.')
89 $files[] = "$dir/$file";
93 usort($files, array(&$this, 'sort_files'));
94 foreach ($files as $file)
98 array_splice($files, array_search($file, $files), 0, $this->get_files($file));
100 if (pathinfo($file, PATHINFO_EXTENSION) != $extension)
102 unset($files[array_search($file, $files)]);
109 function sort_files(&$a, &$b)
111 if (is_dir($a) && is_dir($b) || !(is_dir($a) || is_dir($b)))
113 return strnatcasecmp($a, $b);