|
1
|
|
|
<?php |
|
2
|
|
|
require_once "../core/Logger.php"; |
|
3
|
|
|
class PUnit{ |
|
4
|
|
|
function assertEquals($value,$expected,$info = NULL){ |
|
|
|
|
|
|
5
|
|
|
if($value != $expected){ |
|
6
|
|
|
throw new Exception($info." expected ".$expected." but ".$value); |
|
7
|
|
|
} |
|
8
|
|
|
} |
|
9
|
|
|
function run($torun = NULL){ |
|
|
|
|
|
|
10
|
|
|
$r = new ReflectionClass($this); |
|
11
|
|
|
foreach($r->getMethods() as $key=>$methodObj){ |
|
12
|
|
|
if($methodObj->isPrivate()) |
|
13
|
|
|
$methods[$key]['type'] = 'private'; |
|
14
|
|
|
elseif($methodObj->isProtected()) |
|
15
|
|
|
$methods[$key]['type'] = 'protected'; |
|
16
|
|
|
else |
|
17
|
|
|
$methods[$key]['type'] = 'public'; |
|
18
|
|
|
$methods[$key]['name'] = $methodObj->name; |
|
19
|
|
|
$methods[$key]['class'] = $methodObj->class; |
|
20
|
|
|
} |
|
21
|
|
|
$before = NULL; |
|
22
|
|
|
$after = NULL; |
|
|
|
|
|
|
23
|
|
|
foreach ($methods as $method) { |
|
|
|
|
|
|
24
|
|
|
if($method["class"] != "PUnit"&&$method["name"] == "before"){ |
|
25
|
|
|
$before = $method; |
|
26
|
|
|
} |
|
27
|
|
|
if($method["class"] != "PUnit"&&$method["name"] == "after"){ |
|
28
|
|
|
$after = $method; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
$error = array(); |
|
32
|
|
|
$success = array(); |
|
33
|
|
|
foreach ($methods as $method) { |
|
34
|
|
|
if($method["class"] != "PUnit"&&substr($method["name"],0,4) == "test"){ |
|
35
|
|
|
if($torun !== NULL){ |
|
36
|
|
|
if(!in_array($method["name"],$torun)) |
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
try{ |
|
40
|
|
|
if($method["type"] == "public"){ |
|
41
|
|
|
$log = new Logger(); |
|
42
|
|
|
$log->info("Run unit --->".$method["name"]); |
|
43
|
|
|
if($before!=NULL) |
|
44
|
|
|
$this->$before["name"](); |
|
45
|
|
|
$this->$method["name"](); |
|
46
|
|
|
array_push($success,$method["name"]); |
|
47
|
|
|
} |
|
48
|
|
|
}catch(Exception $e){ |
|
49
|
|
|
$error[$method["name"]]="".$e; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
echo "\r\nPHP Unit-----------error"."\r\n"; |
|
54
|
|
|
print_r($error); |
|
55
|
|
|
echo "PHP Unit-----------result"."\r\n"; |
|
56
|
|
|
echo "total:".(count($success)+count($error)).",success:".count($success).",error:".count($error)."\r\n"; |
|
57
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
?> |
|
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.