/tests/DoctrineTest/UnitTestCase.php

https://github.com/xdade/doctrine1 · PHP · 252 lines · 209 code · 41 blank · 2 comment · 31 complexity · 1504930aa58d8ac06e7e55e32a98306a MD5 · raw file

  1. <?php
  2. class UnitTestCase
  3. {
  4. protected $_passed = 0;
  5. protected $_failed = 0;
  6. protected $_messages = array();
  7. protected static $_passesAndFails = array('passes' => array(), 'fails' => array());
  8. protected static $_lastRunsPassesAndFails = array('passes' => array(), 'fails' => array());
  9. public function init()
  10. {
  11. $tmpFileName = $this->getPassesAndFailsCachePath();
  12. if (file_exists($tmpFileName)) {
  13. $array = unserialize(file_get_contents($tmpFileName));
  14. } else {
  15. $array = array();
  16. }
  17. if ($array) {
  18. self::$_lastRunsPassesAndFails = $array;
  19. }
  20. }
  21. public function addMessage($msg)
  22. {
  23. $this->_messages[] = $msg;
  24. }
  25. public function assertEqual($value, $value2)
  26. {
  27. if ($value == $value2) {
  28. $this->pass();
  29. } else {
  30. $seperator = "<br>";
  31. if (PHP_SAPI === "cli") {
  32. $seperator = "\n";
  33. }
  34. if (is_array($value)) {
  35. $value = var_export($value, true);
  36. }
  37. if (is_array($value2)) {
  38. $value2 = var_export($value2, true);
  39. }
  40. $message = "$seperator Value1: $value $seperator != $seperator Value2: $value2 $seperator";
  41. $this->_fail($message);
  42. }
  43. }
  44. public function assertIdentical($value, $value2)
  45. {
  46. if ($value === $value2) {
  47. $this->pass();
  48. } else {
  49. $this->_fail();
  50. }
  51. }
  52. public function assertNotEqual($value, $value2)
  53. {
  54. if ($value != $value2) {
  55. $this->pass();
  56. } else {
  57. $this->_fail();
  58. }
  59. }
  60. public function assertTrue($expr)
  61. {
  62. if ($expr) {
  63. $this->pass();
  64. } else {
  65. $this->_fail();
  66. }
  67. }
  68. public function assertFalse($expr)
  69. {
  70. if ( ! $expr) {
  71. $this->pass();
  72. } else {
  73. $this->_fail();
  74. }
  75. }
  76. public function assertNull($expr)
  77. {
  78. if (is_null($expr)) {
  79. $this->pass();
  80. } else {
  81. $this->fail();
  82. }
  83. }
  84. public function assertNotNull($expr)
  85. {
  86. if (is_null($expr)) {
  87. $this->fail();
  88. } else {
  89. $this->pass();
  90. }
  91. }
  92. public function pass()
  93. {
  94. $class = get_class($this);
  95. if ( ! isset(self::$_passesAndFails['fails'][$class])) {
  96. self::$_passesAndFails['passes'][$class] = $class;
  97. }
  98. $this->_passed++;
  99. }
  100. public function fail($message = "")
  101. {
  102. $this->_fail($message);
  103. }
  104. public function _fail($message = "")
  105. {
  106. $trace = debug_backtrace();
  107. array_shift($trace);
  108. foreach ($trace as $stack) {
  109. if (substr($stack['function'], 0, 4) === 'test') {
  110. $class = new ReflectionClass($stack['class']);
  111. if ( ! isset($line)) {
  112. $line = $stack['line'];
  113. }
  114. $errorMessage = $class->getName() . ' : method ' . $stack['function'] . ' failed on line ' . $line;
  115. $this->_messages[] = $errorMessage . " " . $message;
  116. break;
  117. }
  118. $line = $stack['line'];
  119. }
  120. $this->_failed++;
  121. $class = get_class($this);
  122. if (isset(self::$_passesAndFails['passes'][$class])) {
  123. unset(self::$_passesAndFails['passes'][$class]);
  124. }
  125. self::$_passesAndFails['fails'][$class] = $class;
  126. }
  127. public function run(DoctrineTest_Reporter $reporter = null, $filter = null)
  128. {
  129. foreach (get_class_methods($this) as $method) {
  130. if (substr($method, 0, 4) === 'test') {
  131. $this->setUp();
  132. $this->$method();
  133. $this->tearDown();
  134. }
  135. }
  136. }
  137. public function getMessages()
  138. {
  139. return $this->_messages;
  140. }
  141. public function getFailCount()
  142. {
  143. return $this->_failed;
  144. }
  145. public function getPassCount()
  146. {
  147. return $this->_passed;
  148. }
  149. public function getPassesAndFailsCachePath()
  150. {
  151. $dir = dirname(__FILE__) . '/doctrine_tests';
  152. if ( ! is_dir($dir)) {
  153. mkdir($dir, 0777, true);
  154. }
  155. $path = $dir . '/' . md5(serialize(array_keys($this->_testCases)));
  156. return $path;
  157. }
  158. public function cachePassesAndFails()
  159. {
  160. $tmpFileName = $this->getPassesAndFailsCachePath();
  161. file_put_contents($tmpFileName, serialize(self::$_passesAndFails));
  162. }
  163. public function getPassesAndFails()
  164. {
  165. return self::$_passesAndFails;
  166. }
  167. public function getLastRunsPassesAndFails()
  168. {
  169. return self::$_lastRunsPassesAndFails;
  170. }
  171. public function getLastRunsFails()
  172. {
  173. return isset(self::$_lastRunsPassesAndFails['fails']) ? self::$_lastRunsPassesAndFails['fails'] : array();
  174. }
  175. public function getLastRunsPass()
  176. {
  177. return isset(self::$_lastRunsPassesAndFails['passes']) ? self::$_lastRunsPassesAndFails['passes'] : array();
  178. }
  179. public function getNewFails()
  180. {
  181. $newFails = array();
  182. $fails = self::$_passesAndFails['fails'];
  183. foreach ($fails as $fail) {
  184. // If it passed before then it is a new fail
  185. if (isset(self::$_lastRunsPassesAndFails['passes'][$fail])) {
  186. $newFails[$fail] = $fail;
  187. }
  188. }
  189. return $newFails;;
  190. }
  191. public function getFixedFails()
  192. {
  193. $fixed = array();
  194. $fails = self::$_lastRunsPassesAndFails['fails'];
  195. foreach ($fails as $fail) {
  196. // If the fail passes this time then it is fixed
  197. if (isset(self::$_passesAndFails['passes'][$fail])) {
  198. $fixed[$fail] = $fail;
  199. }
  200. }
  201. return $fixed;;
  202. }
  203. public function getNumNewFails()
  204. {
  205. return count($this->getNewFails());
  206. }
  207. public function getNumFixedFails()
  208. {
  209. return count($this->getFixedFails());
  210. }
  211. }