root / drupal7 / sites / all / libraries / simplepie-1.3.1 / tests / oldtests / compat_test_harness.php @ 7295e063
1 |
<?php
|
---|---|
2 |
|
3 |
class Unit_Test2 |
4 |
{ |
5 |
public $test = true; |
6 |
public $name; |
7 |
public $expected; |
8 |
public $result; |
9 |
protected $data; |
10 |
|
11 |
/**
|
12 |
* Set the test name to the class name by default, replacing "_" with " "
|
13 |
*/
|
14 |
public function Unit_Test2() |
15 |
{ |
16 |
$this->name = str_replace('_', ' ', get_class($this)); |
17 |
} |
18 |
|
19 |
/**
|
20 |
* Whether this class is a test
|
21 |
*/
|
22 |
public function is_test() |
23 |
{ |
24 |
return (bool) $this->test; |
25 |
} |
26 |
|
27 |
/**
|
28 |
* Test name
|
29 |
*/
|
30 |
public function name() |
31 |
{ |
32 |
return $this->name; |
33 |
} |
34 |
|
35 |
public function run() |
36 |
{ |
37 |
$this->init();
|
38 |
$this->data();
|
39 |
$this->expected();
|
40 |
$this->test();
|
41 |
} |
42 |
|
43 |
protected function init() {} |
44 |
protected function data() {} |
45 |
protected function expected() {} |
46 |
protected function test() {} |
47 |
} |
48 |
|
49 |
class Unit_Test2_Group |
50 |
{ |
51 |
public $name; |
52 |
public $tests; |
53 |
|
54 |
public function Unit_Test2_Group($name) |
55 |
{ |
56 |
$this->name = $name; |
57 |
} |
58 |
|
59 |
/**
|
60 |
* Add a test (a Unit_Test2 child, or a Unit_Test2_Group)
|
61 |
*
|
62 |
* @param object $test Test to add
|
63 |
*/
|
64 |
public function add($test) |
65 |
{ |
66 |
$this->tests[$test->name][] = $test; |
67 |
} |
68 |
|
69 |
/**
|
70 |
* Remove a test
|
71 |
*
|
72 |
* @param string $name Test name
|
73 |
*/
|
74 |
public function remove($name) |
75 |
{ |
76 |
unset($this->tests[$name]); |
77 |
} |
78 |
|
79 |
/**
|
80 |
* Load tests in folder
|
81 |
*
|
82 |
* This loads all the Unit_Test2 classes within files with the same
|
83 |
* extension as this file within the specified folder
|
84 |
*
|
85 |
* @param string $folder Folder name
|
86 |
*/
|
87 |
public function load_folder($folder) |
88 |
{ |
89 |
static $extension = null; |
90 |
if (!$extension) |
91 |
{ |
92 |
$extension = pathinfo(__FILE__, PATHINFO_EXTENSION); |
93 |
} |
94 |
$files = Unit_Test2_Files::get_files($folder); |
95 |
$count_classes = count(get_declared_classes()); |
96 |
foreach ($files as $file) |
97 |
{ |
98 |
if (is_file($file) && pathinfo($file, PATHINFO_EXTENSION) === $extension) |
99 |
{ |
100 |
include $file; |
101 |
} |
102 |
} |
103 |
$classes = array_slice(get_declared_classes(), $count_classes); |
104 |
foreach ($classes as $class) |
105 |
{ |
106 |
if (is_subclass_of($class, 'Unit_Test2')) |
107 |
{ |
108 |
$class = new $class; |
109 |
if ($class->is_test()) |
110 |
{ |
111 |
$this->add($class); |
112 |
} |
113 |
} |
114 |
} |
115 |
} |
116 |
} |
117 |
|
118 |
/**
|
119 |
* File listing class
|
120 |
*
|
121 |
* @package Unit Test
|
122 |
*/
|
123 |
class Unit_Test2_Files |
124 |
{ |
125 |
/**
|
126 |
* Get a list of files/folders within $dir
|
127 |
*
|
128 |
* @param string $dir Folder to get listing for
|
129 |
* @return array
|
130 |
*/
|
131 |
static public function get_files($dir) |
132 |
{ |
133 |
$files = array(); |
134 |
if ($dh = opendir($dir)) |
135 |
{ |
136 |
while (($file = readdir($dh)) !== false) |
137 |
{ |
138 |
if (substr($file, 0, 1) != '.') |
139 |
{ |
140 |
$files[] = "$dir/$file"; |
141 |
} |
142 |
} |
143 |
closedir($dh); |
144 |
usort($files, array(__CLASS__, 'sort_files')); |
145 |
foreach ($files as $file) |
146 |
{ |
147 |
if (is_dir($file)) |
148 |
{ |
149 |
array_splice($files, array_search($file, $files), 0, Unit_Test2_Files::get_files($file)); |
150 |
} |
151 |
} |
152 |
} |
153 |
return $files; |
154 |
} |
155 |
|
156 |
/**
|
157 |
* Sort files/folders with files listed before inner folders
|
158 |
*
|
159 |
* @param string $a File/folder 1
|
160 |
* @param string $b File/folder 2
|
161 |
* @return int
|
162 |
*/
|
163 |
static public function sort_files($a, $b) |
164 |
{ |
165 |
if (is_dir($a) && is_dir($b)) |
166 |
{ |
167 |
return strnatcmp($a, $b); |
168 |
} |
169 |
else if (is_dir($a)) |
170 |
{ |
171 |
return 1; |
172 |
} |
173 |
else if (is_dir($b)) |
174 |
{ |
175 |
return -1; |
176 |
} |
177 |
else
|
178 |
{ |
179 |
return strnatcmp($a, $b); |
180 |
} |
181 |
} |
182 |
} |