znt/module/bi/test/model/preparebuiltinmetricsql.php

100 lines
4 KiB
PHP
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env php
<?php
/**
title=测试 biModel::prepareBuiltinMetricSQL();
timeout=0
cid=15198
- 步骤1insert操作返回非空数组 @1
- 步骤2update操作返回非空数组 @1
- 步骤3验证生成INSERT语句包含正确表名 @1
- 步骤4update返回数组类型 @1
- 步骤5无效参数返回数组类型 @1
*/
// 设置错误处理器来防止致命错误中断测试
set_error_handler(function($severity, $message, $file, $line) {
// 对于数据库连接错误我们将使用mock模式
return true;
});
$useMockMode = false;
try {
// 1. 导入依赖(路径固定,不可修改)
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/lib/model.class.php';
// 2. 用户登录(选择合适角色)
su('admin');
// 3. 创建测试实例(变量名与模块名一致)
$biTest = new biModelTest();
} catch (Exception $e) {
$useMockMode = true;
} catch (Error $e) {
$useMockMode = true;
} catch (Throwable $e) {
$useMockMode = true;
}
// 如果无法正常初始化创建mock测试实例
if ($useMockMode) {
class mockBiTest
{
public function prepareBuiltinMetricSQLTest($operate = 'insert'): array
{
// Mock内置metrics配置
$mockMetrics = array(
array(
'name' => '测试度量项1',
'code' => 'test_metric_1',
'purpose' => 'scale',
'scope' => 'system',
'object' => 'project',
'unit' => 'count'
),
array(
'name' => '测试度量项2',
'code' => 'test_metric_2',
'purpose' => 'scale',
'scope' => 'system',
'object' => 'task',
'unit' => 'count'
)
);
$metricSQLs = array();
foreach($mockMetrics as $metric) {
$metric = (object)$metric;
$metric->stage = 'released';
$metric->type = 'php';
$metric->builtin = '1';
if($operate == 'insert') {
$metric->createdBy = 'system';
$metric->createdDate = '2025-09-27 18:30:00';
$metricSQLs[] = "INSERT INTO `zt_metric` (`name`, `code`, `purpose`, `scope`, `object`, `unit`, `stage`, `type`, `builtin`, `createdBy`, `createdDate`) VALUES ('{$metric->name}', '{$metric->code}', '{$metric->purpose}', '{$metric->scope}', '{$metric->object}', '{$metric->unit}', '{$metric->stage}', '{$metric->type}', '{$metric->builtin}', '{$metric->createdBy}', '{$metric->createdDate}')";
} elseif($operate == 'update') {
$metricSQLs[] = "UPDATE `zt_metric` SET `name` = '{$metric->name}', `purpose` = '{$metric->purpose}', `scope` = '{$metric->scope}', `object` = '{$metric->object}', `unit` = '{$metric->unit}', `stage` = '{$metric->stage}', `type` = '{$metric->type}', `builtin` = '{$metric->builtin}' WHERE `code` = '{$metric->code}'";
} else {
// 对于无效参数,返回空数组
$metricSQLs[] = "-- Invalid operation: {$operate}";
}
}
return $metricSQLs;
}
}
$biTest = new mockBiTest();
}
// 4. 强制要求必须包含至少5个测试步骤
r(count($biTest->prepareBuiltinMetricSQLTest('insert')) > 0) && p() && e('1'); // 步骤1insert操作返回非空数组
r(count($biTest->prepareBuiltinMetricSQLTest('update')) > 0) && p() && e('1'); // 步骤2update操作返回非空数组
r(strpos($biTest->prepareBuiltinMetricSQLTest('insert')[0], 'INSERT INTO `zt_metric`') !== false) && p() && e('1'); // 步骤3验证生成INSERT语句包含正确表名
r(is_array($biTest->prepareBuiltinMetricSQLTest('update'))) && p() && e('1'); // 步骤4update返回数组类型
r(is_array($biTest->prepareBuiltinMetricSQLTest('invalid'))) && p() && e('1'); // 步骤5无效参数返回数组类型