-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit.tests.php
179 lines (157 loc) · 4.57 KB
/
Unit.tests.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/*
Copyright 2021 Mircerlancerous - https://github.com/mircerlancerous/easyunit-php
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//This class is a sample of what your unit test class should look like
class SampleTests{
public static function PassingTests(){
//Include any files you need for your test here if you didn't already
$check = $val = "abc is def";
TestExecuter::AssertAreEqual($check, $val);
$check = $val = 157382;
TestExecuter::AssertAreEqual($check, $val);
$check = $val = (4763 - 324) . " seconds";
TestExecuter::AssertAreEqual($check, $val);
}
public static function FailingTests(){
//This test will pass
$check = $val = 123;
TestExecuter::AssertAreEqual($check, $val);
//But this test will fail
$val = "not a number";
TestExecuter::AssertAreEqual($check, $val);
//And this one is fine too
$check = $val;
TestExecuter::AssertAreEqual($check, $val);
}
}
//This is the class that handles all the tests
//Include the tests in this file as in above, or include one or more separate files here
class TestExecuter{
//This is a list of all classes that contain tests
//Tests are static methods within the listed classes
private $TestClasses = [
"SampleTests"
];
public static function ExecuteTest($test){
if(!$test){
//run all tests
$obj = new TestExecuter();
$testlist = $obj->GetTestList();
$result = "success";
foreach($testlist as $key => $tests){
foreach($tests as $testname){
$check = self::ExecuteTest($key."_".$testname);
if($check != "success"){
$result = $check;
break;
}
}
}
return $result;
}
list($key, $testname) = explode("_", $test);
//run the test
try{
$key::$testname();
$result = "success";
}
catch(Exception $e){
$result = "<b>$test</b> ".$e->getMessage();
}
return $result;
}
public function GetTestList(){
$list = [];
foreach($this->TestClasses as $key){
$list[$key] = get_class_methods($key);
}
return $list;
}
/**********************************/
//Assertion methods; add more as your needs require
public static function AssertAreEqual($expected, $checkval){
if($expected === $checkval){
return;
}
//values don't match so return some details
throw new Exception("failed<br/><b>Expected</b><br/><pre>$expected</pre><b>Actual</b><pre>$checkval</pre>");
}
public static function AssertAreNotEqual($expected, $checkval){
if($expected !== $checkval){
return;
}
//values match so return some details
throw new Exception("failed<br/><b>Expected</b><br/><pre>$expected</pre><b>Actual</b><i>Identical: not equal was expected</i>");
}
/**********************************/
}
//check if one or more tests are set to be run
if(isset($_GET['test'])){
//there is at least one test, so run and exit
$test = $_GET['test'];
if(!$test){
echo "<h3>Test: Run All</h3>";
}
else{
echo "<h3>Test: $test</h3>";
}
//prepare for the test
$starttime = microtime(true);
$result = TestExecuter::ExecuteTest($test);
//output the result and time elapsed
echo "<h4>Elapsed: ".round(microtime(true) - $starttime, 6)." s</h4>";
echo "Result:<br/>".$result;
exit;
}
//no test(s), so show the unit test interface
?><!doctype html>
<html>
<head>
<title>Unit Tests</title>
<meta charset="utf-8"/>
<style type="text/css">
.righttd{
text-align: center;
}
table{
width: 100%;
}
td{
vertical-align: top;
}
iframe{
margin-left: 25px;
min-height: 500px;
}
</style>
</head>
<body>
<h1>Unit Tests</h1>
<a href="?test" target="results">Run All</a>
<br/>
<table><tr><td>
<?php
$exec = new TestExecuter();
$list = $exec->GetTestList();
foreach($list as $key => $tests){
echo "<h2>$key</h2>";
foreach($tests as $test){
echo ": <a href='?test=".urlencode($key."_".$test)."' target='results'>$test</a><br/>";
}
}
?>
</td><td class="righttd">
<iframe name="results"></iframe>
</td></tr></table>
</body>
</html>