/src/PMango/lib/phpgacl/test_suite/phpunit/phpunit_test.php

https://github.com/marcotinacci/PMango-Diagrams · PHP · 268 lines · 221 code · 36 blank · 11 comment · 14 complexity · 9c4060c5c72b0e12b0c78838a412f010 MD5 · raw file

  1. <?php
  2. require "phpunit.php";
  3. class SelfTestResult extends TextTestResult {
  4. /* Specialize result class for use in self-tests, to handle
  5. special situation where many tests are expected to fail. */
  6. function SelfTestResult() {
  7. $this->TextTestResult();
  8. echo '<table class="details">';
  9. echo '<tr><th>Test name</th><th>Result</th><th>Meta-result</th></tr>';
  10. }
  11. function _startTest($test) {
  12. print('<tr><td>');
  13. if (phpversion() > '4') {
  14. printf("%s - %s ", get_class($test), $test->name());
  15. } else {
  16. printf("%s ", $test->name());
  17. }
  18. print('</td>');
  19. flush();
  20. }
  21. function _endTest($test) {
  22. /* Report both the test result and, for this special situation
  23. where some tests are expected to fail, a "meta" test result
  24. which indicates whether the test result matches the
  25. expected result. */
  26. $expect_failure = preg_match('/fail/i', $test->name());
  27. $test_passed = ($test->failed() == 0);
  28. if ($test->errored())
  29. $outcome = "<span class=\"Error\">ERROR</span>";
  30. else if ($test->failed())
  31. $outcome = "<span class=\"Failure\">FAIL</span>";
  32. else
  33. $outcome = "<span class=\"Pass\">OK</span>";
  34. if ($test->errored())
  35. $meta_outcome = '<span class="Unknown">unknown</span>';
  36. else
  37. $meta_outcome = ($expect_failure xor $test_passed)
  38. ? '<span class="Expected">as expected</span>'
  39. : '<span class="Unexpected">UNEXPECTED</span>';
  40. printf("<td>$outcome</td><td>$meta_outcome</td></tr>\n");
  41. flush();
  42. }
  43. }
  44. class TestFixture extends TestCase {
  45. function TestFixture($name) {
  46. $this->TestCase($name);
  47. }
  48. function setUp() {
  49. /* put any common setup here */
  50. $this->intVal = 1;
  51. $this->strVal = 'foo';
  52. }
  53. function testFail1() {
  54. $this->assert($this->intVal == 0, "1 == 0");
  55. }
  56. function testFail2() {
  57. $this->assert($this->strVal == 'bar');
  58. }
  59. function testPass1() {
  60. $this->assert($this->intVal == 1);
  61. }
  62. }
  63. $suite = new TestSuite;
  64. $suite->addTest(new TestFixture("testFail1"));
  65. $suite->addTest(new TestFixture("testFail2"));
  66. $suite->addTest(new TestFixture("testPass1"));
  67. //$suite->addTest(new TestFixture("testNotExistFail"));
  68. class Fixture2 extends TestCase {
  69. function Fixture2($name) {
  70. $this->TestCase($name);
  71. }
  72. function setUp() {
  73. $this->str1 = 'foo';
  74. $this->str2 = 'bar';
  75. }
  76. function runTest() {
  77. $this->testStrNotEqual();
  78. $this->testStrAppend();
  79. }
  80. function testStrNotEqual() {
  81. $this->assert($this->str1 == $this->str2, 'str equal');
  82. }
  83. function testStrAppend() {
  84. $this->assertEquals($this->str1 . 'bar', 'foobars', 'str append');
  85. }
  86. }
  87. $suite->addTest(new Fixture2("Fail3"));
  88. class TestPass2 extends TestFixture {
  89. function TestPass2($name) { $this->TestFixture($name); }
  90. function runTest() {
  91. $this->assertEquals($this->strVal . 'x', $this->strVal . 'x');
  92. $this->assertEquals($this->strVal . 'x', $this->strVal . 'y');
  93. $this->assertEquals(1, 0);
  94. $this->assertEquals(1, "1", 'equals int and str');
  95. }
  96. }
  97. $suite->addTest(new TestPass2("Fail4"));
  98. class MoreTesterTests extends TestCase {
  99. function MoreTesterTests($name) { $this->TestCase($name); }
  100. function testRegexpPass() {
  101. $this->assertRegexp('/fo+ba[^a-m]/', 'foobar');
  102. }
  103. function testRegexpFail() {
  104. $this->assertRegexp('/fo+ba[^m-z]/', 'foobar');
  105. }
  106. function testRegexpFailWithMessage() {
  107. $this->assertRegexp('/fo+ba[^m-z]/', 'foobar', "This is the message");
  108. }
  109. }
  110. $suite->addTest(new TestSuite("MoreTesterTests"));
  111. class ManyFailingTests extends TestCase {
  112. function ManyFailingTests($name) { $this->TestCase($name); }
  113. function testPass1() { $this->assertEquals(0, 0); }
  114. function testPass2() { $this->assertEquals(0, 0); }
  115. function testFail1() { $this->assertEquals(1, 0); }
  116. function testFail2() { $this->assertEquals(1, 0); }
  117. function testFail3() { $this->assertEquals(1, 0); }
  118. function testFail4() { $this->assertEquals(1, 0); }
  119. function testFail5() { $this->assertEquals(1, 0); }
  120. function testFail6() { $this->assertEquals(1, 0); }
  121. function testPass3() { $this->assertEquals(0, 0); }
  122. function testFail7() { $this->assertEquals(1, 0); }
  123. function testPass4() { $this->assertEquals(0, 0); }
  124. function testFail8() { $this->assertEquals(1, 0); }
  125. function testPass5() { $this->assertEquals(0, 0); }
  126. function testPass6() { $this->assertEquals(0, 0); }
  127. function testFail9() { $this->assertEquals(1, 0); }
  128. function testPass7() { $this->assertEquals(0, 0); }
  129. function testPass8() { $this->assertEquals(0, 0); }
  130. }
  131. $suite->addTest(new TestSuite("ManyFailingTests"));
  132. class DummyClass1 {
  133. var $fX;
  134. }
  135. class DummyClass2 {
  136. var $fX;
  137. var $fY;
  138. function DummyClass2($x="", $y="") {
  139. $this->fX = $x;
  140. $this->fY = $y;
  141. }
  142. function equals($another) {
  143. return $another->fX == $this->fX;
  144. }
  145. function toString() {
  146. return sprintf("DummyClass2(%s, %s)", $this->fX, $this->fY);
  147. }
  148. }
  149. class AssertEqualsTests extends TestCase {
  150. function AssertEqualsTests($name) { $this->TestCase($name); }
  151. function testDiffTypesFail() {
  152. $this->assertEquals(0, "");
  153. }
  154. function testMultiLinePass() {
  155. $str1 = "line1\nline2\nline3";
  156. $str2 = "line1\nline2\nline3";
  157. $this->assertEqualsMultilineStrings($str1, $str2);
  158. }
  159. function testMultiLineFail() {
  160. $str1 = "line1\nline2\nline3";
  161. $str2 = "line1\nline2 modified\nline3";
  162. $this->assertEqualsMultilineStrings($str1, $str2);
  163. }
  164. function testMultiLineFail2() {
  165. $str1 = "line1\nline2\nline3";
  166. $str2 = "line1\nline2\nline3\nline4";
  167. $this->assertEqualsMultilineStrings($str1, $str2);
  168. }
  169. }
  170. $suite->addTest(new TestSuite("AssertEqualsTests"));
  171. class AssertEqualsPhp3ErrorTests extends TestCase {
  172. /* These tests create an ERROR in PHP3 and work as expected in PHP4. */
  173. function AssertEqualsPhp3ErrorTests($name) { $this->TestCase($name); }
  174. function testDiffClassFail() {
  175. $this->assertEquals(new DummyClass1, new DummyClass2);
  176. }
  177. function testSameClassPass() {
  178. $this->assertEquals(new DummyClass1, new DummyClass1);
  179. }
  180. function testSameClassFail() {
  181. $dummy1 = new DummyClass1;
  182. $dummy2 = new DummyClass1;
  183. $dummy1->fX = 1;
  184. $dummy2->fX = 2;
  185. $this->assertEquals($dummy1, $dummy2);
  186. }
  187. function testSameClassEqualsFail() {
  188. $dummy1 = new DummyClass2(3);
  189. $dummy2 = new DummyClass2(4);
  190. $this->assertEquals($dummy1, $dummy2);
  191. }
  192. function testSameClassEqualsPass() {
  193. $dummy1 = new DummyClass2(5, 6);
  194. $dummy2 = new DummyClass2(5, 7);
  195. $this->assertEquals($dummy1, $dummy2);
  196. }
  197. }
  198. $suite->addTest(new TestSuite("AssertEqualsPhp3ErrorTests"));
  199. if (phpversion() >= '4') {
  200. class AssertEqualsTests4 extends TestCase {
  201. /* these tests only make sense starting with PHP4 */
  202. function AssertEqualsTests($name) { $this->TestCase($name); }
  203. function testNullFail() {
  204. $this->assertEquals(0, NULL);
  205. }
  206. function testNullPass() {
  207. $this->assertEquals(NULL, NULL);
  208. }
  209. function testArrayValuesPass1() {
  210. $a1 = array('first' => 10, 'second' => 20);
  211. $a2 = array('first' => 10, 'second' => 20);
  212. $this->assertEquals($a1, $a2);
  213. }
  214. function testArrayValuesFail1() {
  215. $a1 = array('first' => 10, 'second' => 20);
  216. $a2 = array('first' => 10, 'second' => 22);
  217. $this->assertEquals($a1, $a2);
  218. }
  219. }
  220. $suite->addTest(new TestSuite("AssertEqualsTests4"));
  221. class TestClassNameStartingWithTest extends TestCase {
  222. function TestClassNameStartingWithTest($name) {
  223. $this->TestCase($name);
  224. }
  225. function testWhateverPass() {
  226. $this->assert(true);
  227. }
  228. }
  229. $suite->addTest(new TestSuite("TestClassNameStartingWithTest"));
  230. }
  231. // $suite now consists of phpUnit self-test suite
  232. ?>