', '<'); private $data_temp = array(); //临时存放预测数据 private $classname; //临时存放class private $testresult= array(); //预测的结果集 /** * 系统自动加载InitPHP类库 * @param string $class_name 类名称 * @param string $type 类所属类型 * @return object */ public function run($file = '') { $InitPHP_conf = InitPHP::getConfig(); if ($file == '') { $fileArr = $this->get_all_file(); } elseif (is_array($file)) { $fileArr = (count($file) > 0) ? $file : $this->get_all_file();; } else { $fileArr = array($file); } foreach ($fileArr as $v) { $this->classname = $v; $test_file_name = $this->classname . $InitPHP_conf['unittesting']['test_postfix']; $test_file_path = $InitPHP_conf['unittesting']['path'] . $test_file_name . '.php'; if (InitPHP::import($test_file_path)) { $obj = InitPHP::loadclass($test_file_name); $obj->run($this); } } $this->print_result(); } /** * 添加预测数据 * @param array $function_param 方法传递的参数 * @param string $forecast_result 预测结果的值 * @param string $type 预测结果的类型 * @return object */ public function add_data($function_param, $forecast_result, $type = '=') { if (!in_array($type, $this->test_type)) $type = '='; return $this->data_temp[] = array($function_param, $forecast_result, $type); } /** * 预测数据 * @param array $function 方法名称 * @return object */ public function test($function) { $InitPHP_conf = InitPHP::getConfig(); $obj = $this->get_service_obj(); if ($obj) { if ($this->data_temp) { foreach ($this->data_temp as $v) { $result =call_user_func_array(array($obj, $function), $v[0]); if ($v[2] == 'type') { $result_test = (strtolower(gettype($result)) == strtolower($v[1])) ? true : false; } elseif ($v[2] == '=') { $result_test = ($result == $v[1]) ? true : false; } elseif ($v[2] == '!=') { $result_test = ((int)$result != (int)$v[1]) ? true : false; } elseif ($v[2] == '!==') { $result_test = ($result !== $v[1]) ? true : false; } elseif ($v[2] == '>') { $result_test = ((int)$result > (int)$v[1]) ? true : false; } elseif ($v[2] == '<') { $result_test = ((int)$result < (int)$v[1]) ? true : false; } $this->testresult[] = array( 'result' => $result_test, 'class' => $this->classname . $InitPHP_conf['service']['service_postfix'], 'func' => $function, 'data' => $v[0] ); } $this->data_temp = array(); } } } /** * 打印出结果数据 * @return object */ private function print_result() { $tempError = array(); $tempErrorCount = $tempYesCount = $tempAllCount = 0; foreach ($this->testresult as $v) { if (!$v['result']) { $tempError[] = $v; $tempErrorCount++; } else { $tempYesCount++; } $tempAllCount++; } echo '
---------------------------------------------------InitPHP单元测试------------------------------------------------------------
Query : '.$tempAllCount.' 条数据,OK : '.$tempYesCount.' 条数据,Error : '.$tempErrorCount.' 条数据
'; if ($tempErrorCount > 0) { echo '----------------------------------------------------------Error列表----------------------------------------------------------
'; foreach ($tempError as $v) { echo '类名:'.$v['class'].' 方法名:'.$v['func'].' 参数:'; print_r($v['data']); echo '
'; } } echo '