Merge branch 'testta_ly' into 'master'
Testta ly See merge request easycorp/zentaopms!5303
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
<?php
|
||||
$config->test = new stdclass;
|
||||
$config->test->dbPrefix = "db_";
|
||||
$config->test->rawDB = 'zentaounittest';
|
||||
$config->test->dbNum = 10;
|
||||
$config->test->dbPrefix = "db_"; //复制数据库的数据库前缀,便于区分复制数据库
|
||||
$config->test->rawDB = ''; //要复制的数据库,也就是ztest init初始化的数据库,例子:zentaounittest
|
||||
$config->test->dbNum = 10; //复制数据库的数量,可以根据自己的需要自行调节
|
||||
|
||||
$config->test->base = ''; //api测试基准路径,例子:http://liyang.oop.cc:8072/max/api.php/v1
|
||||
$config->test->account = ''; //api测试登录账户,例子:admin
|
||||
$config->test->password = ''; //api测试账户密码,例子:Ly123456
|
||||
|
||||
+14
-2
@@ -28,7 +28,12 @@ $frameworkRoot = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'fr
|
||||
function c($code)
|
||||
{
|
||||
global $_result;
|
||||
if($_result and isset($_result->status_code) and $_result->status_code == $code)
|
||||
if(empty($_result) or empty($_result->status_code)) return false;
|
||||
|
||||
$codeStr = (string)$code;
|
||||
$statusCodeStr = (string)($_result->status_code);
|
||||
|
||||
if($_result->status_code == $code or ($statusCodeStr[0] === '2' and $codeStr[0] === '2'))
|
||||
{
|
||||
$_result = $_result->body;
|
||||
return true;
|
||||
@@ -55,8 +60,15 @@ $config->zdPath = dirname(dirname(__FILE__)) . '/tools/zd';
|
||||
/* init testDB. */
|
||||
include $testPath . 'config/config.php';
|
||||
include $testPath. 'lib/db.class.php';
|
||||
$db = new db();
|
||||
include $testPath. 'lib/rest.php';
|
||||
$db = new db();
|
||||
$rest = new rest($config->test->base);
|
||||
|
||||
if(isset($config->test->account, $config->test->password))
|
||||
{
|
||||
$token = $rest->post('/tokens', array('account' => $config->test->account, 'password' => $config->test->password));
|
||||
$token = $token->body;
|
||||
}
|
||||
/**
|
||||
* Save variable to $_result.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
define('LIB_ROOT', dirname(dirname(__FILE__)) . '/lib/');
|
||||
|
||||
include LIB_ROOT . 'init.php';
|
||||
|
||||
$apiRoot = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'api/v1/entries';
|
||||
$apiFiles = scandir($apiRoot);
|
||||
$apiFiles = array_splice($apiFiles, 2);
|
||||
|
||||
/**
|
||||
* Get api List from the file and database.
|
||||
*
|
||||
* @param string $apiRoot
|
||||
* @param string $apiFiles
|
||||
* @param boole $info
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
function getApi($apiRoot, $apiFiles, $info = false)
|
||||
{
|
||||
global $config, $dao;
|
||||
$routes = $config->routes;
|
||||
|
||||
$apiMethods = array();
|
||||
foreach($apiFiles as $apiFile)
|
||||
{
|
||||
preg_match_all('/function\s(get|put|delete|post)\(/', file_get_contents($apiRoot . DS . $apiFile), $methods);
|
||||
if($methods)
|
||||
{
|
||||
$apiFileName = str_replace(strrchr($apiFile, "."),"",$apiFile);
|
||||
$apiMethods[$apiFileName] = $methods[1];
|
||||
}
|
||||
}
|
||||
|
||||
$noApiFile = '';
|
||||
$nodata = '';
|
||||
$api = array();
|
||||
|
||||
foreach($routes as $url => $apiName)
|
||||
{
|
||||
$apiName = strtolower($apiName);
|
||||
if(isset($apiMethods[$apiName]))
|
||||
{
|
||||
$api[$apiName]['url'] = $url;
|
||||
|
||||
foreach($apiMethods[$apiName] as $apiMethod)
|
||||
{
|
||||
$apiData = $dao->select('params, paramsExample')->from(TABLE_API)->where('path')->eq($url)->andwhere('method')->eq(strtoupper($apiMethod))->fetch();
|
||||
|
||||
if($apiData)
|
||||
{
|
||||
$api[$apiName]['method'][$apiMethod]['params'] = $apiData->params;
|
||||
$api[$apiName]['method'][$apiMethod]['example'] = $apiData->paramsExample;
|
||||
}
|
||||
else
|
||||
{
|
||||
$nodata .= $url . '的' . $apiMethod . '方法没有在数据库维护' . PHP_EOL;
|
||||
|
||||
$api[$apiName]['method'][$apiMethod]['params'] = '';
|
||||
$api[$apiName]['method'][$apiMethod]['example'] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$noApiFile .= "没有api文件的routes:" . $apiName . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
if($info === true) echo '没有数据库数据:' . PHP_EOL . $nodata . PHP_EOL . '没有API文件:' . PHP_EOL . $noApiFile . PHP_EOL;
|
||||
return $api;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of the parameter.
|
||||
*
|
||||
* @param string $values
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function setValue($values)
|
||||
{
|
||||
$result = "array(";
|
||||
foreach($values as $field => $value)
|
||||
{
|
||||
if(is_array($value))
|
||||
{
|
||||
$result .= setValue($value);
|
||||
}
|
||||
else
|
||||
{
|
||||
$result .= "'$field' => '$value', ";
|
||||
}
|
||||
}
|
||||
$result = trim($result, ", ") . ')';
|
||||
return $result;
|
||||
}
|
||||
|
||||
$apiList = getApi($apiRoot, $apiFiles);
|
||||
$caseRoot = dirname(__FILE__, 2) . '/checkapi';
|
||||
|
||||
/**
|
||||
* Batch create API cases.
|
||||
*
|
||||
* @param string $caseRoot
|
||||
* @param string $apiList
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function createCase($caseRoot, $apiList)
|
||||
{
|
||||
if(!is_dir($caseRoot)) mkdir($caseRoot, 0777, true);
|
||||
|
||||
foreach($apiList as $apiName => $api)
|
||||
{
|
||||
if(strpos($api['url'], ':'))
|
||||
{
|
||||
preg_match_all('/\/(:\w+)/', $api['url'], $matches);
|
||||
$apiUrl = str_replace($matches[1], '1', $api['url']);
|
||||
}
|
||||
else
|
||||
{
|
||||
$apiUrl = $api['url'];
|
||||
}
|
||||
|
||||
foreach($api['method'] as $method => $methodValue)
|
||||
{
|
||||
$apiMethodUrl = $apiUrl;
|
||||
$caseFile = $caseRoot . DS . $apiName . DS . $method;
|
||||
if($method === 'get')
|
||||
{
|
||||
if($methodValue['params'])
|
||||
{
|
||||
$queryParams = json_decode($methodValue['params'])->query;
|
||||
|
||||
$querys = '?';
|
||||
foreach($queryParams as $queryParam) $querys .= $queryParam->field . '=&';
|
||||
|
||||
$apiMethodUrl .= rtrim($querys, "&");
|
||||
}
|
||||
$testData = "array('token' => \$token->token)";
|
||||
}
|
||||
elseif($method === 'post' or $method === 'put')
|
||||
{
|
||||
if($methodValue['example'] !== '')
|
||||
{
|
||||
$testData = "array(";
|
||||
$examples = json_decode(str_replace('"', '"', $methodValue['example']), true);
|
||||
|
||||
foreach($examples as $key => $value)
|
||||
{
|
||||
if(is_array($value))
|
||||
{
|
||||
$paramValue = setValue($value);
|
||||
}
|
||||
else
|
||||
{
|
||||
$paramValue = "'$value'";
|
||||
}
|
||||
$testData .= "'" . $key . "' => " . $paramValue . ", ";
|
||||
}
|
||||
|
||||
if($apiMethodUrl == "/tokens")
|
||||
{
|
||||
$testData = rtrim($testData, ", ") . ")";
|
||||
}
|
||||
else
|
||||
{
|
||||
$testData = rtrim($testData, ", ") . "), array('token' => \$token->token)";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$testData = "array('token' => \$token->token)";
|
||||
}
|
||||
}
|
||||
$testData = "array('token' => \$token->token)";
|
||||
|
||||
$caseContent = <<<caseContent
|
||||
#!use/bin/env php
|
||||
<?php
|
||||
include dirname(dirname(dirname(__FILE__))) . '/lib/init.php';
|
||||
|
||||
/**
|
||||
|
||||
title:%s;
|
||||
cid=1
|
||||
pid=1
|
||||
|
||||
通用api检查 >> 0
|
||||
|
||||
*/
|
||||
\$%s = \$rest->%s('%s', %s);
|
||||
|
||||
r(\$%s) && c(200) && p() && e('0'); // 通用api检查
|
||||
caseContent;
|
||||
|
||||
$caseContent = sprintf($caseContent, $apiName . "测试" . $method, $apiName, $method, $apiMethodUrl, $testData, $apiName);
|
||||
|
||||
if(!is_dir(dirname($caseFile))) mkdir(dirname($caseFile), 0777, true);
|
||||
file_put_contents($caseFile, $caseContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
createCase($caseRoot, $apiList);
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
include dirname(__FILE__, 3) . DIRECTORY_SEPARATOR . 'lib/requests/requests.class.php';
|
||||
/**
|
||||
* The rest class.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user