* [unittest] modify unittest.
This commit is contained in:
@@ -455,10 +455,11 @@ class adminModel extends model
|
||||
* 获取禅道使用时长。
|
||||
* Get date used object.
|
||||
*
|
||||
* @param string $end
|
||||
* @access public
|
||||
* @return object
|
||||
*/
|
||||
public function genDateUsed(): object
|
||||
public function genDateUsed($end = ''): object
|
||||
{
|
||||
$firstUseDate = $this->dao->select('min(`date`) as `date`')->from(TABLE_ACTION)
|
||||
->where('`date`')->ge(FIRST_RELEASE_DATE)
|
||||
@@ -466,7 +467,7 @@ class adminModel extends model
|
||||
->fetch('date');
|
||||
|
||||
if($firstUseDate) $firstUseDate = substr($firstUseDate, 0, 10);
|
||||
return helper::getDateInterval($firstUseDate);
|
||||
return helper::getDateInterval($firstUseDate, $end);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,135 +1,39 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
include dirname(__FILE__, 5) . '/test/lib/init.php';
|
||||
su('admin');
|
||||
|
||||
zenData('action')->gen(10);
|
||||
|
||||
/**
|
||||
|
||||
title=测试 adminModel::genDateUsed();
|
||||
timeout=0
|
||||
cid=14978
|
||||
|
||||
✓ Expected: numeric, Actual: 2 (string)
|
||||
✓ Expected: numeric, Actual: 9 (string)
|
||||
✓ Expected: numeric, Actual: 8 (string)
|
||||
✓ Expected: numeric, Actual: 16 (string)
|
||||
✓ Expected: all_numeric, Actual: 17,2
|
||||
|
||||
- 查看生成的日期使用情况
|
||||
- 属性year @0
|
||||
- 属性month @10
|
||||
- 属性day @1
|
||||
- 属性hour @0
|
||||
- 属性minute @0
|
||||
- 属性secound @0
|
||||
- 查看生成的日期使用情况
|
||||
- 属性year @0
|
||||
- 属性month @1
|
||||
- 属性day @30
|
||||
- 属性hour @0
|
||||
- 属性minute @0
|
||||
- 属性secound @0
|
||||
|
||||
*/
|
||||
|
||||
// 最小化init逻辑,只保留必要的测试函数定义
|
||||
function r($obj) { global $t; $t = new Test($obj); return $t; }
|
||||
function p($params = '') { global $t; return $t->p($params); }
|
||||
function e($expect) { global $t; return $t->e($expect); }
|
||||
global $tester;
|
||||
|
||||
class Test {
|
||||
public $obj;
|
||||
public $params;
|
||||
public function __construct($obj) { $this->obj = $obj; }
|
||||
public function p($params) { $this->params = $params; return $this; }
|
||||
public function e($expect) {
|
||||
$result = $this->obj;
|
||||
$tester->loadModel('admin');
|
||||
$result = $tester->admin->genDateUsed('2025-01-01');
|
||||
r($result) && p('year,month,day,hour,minute,secound') && e('0,10,1,0,0,0'); // 查看生成的日期使用情况
|
||||
|
||||
// 处理对象属性访问
|
||||
if ($this->params && is_object($result)) {
|
||||
if(strpos($this->params, ',') !== false) {
|
||||
// 多个属性,组合结果
|
||||
$parts = explode(',', $this->params);
|
||||
$values = array();
|
||||
foreach($parts as $part) {
|
||||
$part = trim($part);
|
||||
if(isset($result->$part)) {
|
||||
$values[] = $result->$part;
|
||||
}
|
||||
}
|
||||
$actualStr = implode(',', $values);
|
||||
} else {
|
||||
// 单个属性
|
||||
$param = trim($this->params);
|
||||
if(isset($result->$param)) {
|
||||
$result = $result->$param;
|
||||
$actualStr = (string)$result;
|
||||
} else {
|
||||
$result = 'undefined';
|
||||
$actualStr = 'undefined';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$actualStr = $result === false ? '0' : (string)$result;
|
||||
}
|
||||
|
||||
// 特殊处理:检查数字类型
|
||||
if($expect === 'numeric') {
|
||||
$passed = is_numeric($result);
|
||||
echo ($passed ? "✓ " : "✗ ") . "Expected: numeric, Actual: $actualStr (" . gettype($result) . ")\n";
|
||||
return $passed;
|
||||
}
|
||||
|
||||
// 特殊处理:检查所有逗号分隔的值都是数字
|
||||
if($expect === 'all_numeric') {
|
||||
$passed = true;
|
||||
if(strpos($actualStr, ',') !== false) {
|
||||
$parts = explode(',', $actualStr);
|
||||
foreach($parts as $part) {
|
||||
if(!is_numeric(trim($part))) {
|
||||
$passed = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$passed = is_numeric($actualStr);
|
||||
}
|
||||
echo ($passed ? "✓ " : "✗ ") . "Expected: all_numeric, Actual: $actualStr\n";
|
||||
return $passed;
|
||||
}
|
||||
|
||||
echo ($result == $expect ? "✓ " : "✗ ") . "Expected: $expect, Actual: $actualStr\n";
|
||||
return $result == $expect;
|
||||
}
|
||||
}
|
||||
|
||||
// 实现getDateInterval方法的副本(来自helper.class.php)
|
||||
function getDateInterval($begin, $end = '', $format = '') {
|
||||
if(empty($end)) $end = time();
|
||||
if(is_int($begin)) $begin = date('Y-m-d H:i:s', $begin);
|
||||
if(is_int($end)) $end = date('Y-m-d H:i:s', $end);
|
||||
|
||||
$begin = date_create($begin);
|
||||
$end = date_create($end);
|
||||
$interval = date_diff($begin, $end);
|
||||
|
||||
if($format) {
|
||||
$dateInterval = $interval->format($format);
|
||||
} else {
|
||||
$dateInterval = new stdClass();
|
||||
$dateInterval->year = $interval->format('%y');
|
||||
$dateInterval->month = $interval->format('%m');
|
||||
$dateInterval->day = $interval->format('%d');
|
||||
$dateInterval->hour = $interval->format('%H');
|
||||
$dateInterval->minute = $interval->format('%i');
|
||||
$dateInterval->secound = $interval->format('%s');
|
||||
$dateInterval->year = $dateInterval->year == '00' ? 0 : ltrim($dateInterval->year, '0');
|
||||
$dateInterval->month = $dateInterval->month == '00' ? 0 : ltrim($dateInterval->month, '0');
|
||||
$dateInterval->day = $dateInterval->day == '00' ? 0 : ltrim($dateInterval->day, '0');
|
||||
$dateInterval->hour = $dateInterval->hour == '00' ? 0 : ltrim($dateInterval->hour, '0');
|
||||
$dateInterval->minute = $dateInterval->minute == '00' ? 0 : ltrim($dateInterval->minute, '0');
|
||||
$dateInterval->secound = $dateInterval->secound == '00' ? 0 : ltrim($dateInterval->secound, '0');
|
||||
}
|
||||
return $dateInterval;
|
||||
}
|
||||
|
||||
// 模拟genDateUsed方法的实现
|
||||
function genDateUsedTest() {
|
||||
// 模拟从数据库查询第一次使用日期
|
||||
// 由于测试环境中可能没有真实数据,我们使用一个固定的开始时间
|
||||
$firstUseDate = '2023-01-01';
|
||||
|
||||
// 调用getDateInterval计算时间间隔
|
||||
return getDateInterval($firstUseDate);
|
||||
}
|
||||
|
||||
// 执行测试
|
||||
r(genDateUsedTest()) && p('year') && e('numeric'); // 步骤1:测试返回对象有年份属性且值为数字
|
||||
r(genDateUsedTest()) && p('month') && e('numeric'); // 步骤2:测试返回对象有月份属性且值为数字
|
||||
r(genDateUsedTest()) && p('day') && e('numeric'); // 步骤3:测试返回对象有天数属性且值为数字
|
||||
r(genDateUsedTest()) && p('hour') && e('numeric'); // 步骤4:测试返回对象有小时属性且值为数字
|
||||
r(genDateUsedTest()) && p('minute,secound') && e('all_numeric'); // 步骤5:测试返回对象有分钟和秒属性且值为数字
|
||||
$result = $tester->admin->genDateUsed('2026-01-01');
|
||||
r($result) && p('year,month,day,hour,minute,secound') && e('0,1,30,0,0,0'); // 查看生成的日期使用情况
|
||||
Reference in New Issue
Block a user