* Refactor create case.

This commit is contained in:
tianshujie
2023-09-22 13:59:02 +08:00
parent e0734ed380
commit cd21520994
3 changed files with 44 additions and 56 deletions
+1 -1
View File
@@ -495,7 +495,7 @@ class productplanModel extends model
$this->dao->insert(TABLE_PRODUCTPLAN)->data($plan)
->autoCheck()
->batchCheck($this->config->productplan->create->requiredFields, 'notempty')
->checkIF(!$isFuture, 'end', 'ge', $plan->begin)
->checkIF(!$isFuture && $plan->begin != $this->config->productplan->future, 'end', 'ge', $plan->begin)
->checkFlow()
->exec();
if(dao::isError()) return false;
+33 -41
View File
@@ -3,59 +3,51 @@
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/productplan.class.php';
zdTable('productplan')->gen(0);
/**
title=productpanModel->create();
timeout=0
cid=1
pid=1
测试正常创建 >> 110
测试不传入名称的情况 >> 『名称』不能为空。
测试不传开始时间的情况 >> 111
测试不传结束时间的情况 >> 112
测试不传开始时间及结束时间的情况 >> 113
测试不传UID的情况 >> 114
测试不传关联产品的情况 >> 『产品』应当是数字。
测试不传父级计划的情况 >> 『父计划』应当是数字。
*/
$plan = new productPlan('admin');
$posts = array();
$posts['title'] = '测试创建1';
$posts['begin'] = '2021-10-25';
$posts['end'] = '2021-10-29';
$posts['uid'] = '623927843dd9b';
$posts['product'] = '2';
$posts['parent'] = '0';
$postData = new stdclass();
$postData->title = '测试创建1';
$postData->begin = '2021-10-25';
$postData->end = '2021-10-29';
$postData->uid = '623927843dd9b';
$postData->product = '2';
$postData->parent = '0';
$noTitle = $posts;
$noTitle['title'] = '';
$noTitle = clone $postData;
$noTitle->title = '';
$noBegin = $posts;
$noBegin['begin'] = '';
$noBegin = clone $postData;
$noBegin->begin = '';
$noEnd = $posts;
$noEnd['end'] = '';
$noEnd = clone$postData;
$noEnd->end = '';
$noBeginEnd = $noBegin;
$noBeginEnd['end'] = '';
$noBeginEnd = clone $noBegin;
$noBeginEnd->end = '';
$noUid = $posts;
$noUid['uid'] = '';
$noUid = clone $postData;
$noUid->uid = '';
$noProduct = $posts;
$noProduct['product'] = '';
$parent = clone $postData;
$parent->parent = 1;
$noParent = $posts;
$noParent['parent'] = '';
$isFutureList = array(false, true);
r($plan->create($posts)) && p() && e('110'); //测试正常创建
r($plan->create($noTitle)) && p('title:0') && e('『名称』不能为空。'); //测试不传入名称的情况
r($plan->create($noBegin)) && p() && e('111'); //测试不传开始时间的情况
r($plan->create($noEnd)) && p() && e('112'); //测试不传结束时间的情况
r($plan->create($noBeginEnd)) && p() && e('113'); //测试不传开始时间及结束时间的情况
r($plan->create($noUid)) && p() && e('114'); //测试不传UID的情况
r($plan->create($noProduct)) && p('product:0') && e('『产品』应当是数字。'); //测试不传关联产品的情况
r($plan->create($noParent)) && p('parent:0') && e('『父计划』应当是数字。'); //测试不传父级计划的情况
?>
$planTester = new productPlan('admin');
r($planTester->createTest($postData, $isFutureList[0])) && p('title') && e('测试创建1'); // 测试正常创建
r($planTester->createTest($postData, $isFutureList[1])) && p('title') && e('测试创建1'); // 测试正常创建
r($planTester->createTest($noTitle, $isFutureList[0])) && p('title:0') && e('『名称』不能为空。'); // 测试不填名称创建失败
r($planTester->createTest($noBegin, $isFutureList[0])) && p('begin') && e('『开始日期』不能为空。'); // 测试不填开始时间创建失败
r($planTester->createTest($noEnd, $isFutureList[0])) && p('end') && e('『结束日期』不能为空。'); // 测试不填结束日期创建失败
r($planTester->createTest($noBeginEnd, $isFutureList[0])) && p('begin') && e('『开始日期』不能为空。'); // 测试不填开始日期和结束日期创建失败
r($planTester->createTest($noUid, $isFutureList[0])) && p('title') && e('测试创建1'); // 测试没有uid
r($planTester->createTest($parent, $isFutureList[0])) && p('parent') && e('1'); // 测试创建子计划
+10 -14
View File
@@ -186,25 +186,21 @@ class productPlan
}
/**
* Create
* 创建一个计划。
* Create a productplan.
*
* @param array $param
* @param object $param
* @param bool $isFuture
* @access public
* @return array
* @return object|array
*/
public function create($param)
public function createTest(object $postData, bool $isFuture = false): object|array
{
//初始化传的参数,这里可以设置默认值
$createPlan = array('title' => '', 'begin' => '', 'end' => '', 'delta' => '', 'desc' => '', 'uid' => '', 'product' => '', 'parent' => '');
//设置foreach循环,将初始化参数传给全局变量$_POST中,通过此方式将传的参数提供给源码create方法
foreach($createPlan as $field => $defaultvalue) $_POST[$field] = $defaultvalue;
//二次遍历数组将此方法的形参传入$_POST中,这行可以替换掉初始化参数,做到以传入参数为最终结果的目的。同时不传参数的话取默认值
foreach($param as $key => $value) $_POST[$key] = $value;
//这行代码的作用是调用源码中的方法(参数通过$_POST直接取,具体原理可以不知道)
$productPlans = $this->productplan->create();
//将方法返回内容返回
$this->productplan->config->productplan->create->requiredFields = 'title,begin,end';
$planID = $this->productplan->create($postData, $isFuture);
if(dao::isError()) return dao::getError();
return $productPlans;
return $this->productplan->getByID($planID);
}
/**