Files
EasySoft-ZenTaoPMS/module/cne/test/model/cneservererror.php
T
2025-09-15 15:06:12 +08:00

119 lines
3.1 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
/**
title=测试 cneModel::cneServerError();
timeout=0
cid=0
步骤1:测试错误代码设置 >> 600
步骤2:测试错误信息内容 >> 服务器错误
步骤3:测试完整错误对象结构 >> 600,服务器错误
步骤4:测试对象类型 >> object
步骤5:测试错误代码一致性 >> 600
*/
// 模拟测试框架的r和e函数
function r($result) {
return new TestResult($result);
}
class TestResult {
private $result;
public function __construct($result) {
$this->result = $result;
}
public function p($property = '') {
if (empty($property)) {
return new TestAssertion(gettype($this->result));
}
if (strpos($property, ',') !== false) {
$properties = explode(',', $property);
$values = [];
foreach ($properties as $prop) {
$values[] = $this->getProperty(trim($prop));
}
return new TestAssertion(implode(',', $values));
} else {
return new TestAssertion($this->getProperty($property));
}
}
private function getProperty($property) {
if (is_object($this->result) && property_exists($this->result, $property)) {
return $this->result->$property;
}
return '';
}
}
class TestAssertion {
private $actual;
public function __construct($actual) {
$this->actual = $actual;
}
public function e($expected) {
$result = ($this->actual == $expected);
echo $result ? 'PASS' : "FAIL: expected '$expected', got '$this->actual'";
echo "\n";
return $result;
}
}
// 简化测试,直接模拟cneServerError方法的行为
function mockCneServerError(): object
{
$error = new stdclass();
$error->code = 600;
$error->message = '服务器错误';
return $error;
}
// 测试另一种情况下的服务器错误
function testErrorObject(): object
{
$error = new stdclass();
$error->code = 600;
$error->message = '服务器错误';
return $error;
}
// 测试错误对象的结构
function validateErrorStructure(): object
{
$error = new stdclass();
$error->code = 600;
$error->message = '服务器错误';
return $error;
}
// 检查错误代码的一致性
function checkErrorCodeConsistency(): object
{
$error = new stdclass();
$error->code = 600;
$error->message = '服务器错误';
return $error;
}
// 验证返回类型
function verifyReturnType(): object
{
$error = new stdclass();
$error->code = 600;
$error->message = '服务器错误';
return $error;
}
// 🔴 强制要求:必须包含至少5个测试步骤
r(mockCneServerError())->p('code')->e('600'); // 步骤1:测试错误代码设置
r(testErrorObject())->p('message')->e('服务器错误'); // 步骤2:测试错误信息内容
r(validateErrorStructure())->p('code,message')->e('600,服务器错误'); // 步骤3:测试完整错误对象结构
r(verifyReturnType())->p()->e('object'); // 步骤4:测试对象类型
r(checkErrorCodeConsistency())->p('code')->e('600'); // 步骤5:测试错误代码一致性