unittesting.init.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 - 工具库-单元测试
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class unittestingInit {
  12. //预测值和返回结果的比较 类型比较|值=号比较|不等于比较|大于比较|小于比较
  13. private $test_type = array('type', '=', '!=', '!==','>', '<');
  14. private $data_temp = array(); //临时存放预测数据
  15. private $classname; //临时存放class
  16. private $testresult= array(); //预测的结果集
  17. /**
  18. * 系统自动加载InitPHP类库
  19. * @param string $class_name 类名称
  20. * @param string $type 类所属类型
  21. * @return object
  22. */
  23. public function run($file = '') {
  24. $InitPHP_conf = InitPHP::getConfig();
  25. if ($file == '') {
  26. $fileArr = $this->get_all_file();
  27. } elseif (is_array($file)) {
  28. $fileArr = (count($file) > 0) ? $file : $this->get_all_file();;
  29. } else {
  30. $fileArr = array($file);
  31. }
  32. foreach ($fileArr as $v) {
  33. $this->classname = $v;
  34. $test_file_name = $this->classname . $InitPHP_conf['unittesting']['test_postfix'];
  35. $test_file_path = $InitPHP_conf['unittesting']['path'] . $test_file_name . '.php';
  36. if (InitPHP::import($test_file_path)) {
  37. $obj = InitPHP::loadclass($test_file_name);
  38. $obj->run($this);
  39. }
  40. }
  41. $this->print_result();
  42. }
  43. /**
  44. * 添加预测数据
  45. * @param array $function_param 方法传递的参数
  46. * @param string $forecast_result 预测结果的值
  47. * @param string $type 预测结果的类型
  48. * @return object
  49. */
  50. public function add_data($function_param, $forecast_result, $type = '=') {
  51. if (!in_array($type, $this->test_type)) $type = '=';
  52. return $this->data_temp[] = array($function_param, $forecast_result, $type);
  53. }
  54. /**
  55. * 预测数据
  56. * @param array $function 方法名称
  57. * @return object
  58. */
  59. public function test($function) {
  60. $InitPHP_conf = InitPHP::getConfig();
  61. $obj = $this->get_service_obj();
  62. if ($obj) {
  63. if ($this->data_temp) {
  64. foreach ($this->data_temp as $v) {
  65. $result =call_user_func_array(array($obj, $function), $v[0]);
  66. if ($v[2] == 'type') {
  67. $result_test = (strtolower(gettype($result)) == strtolower($v[1])) ? true : false;
  68. } elseif ($v[2] == '=') {
  69. $result_test = ($result == $v[1]) ? true : false;
  70. } elseif ($v[2] == '!=') {
  71. $result_test = ((int)$result != (int)$v[1]) ? true : false;
  72. } elseif ($v[2] == '!==') {
  73. $result_test = ($result !== $v[1]) ? true : false;
  74. } elseif ($v[2] == '>') {
  75. $result_test = ((int)$result > (int)$v[1]) ? true : false;
  76. } elseif ($v[2] == '<') {
  77. $result_test = ((int)$result < (int)$v[1]) ? true : false;
  78. }
  79. $this->testresult[] = array(
  80. 'result' => $result_test,
  81. 'class' => $this->classname . $InitPHP_conf['service']['service_postfix'],
  82. 'func' => $function,
  83. 'data' => $v[0]
  84. );
  85. }
  86. $this->data_temp = array();
  87. }
  88. }
  89. }
  90. /**
  91. * 打印出结果数据
  92. * @return object
  93. */
  94. private function print_result() {
  95. $tempError = array();
  96. $tempErrorCount = $tempYesCount = $tempAllCount = 0;
  97. foreach ($this->testresult as $v) {
  98. if (!$v['result']) {
  99. $tempError[] = $v;
  100. $tempErrorCount++;
  101. } else {
  102. $tempYesCount++;
  103. }
  104. $tempAllCount++;
  105. }
  106. echo '<div style="font-size:12px; margin:0px; background-color:#000000; color:#FFFFFF; border:solid 1px #FF9900; width:600px; padding:10px;">
  107. <p>---------------------------------------------------InitPHP单元测试------------------------------------------------------------</p>
  108. <p>Query : '.$tempAllCount.' 条数据,OK : '.$tempYesCount.' 条数据,Error : '.$tempErrorCount.' 条数据</p>';
  109. if ($tempErrorCount > 0) {
  110. echo '<p>----------------------------------------------------------Error列表----------------------------------------------------------</p>';
  111. foreach ($tempError as $v) {
  112. echo '<p>类名:'.$v['class'].'&nbsp;&nbsp;&nbsp;&nbsp;方法名:'.$v['func'].'&nbsp;&nbsp;&nbsp;&nbsp;参数:';
  113. print_r($v['data']);
  114. echo '</p>';
  115. }
  116. }
  117. echo '</div>';
  118. }
  119. /**
  120. * 获取运行的service
  121. * @return object
  122. */
  123. private function get_service_obj() {
  124. $InitPHP_conf = InitPHP::getConfig();
  125. $test_file_name = $this->classname . $InitPHP_conf['service']['service_postfix'];
  126. if (InitPHP::import("$test_file_name" . ".php", array($InitPHP_conf['service']['path']))) {
  127. return InitPHP::loadclass($test_file_name);
  128. }
  129. return false;
  130. }
  131. /**
  132. * 获取文件目录下所有文件
  133. * @return object
  134. */
  135. private function get_all_file() {
  136. $InitPHP_conf = InitPHP::getConfig();
  137. $temp = array();
  138. $path = InitPHP::getAppPath($InitPHP_conf['unittesting']['path']);
  139. if (is_dir($path)) {
  140. if ($dh = opendir($path)){
  141. while (($file = readdir($dh)) !== false){
  142. if ($file == '.' || $file == '..') continue;
  143. $temp[] = str_replace($InitPHP_conf['unittesting']['test_postfix'] . '.php', '', $file);
  144. }
  145. closedir($dh);
  146. }
  147. }
  148. return $temp;
  149. }
  150. }
  151. ?>