]> git.mxchange.org Git - friendica.git/blob - simplepie/test/unit_test/unit_test.php
Add simplepie
[friendica.git] / simplepie / test / unit_test / unit_test.php
1 <?php
2
3 /**
4  * @package Unit Test
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
10  */
11
12 class Unit_Test
13 {
14         var $passed;
15         var $failed;
16         var $success_callback;
17         var $fail_callback;
18         
19         function Unit_Test($success, $fail)
20         {
21                 $this->success_callback = $success;
22                 $this->fail_callback = $fail;
23         }
24         
25         function do_test($callback, $dir, $vars = 'data')
26         {
27                 $files = $this->get_files($dir);
28                 foreach ($files as $file)
29                 {
30                         $istest = true;
31                         $debug = false;
32                         include $file;
33                         if ($istest)
34                         {
35                                 $args = compact($vars);
36                                 $result = call_user_func_array($callback, $args);
37                                 $this->run_test($file, $result === $expected);
38                                 if ($debug)
39                                 {
40                                         var_dump($file, $args, $result, $expected);
41                                 }
42                         }
43                 }
44         }
45         
46         function run_test($file, $success)
47         {
48                 if ($success)
49                 {
50                         $this->passed++;
51                         call_user_func($this->success_callback, $file);
52                 }
53                 else
54                 {
55                         $this->failed++;
56                         call_user_func($this->fail_callback, $file);
57                 }
58         }
59         
60         function passed()
61         {
62                 return $this->passed;
63         }
64         
65         function failed()
66         {
67                 return $this->failed;
68         }
69         
70         function total()
71         {
72                 return $this->passed + $this->failed;
73         }
74         
75         function get_files($dir)
76         {
77                 static $extension = null;
78                 if (!$extension)
79                 {
80                         $extension = pathinfo(__FILE__, PATHINFO_EXTENSION);
81                 }
82                 $files = array();
83                 if ($dh = opendir($dir))
84                 {
85                         while (($file = readdir($dh)) !== false)
86                         {
87                                 if (substr($file, 0, 1) != '.')
88                                 {
89                                         $files[] = "$dir/$file";
90                                 }
91                         }
92                         closedir($dh);
93                         usort($files, array(&$this, 'sort_files'));
94                         foreach ($files as $file)
95                         {
96                                 if (is_dir($file))
97                                 {
98                                         array_splice($files, array_search($file, $files), 0, $this->get_files($file));
99                                 }
100                                 if (pathinfo($file, PATHINFO_EXTENSION) != $extension)
101                                 {
102                                         unset($files[array_search($file, $files)]);
103                                 }
104                         }
105                 }
106                 return $files;
107         }
108         
109         function sort_files(&$a, &$b)
110         {
111                 if (is_dir($a) && is_dir($b) || !(is_dir($a) || is_dir($b)))
112                 {
113                         return strnatcasecmp($a, $b);
114                 }
115                 else if (is_dir($a))
116                 {
117                         return 1;
118                 }
119                 else if (is_dir($b))
120                 {
121                         return -1;
122                 }
123         }
124 }
125
126 ?>