aboutsummaryrefslogtreecommitdiffstats
path: root/simplepie/test/unit_test/unit_test.php
blob: 0a27a34337da5def70b53342433b1cb62b28b814 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php

/**
 * @package Unit Test
 * @author Geoffrey Sneddon <geoffers@gmail.com>
 * @version $Id: unit_test.php 6 2007-04-23 15:15:40Z gsnedders $
 * @license http://www.opensource.org/licenses/zlib-license.php zlib/libpng license
 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
 * @copyright Copyright © 2007, Geoffrey Sneddon
 */

class Unit_Test
{
	var $passed;
	var $failed;
	var $success_callback;
	var $fail_callback;
	
	function Unit_Test($success, $fail)
	{
		$this->success_callback = $success;
		$this->fail_callback = $fail;
	}
	
	function do_test($callback, $dir, $vars = 'data')
	{
		$files = $this->get_files($dir);
		foreach ($files as $file)
		{
			$istest = true;
			$debug = false;
			include $file;
			if ($istest)
			{
				$args = compact($vars);
				$result = call_user_func_array($callback, $args);
				$this->run_test($file, $result === $expected);
				if ($debug)
				{
					var_dump($file, $args, $result, $expected);
				}
			}
		}
	}
	
	function run_test($file, $success)
	{
		if ($success)
		{
			$this->passed++;
			call_user_func($this->success_callback, $file);
		}
		else
		{
			$this->failed++;
			call_user_func($this->fail_callback, $file);
		}
	}
	
	function passed()
	{
		return $this->passed;
	}
	
	function failed()
	{
		return $this->failed;
	}
	
	function total()
	{
		return $this->passed + $this->failed;
	}
	
	function get_files($dir)
	{
		static $extension = null;
		if (!$extension)
		{
			$extension = pathinfo(__FILE__, PATHINFO_EXTENSION);
		}
		$files = array();
		if ($dh = opendir($dir))
		{
			while (($file = readdir($dh)) !== false)
			{
				if (substr($file, 0, 1) != '.')
				{
					$files[] = "$dir/$file";
				}
			}
			closedir($dh);
			usort($files, array(&$this, 'sort_files'));
			foreach ($files as $file)
			{
				if (is_dir($file))
				{
					array_splice($files, array_search($file, $files), 0, $this->get_files($file));
				}
				if (pathinfo($file, PATHINFO_EXTENSION) != $extension)
				{
					unset($files[array_search($file, $files)]);
				}
			}
		}
		return $files;
	}
	
	function sort_files(&$a, &$b)
	{
		if (is_dir($a) && is_dir($b) || !(is_dir($a) || is_dir($b)))
		{
			return strnatcasecmp($a, $b);
		}
		else if (is_dir($a))
		{
			return 1;
		}
		else if (is_dir($b))
		{
			return -1;
		}
	}
}

?>