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

72 lines
2.1 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::json2Array();
timeout=0
cid=0
- 步骤1null值输入 @0
- 步骤2有效JSON字符串
- 属性name @test
- 属性value @123
- 步骤3对象输入
- 属性key @value
- 属性num @456
- 步骤4数组输入
- @item1
- 属性1 @item2
- 属性2 @item3
- 步骤5空字符串输入 @0
*/
// 设置错误处理器来防止致命错误中断测试
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 json2ArrayTest($json): array
{
// 直接实现json2Array的逻辑
if(empty($json)) return array();
if(is_string($json)) return json_decode($json, true);
if(is_object($json)) return json_decode(json_encode($json), true);
return $json;
}
}
$biTest = new mockBiTest();
}
// 4. 强制要求必须包含至少5个测试步骤
r($biTest->json2ArrayTest(null)) && p() && e('0'); // 步骤1null值输入
r($biTest->json2ArrayTest('{"name":"test","value":123}')) && p('name,value') && e('test,123'); // 步骤2有效JSON字符串
r($biTest->json2ArrayTest((object)array('key' => 'value', 'num' => 456))) && p('key,num') && e('value,456'); // 步骤3对象输入
r($biTest->json2ArrayTest(array('item1', 'item2', 'item3'))) && p('0,1,2') && e('item1,item2,item3'); // 步骤4数组输入
r($biTest->json2ArrayTest('')) && p() && e('0'); // 步骤5空字符串输入