Support for larger size codes (such as SNOMED US Extension codes)
[openemr.git] / gacl / test_suite / phpunit / phpunit_test.php
blob01c6dd96de5e82618865874429aa2040f24bdec0
1 <?php
2 require "phpunit.php";
4 class SelfTestResult extends TextTestResult {
5 /* Specialize result class for use in self-tests, to handle
6 special situation where many tests are expected to fail. */
7 function SelfTestResult() {
8 $this->TextTestResult();
9 echo '<table class="details">';
10 echo '<tr><th>Test name</th><th>Result</th><th>Meta-result</th></tr>';
13 function _startTest($test) {
14 print('<tr><td>');
15 if (phpversion() > '4') {
16 printf("%s - %s ", get_class($test), $test->name());
17 } else {
18 printf("%s ", $test->name());
20 print('</td>');
21 flush();
24 function _endTest($test) {
25 /* Report both the test result and, for this special situation
26 where some tests are expected to fail, a "meta" test result
27 which indicates whether the test result matches the
28 expected result. */
29 $expect_failure = preg_match('/fail/i', $test->name());
30 $test_passed = ($test->failed() == 0);
32 if ($test->errored())
33 $outcome = "<span class=\"Error\">ERROR</span>";
34 else if ($test->failed())
35 $outcome = "<span class=\"Failure\">FAIL</span>";
36 else
37 $outcome = "<span class=\"Pass\">OK</span>";
39 if ($test->errored())
40 $meta_outcome = '<span class="Unknown">unknown</span>';
41 else
42 $meta_outcome = ($expect_failure xor $test_passed)
43 ? '<span class="Expected">as expected</span>'
44 : '<span class="Unexpected">UNEXPECTED</span>';
46 printf("<td>$outcome</td><td>$meta_outcome</td></tr>\n");
47 flush();
52 class TestFixture extends TestCase {
53 function TestFixture($name) {
54 $this->TestCase($name);
57 function setUp() {
58 /* put any common setup here */
59 $this->intVal = 1;
60 $this->strVal = 'foo';
63 function testFail1() {
64 $this->assert($this->intVal == 0, "1 == 0");
67 function testFail2() {
68 $this->assert($this->strVal == 'bar');
71 function testPass1() {
72 $this->assert($this->intVal == 1);
76 $suite = new TestSuite;
77 $suite->addTest(new TestFixture("testFail1"));
78 $suite->addTest(new TestFixture("testFail2"));
79 $suite->addTest(new TestFixture("testPass1"));
80 //$suite->addTest(new TestFixture("testNotExistFail"));
83 class Fixture2 extends TestCase {
84 function Fixture2($name) {
85 $this->TestCase($name);
87 function setUp() {
88 $this->str1 = 'foo';
89 $this->str2 = 'bar';
91 function runTest() {
92 $this->testStrNotEqual();
93 $this->testStrAppend();
95 function testStrNotEqual() {
96 $this->assert($this->str1 == $this->str2, 'str equal');
98 function testStrAppend() {
99 $this->assertEquals($this->str1 . 'bar', 'foobars', 'str append');
103 $suite->addTest(new Fixture2("Fail3"));
106 class TestPass2 extends TestFixture {
107 function TestPass2($name) { $this->TestFixture($name); }
108 function runTest() {
109 $this->assertEquals($this->strVal . 'x', $this->strVal . 'x');
110 $this->assertEquals($this->strVal . 'x', $this->strVal . 'y');
111 $this->assertEquals(1, 0);
112 $this->assertEquals(1, "1", 'equals int and str');
115 $suite->addTest(new TestPass2("Fail4"));
118 class MoreTesterTests extends TestCase {
119 function MoreTesterTests($name) { $this->TestCase($name); }
121 function testRegexpPass() {
122 $this->assertRegexp('/fo+ba[^a-m]/', 'foobar');
125 function testRegexpFail() {
126 $this->assertRegexp('/fo+ba[^m-z]/', 'foobar');
129 function testRegexpFailWithMessage() {
130 $this->assertRegexp('/fo+ba[^m-z]/', 'foobar', "This is the message");
133 $suite->addTest(new TestSuite("MoreTesterTests"));
135 class ManyFailingTests extends TestCase {
136 function ManyFailingTests($name) { $this->TestCase($name); }
138 function testPass1() { $this->assertEquals(0, 0); }
139 function testPass2() { $this->assertEquals(0, 0); }
140 function testFail1() { $this->assertEquals(1, 0); }
141 function testFail2() { $this->assertEquals(1, 0); }
142 function testFail3() { $this->assertEquals(1, 0); }
143 function testFail4() { $this->assertEquals(1, 0); }
144 function testFail5() { $this->assertEquals(1, 0); }
145 function testFail6() { $this->assertEquals(1, 0); }
146 function testPass3() { $this->assertEquals(0, 0); }
147 function testFail7() { $this->assertEquals(1, 0); }
148 function testPass4() { $this->assertEquals(0, 0); }
149 function testFail8() { $this->assertEquals(1, 0); }
150 function testPass5() { $this->assertEquals(0, 0); }
151 function testPass6() { $this->assertEquals(0, 0); }
152 function testFail9() { $this->assertEquals(1, 0); }
153 function testPass7() { $this->assertEquals(0, 0); }
154 function testPass8() { $this->assertEquals(0, 0); }
157 $suite->addTest(new TestSuite("ManyFailingTests"));
159 class DummyClass1 {
160 var $fX;
163 class DummyClass2 {
164 var $fX;
165 var $fY;
166 function DummyClass2($x="", $y="") {
167 $this->fX = $x;
168 $this->fY = $y;
170 function equals($another) {
171 return $another->fX == $this->fX;
173 function toString() {
174 return sprintf("DummyClass2(%s, %s)", $this->fX, $this->fY);
178 class AssertEqualsTests extends TestCase {
179 function AssertEqualsTests($name) { $this->TestCase($name); }
181 function testDiffTypesFail() {
182 $this->assertEquals(0, "");
184 function testMultiLinePass() {
185 $str1 = "line1\nline2\nline3";
186 $str2 = "line1\nline2\nline3";
187 $this->assertEqualsMultilineStrings($str1, $str2);
189 function testMultiLineFail() {
190 $str1 = "line1\nline2\nline3";
191 $str2 = "line1\nline2 modified\nline3";
192 $this->assertEqualsMultilineStrings($str1, $str2);
194 function testMultiLineFail2() {
195 $str1 = "line1\nline2\nline3";
196 $str2 = "line1\nline2\nline3\nline4";
197 $this->assertEqualsMultilineStrings($str1, $str2);
200 $suite->addTest(new TestSuite("AssertEqualsTests"));
202 class AssertEqualsPhp3ErrorTests extends TestCase {
203 /* These tests create an ERROR in PHP3 and work as expected in PHP4. */
204 function AssertEqualsPhp3ErrorTests($name) { $this->TestCase($name); }
205 function testDiffClassFail() {
206 $this->assertEquals(new DummyClass1, new DummyClass2);
208 function testSameClassPass() {
209 $this->assertEquals(new DummyClass1, new DummyClass1);
211 function testSameClassFail() {
212 $dummy1 = new DummyClass1;
213 $dummy2 = new DummyClass1;
214 $dummy1->fX = 1;
215 $dummy2->fX = 2;
216 $this->assertEquals($dummy1, $dummy2);
218 function testSameClassEqualsFail() {
219 $dummy1 = new DummyClass2(3);
220 $dummy2 = new DummyClass2(4);
221 $this->assertEquals($dummy1, $dummy2);
223 function testSameClassEqualsPass() {
224 $dummy1 = new DummyClass2(5, 6);
225 $dummy2 = new DummyClass2(5, 7);
226 $this->assertEquals($dummy1, $dummy2);
229 $suite->addTest(new TestSuite("AssertEqualsPhp3ErrorTests"));
231 if (phpversion() >= '4') {
232 class AssertEqualsTests4 extends TestCase {
233 /* these tests only make sense starting with PHP4 */
234 function AssertEqualsTests($name) { $this->TestCase($name); }
236 function testNullFail() {
237 $this->assertEquals(0, NULL);
239 function testNullPass() {
240 $this->assertEquals(NULL, NULL);
242 function testArrayValuesPass1() {
243 $a1 = array('first' => 10, 'second' => 20);
244 $a2 = array('first' => 10, 'second' => 20);
245 $this->assertEquals($a1, $a2);
247 function testArrayValuesFail1() {
248 $a1 = array('first' => 10, 'second' => 20);
249 $a2 = array('first' => 10, 'second' => 22);
250 $this->assertEquals($a1, $a2);
253 $suite->addTest(new TestSuite("AssertEqualsTests4"));
255 class TestClassNameStartingWithTest extends TestCase {
256 function TestClassNameStartingWithTest($name) {
257 $this->TestCase($name);
259 function testWhateverPass() {
260 $this->assert(true);
263 $suite->addTest(new TestSuite("TestClassNameStartingWithTest"));
267 // $suite now consists of phpUnit self-test suite