135 lines
5.2 KiB
PHP
Executable File
135 lines
5.2 KiB
PHP
Executable File
#!/usr/bin/env php
|
||
<?php
|
||
|
||
/**
|
||
|
||
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
|
||
|
||
|
||
*/
|
||
|
||
// 最小化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); }
|
||
|
||
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;
|
||
|
||
// 处理对象属性访问
|
||
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:测试返回对象有分钟和秒属性且值为数字
|