/
phpunit_test.php
268 lines (232 loc) · 7.53 KB
/
phpunit_test.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
require "phpunit.php";
class SelfTestResult extends TextTestResult {
/* Specialize result class for use in self-tests, to handle
special situation where many tests are expected to fail. */
function SelfTestResult() {
$this->TextTestResult();
echo '<table class="details">';
echo '<tr><th>Test name</th><th>Result</th><th>Meta-result</th></tr>';
}
function _startTest($test) {
print('<tr><td>');
if (phpversion() > '4') {
printf("%s - %s ", get_class($test), $test->name());
} else {
printf("%s ", $test->name());
}
print('</td>');
flush();
}
function _endTest($test) {
/* Report both the test result and, for this special situation
where some tests are expected to fail, a "meta" test result
which indicates whether the test result matches the
expected result. */
$expect_failure = preg_match('/fail/i', $test->name());
$test_passed = ($test->failed() == 0);
if ($test->errored())
$outcome = "<span class=\"Error\">ERROR</span>";
else if ($test->failed())
$outcome = "<span class=\"Failure\">FAIL</span>";
else
$outcome = "<span class=\"Pass\">OK</span>";
if ($test->errored())
$meta_outcome = '<span class="Unknown">unknown</span>';
else
$meta_outcome = ($expect_failure xor $test_passed)
? '<span class="Expected">as expected</span>'
: '<span class="Unexpected">UNEXPECTED</span>';
printf("<td>$outcome</td><td>$meta_outcome</td></tr>\n");
flush();
}
}
class TestFixture extends TestCase {
function TestFixture($name) {
$this->TestCase($name);
}
function setUp() {
/* put any common setup here */
$this->intVal = 1;
$this->strVal = 'foo';
}
function testFail1() {
$this->assert($this->intVal == 0, "1 == 0");
}
function testFail2() {
$this->assert($this->strVal == 'bar');
}
function testPass1() {
$this->assert($this->intVal == 1);
}
}
$suite = new TestSuite;
$suite->addTest(new TestFixture("testFail1"));
$suite->addTest(new TestFixture("testFail2"));
$suite->addTest(new TestFixture("testPass1"));
//$suite->addTest(new TestFixture("testNotExistFail"));
class Fixture2 extends TestCase {
function Fixture2($name) {
$this->TestCase($name);
}
function setUp() {
$this->str1 = 'foo';
$this->str2 = 'bar';
}
function runTest() {
$this->testStrNotEqual();
$this->testStrAppend();
}
function testStrNotEqual() {
$this->assert($this->str1 == $this->str2, 'str equal');
}
function testStrAppend() {
$this->assertEquals($this->str1 . 'bar', 'foobars', 'str append');
}
}
$suite->addTest(new Fixture2("Fail3"));
class TestPass2 extends TestFixture {
function TestPass2($name) { $this->TestFixture($name); }
function runTest() {
$this->assertEquals($this->strVal . 'x', $this->strVal . 'x');
$this->assertEquals($this->strVal . 'x', $this->strVal . 'y');
$this->assertEquals(1, 0);
$this->assertEquals(1, "1", 'equals int and str');
}
}
$suite->addTest(new TestPass2("Fail4"));
class MoreTesterTests extends TestCase {
function MoreTesterTests($name) { $this->TestCase($name); }
function testRegexpPass() {
$this->assertRegexp('/fo+ba[^a-m]/', 'foobar');
}
function testRegexpFail() {
$this->assertRegexp('/fo+ba[^m-z]/', 'foobar');
}
function testRegexpFailWithMessage() {
$this->assertRegexp('/fo+ba[^m-z]/', 'foobar', "This is the message");
}
}
$suite->addTest(new TestSuite("MoreTesterTests"));
class ManyFailingTests extends TestCase {
function ManyFailingTests($name) { $this->TestCase($name); }
function testPass1() { $this->assertEquals(0, 0); }
function testPass2() { $this->assertEquals(0, 0); }
function testFail1() { $this->assertEquals(1, 0); }
function testFail2() { $this->assertEquals(1, 0); }
function testFail3() { $this->assertEquals(1, 0); }
function testFail4() { $this->assertEquals(1, 0); }
function testFail5() { $this->assertEquals(1, 0); }
function testFail6() { $this->assertEquals(1, 0); }
function testPass3() { $this->assertEquals(0, 0); }
function testFail7() { $this->assertEquals(1, 0); }
function testPass4() { $this->assertEquals(0, 0); }
function testFail8() { $this->assertEquals(1, 0); }
function testPass5() { $this->assertEquals(0, 0); }
function testPass6() { $this->assertEquals(0, 0); }
function testFail9() { $this->assertEquals(1, 0); }
function testPass7() { $this->assertEquals(0, 0); }
function testPass8() { $this->assertEquals(0, 0); }
}
$suite->addTest(new TestSuite("ManyFailingTests"));
class DummyClass1 {
var $fX;
}
class DummyClass2 {
var $fX;
var $fY;
function DummyClass2($x="", $y="") {
$this->fX = $x;
$this->fY = $y;
}
function equals($another) {
return $another->fX == $this->fX;
}
function toString() {
return sprintf("DummyClass2(%s, %s)", $this->fX, $this->fY);
}
}
class AssertEqualsTests extends TestCase {
function AssertEqualsTests($name) { $this->TestCase($name); }
function testDiffTypesFail() {
$this->assertEquals(0, "");
}
function testMultiLinePass() {
$str1 = "line1\nline2\nline3";
$str2 = "line1\nline2\nline3";
$this->assertEqualsMultilineStrings($str1, $str2);
}
function testMultiLineFail() {
$str1 = "line1\nline2\nline3";
$str2 = "line1\nline2 modified\nline3";
$this->assertEqualsMultilineStrings($str1, $str2);
}
function testMultiLineFail2() {
$str1 = "line1\nline2\nline3";
$str2 = "line1\nline2\nline3\nline4";
$this->assertEqualsMultilineStrings($str1, $str2);
}
}
$suite->addTest(new TestSuite("AssertEqualsTests"));
class AssertEqualsPhp3ErrorTests extends TestCase {
/* These tests create an ERROR in PHP3 and work as expected in PHP4. */
function AssertEqualsPhp3ErrorTests($name) { $this->TestCase($name); }
function testDiffClassFail() {
$this->assertEquals(new DummyClass1, new DummyClass2);
}
function testSameClassPass() {
$this->assertEquals(new DummyClass1, new DummyClass1);
}
function testSameClassFail() {
$dummy1 = new DummyClass1;
$dummy2 = new DummyClass1;
$dummy1->fX = 1;
$dummy2->fX = 2;
$this->assertEquals($dummy1, $dummy2);
}
function testSameClassEqualsFail() {
$dummy1 = new DummyClass2(3);
$dummy2 = new DummyClass2(4);
$this->assertEquals($dummy1, $dummy2);
}
function testSameClassEqualsPass() {
$dummy1 = new DummyClass2(5, 6);
$dummy2 = new DummyClass2(5, 7);
$this->assertEquals($dummy1, $dummy2);
}
}
$suite->addTest(new TestSuite("AssertEqualsPhp3ErrorTests"));
if (phpversion() >= '4') {
class AssertEqualsTests4 extends TestCase {
/* these tests only make sense starting with PHP4 */
function AssertEqualsTests($name) { $this->TestCase($name); }
function testNullFail() {
$this->assertEquals(0, NULL);
}
function testNullPass() {
$this->assertEquals(NULL, NULL);
}
function testArrayValuesPass1() {
$a1 = array('first' => 10, 'second' => 20);
$a2 = array('first' => 10, 'second' => 20);
$this->assertEquals($a1, $a2);
}
function testArrayValuesFail1() {
$a1 = array('first' => 10, 'second' => 20);
$a2 = array('first' => 10, 'second' => 22);
$this->assertEquals($a1, $a2);
}
}
$suite->addTest(new TestSuite("AssertEqualsTests4"));
class TestClassNameStartingWithTest extends TestCase {
function TestClassNameStartingWithTest($name) {
$this->TestCase($name);
}
function testWhateverPass() {
$this->assert(true);
}
}
$suite->addTest(new TestSuite("TestClassNameStartingWithTest"));
}
// $suite now consists of phpUnit self-test suite
?>