* Refactor testcaseModel::createStepsFromBug, and add its unit test.
This commit is contained in:
@@ -1176,14 +1176,17 @@ class testcaseModel extends model
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 bug 的步骤创建用例步骤。
|
||||
* Create case steps from a bug's step.
|
||||
*
|
||||
* @param string $steps
|
||||
* @param string $steps
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
function createStepsFromBug($steps)
|
||||
public function createStepsFromBug(string $steps): array
|
||||
{
|
||||
/* 初始化步骤相关变量,获取步骤的描述、结果和期望,以及他们在字符串中的位置。 */
|
||||
/* Initializes the step variable, and get the desc, result and expect of the step and its position. */
|
||||
$steps = strip_tags($steps);
|
||||
$caseSteps = array((object)array('desc' => $steps, 'expect' => '')); // the default steps before parse.
|
||||
$lblStep = strip_tags($this->lang->bug->tplStep);
|
||||
@@ -1193,13 +1196,19 @@ class testcaseModel extends model
|
||||
$lblResultPos = strpos($steps, $lblResult);
|
||||
$lblExpectPos = strpos($steps, $lblExpect);
|
||||
|
||||
if($lblStepPos === false or $lblResultPos === false or $lblExpectPos === false) return $caseSteps;
|
||||
/* 如果 bug 的步骤没有描述、结果或者期望,返回默认步骤。 */
|
||||
/* If steps don't have desc, result or expect, return default steps. */
|
||||
if($lblStepPos === false || $lblResultPos === false || $lblExpectPos === false) return $caseSteps;
|
||||
|
||||
/* 计算描述和期望。 */
|
||||
/* Process desc and expect. */
|
||||
$caseSteps = substr($steps, $lblStepPos + strlen($lblStep), $lblResultPos - strlen($lblStep) - $lblStepPos);
|
||||
$caseExpect = substr($steps, $lblExpectPos + strlen($lblExpect));
|
||||
$caseSteps = trim($caseSteps);
|
||||
$caseExpect = trim($caseExpect);
|
||||
|
||||
/* 计算步骤。 */
|
||||
/* Process steps. */
|
||||
$caseSteps = explode("\n", trim($caseSteps));
|
||||
$stepCount = count($caseSteps);
|
||||
foreach($caseSteps as $key => $caseStep)
|
||||
@@ -1207,6 +1216,7 @@ class testcaseModel extends model
|
||||
$expect = $key + 1 == $stepCount ? $caseExpect : '';
|
||||
$caseSteps[$key] = (object)array('desc' => trim($caseStep), 'expect' => $expect, 'type' => 'item');
|
||||
}
|
||||
|
||||
return $caseSteps;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
include dirname(__FILE__, 5) . '/test/lib/init.php';
|
||||
include dirname(__FILE__, 2) . '/testcase.class.php';
|
||||
|
||||
/**
|
||||
|
||||
title=测试 testcaseModel->createStepsFromBug();
|
||||
cid=1
|
||||
pid=1
|
||||
|
||||
|
||||
*/
|
||||
|
||||
$testcase = new testcaseTest();
|
||||
|
||||
$steps1 = "<p>[步骤]</p><br/>一个步骤\n<p>[结果]</p><br/>一个结果\n<br/><p>[期望]</p><br/>一个期望\n<br/>";
|
||||
$steps2 = "<p>[步骤]</p><br/>1. 步骤1\n2. 步骤2<br/><p>[结果]</p><br/>1. 结果1\n2. 结果2\n<br/><p>[期望]</p><br/>期望\n<br/>";
|
||||
$noStep = "<p>[结果]</p><br/>一个结果\n<p>[期望]</p><br/>一个期望\n";
|
||||
$noResult = "<p>[步骤]</p><br/>一个步骤\n<p>[期望]</p><br/>一个期望\n";
|
||||
$noExpect = "<p>[步骤]</p><br/>一个步骤\n<p>[结果]</p><br/>一个结果\n";
|
||||
|
||||
r($testcase->createStepsFromBugTest($steps1)) && p() && e('step:一个步骤 expect:一个期望 type:item.'); // 测试从 bug 中创建步骤 steps1
|
||||
r($testcase->createStepsFromBugTest($steps2)) && p() && e('step:1. 步骤1 expect: type:item. step:2. 步骤2 expect:期望 type:item.'); // 测试从 bug 中创建步骤 steps2
|
||||
r($testcase->createStepsFromBugTest($noStep)) && p() && e('step:[结果]一个结果 [期望]一个期望 expect:'); // 测试从 bug 中创建步骤 没有步骤
|
||||
r($testcase->createStepsFromBugTest($noResult)) && p() && e('step:[步骤]一个步骤 [期望]一个期望 expect:'); // 测试从 bug 中创建步骤 没有结果
|
||||
r($testcase->createStepsFromBugTest($noExpect)) && p() && e('step:[步骤]一个步骤 [结果]一个结果 expect:'); // 测试从 bug 中创建步骤 没有期望
|
||||
@@ -1157,4 +1157,38 @@ class testcaseTest
|
||||
foreach($configs as $config) $return .= "{$config->key}:{$config->value},";
|
||||
return trim($return, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试从 bug 的步骤创建用例步骤。
|
||||
* Test create case steps from a bug's step.
|
||||
*
|
||||
* @param string $steps
|
||||
* @access public
|
||||
* @return array|string
|
||||
*/
|
||||
public function createStepsFromBugTest(string $steps): array|string
|
||||
{
|
||||
$array = $this->objectModel->createStepsFromBug($steps);
|
||||
|
||||
if(dao::isError()) return dao::getError();
|
||||
|
||||
$return = '';
|
||||
foreach($array as $key => $step)
|
||||
{
|
||||
if(!isset($step->type))
|
||||
{
|
||||
$step->desc = explode("\n", trim($step->desc));
|
||||
|
||||
$return .= 'step:';
|
||||
foreach($step->desc as $desc) $return .= $desc . ' ';
|
||||
$return .= "expect:{$step->expect}";
|
||||
}
|
||||
else
|
||||
{
|
||||
$return .= "step:{$step->desc} expect:{$step->expect} type:{$step->type}. ";
|
||||
}
|
||||
}
|
||||
|
||||
return trim($return);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,8 +76,8 @@ $config->testtask->cases->actionList['confirmChange']['class'] = 'ajax-submit';
|
||||
$config->testtask->cases->actionList['createBug']['icon'] = 'bug';
|
||||
$config->testtask->cases->actionList['createBug']['text'] = $lang->testcase->createBug;
|
||||
$config->testtask->cases->actionList['createBug']['hint'] = $lang->testcase->createBug;
|
||||
$config->testtask->cases->actionList['createBug']['url'] = array('module' => 'testcase', 'method' => 'createBug', 'params' => 'product={product}&branch={branch}&extra=executionID=%execution%,buildID=%build%,caseID={case},version={version},runID={id},testtask=%testtask%', 'data-width' => '90%');
|
||||
$config->testtask->cases->actionList['createBug']['data-width'] = '90%';
|
||||
$config->testtask->cases->actionList['createBug']['url'] = array('module' => 'testcase', 'method' => 'createBug', 'params' => 'product={product}&extra=executionID=%execution%,buildID=%build%,caseID={case},version={version},runID={id},testtask=%testtask%', 'data-width' => '90%');
|
||||
$config->testtask->cases->actionList['createBug']['data-width'] = 'lg';
|
||||
$config->testtask->cases->actionList['createBug']['data-toggle'] = 'modal';
|
||||
|
||||
$config->testtask->cases->actionList['runCase']['icon'] = 'play';
|
||||
|
||||
Reference in New Issue
Block a user