+ [misc] Add unit tests for fileZen::unlinkRealFile() method

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
liugang
2025-09-15 15:09:55 +08:00
co-authored by Claude
parent 6e170c6e41
commit 752a2e15e9
3 changed files with 210 additions and 0 deletions
@@ -51,4 +51,63 @@ class fileZenTest
if(dao::isError()) return dao::getError();
return $result;
}
/**
* Test unlinkRealFile method.
*
* @param object $file 文件对象
* @access public
* @return mixed
*/
public function unlinkRealFileZenTest($file): array
{
// 记录调用前的状态
$beforeCount = 0;
if(!empty($file) && isset($file->pathname))
{
$beforeCount = $this->tester->dao->select('COUNT(1) as count')->from(TABLE_FILE)->where('pathname')->eq($file->pathname)->fetch('count');
}
try
{
$method = $this->fileZenTest->getMethod('unlinkRealFile');
$method->setAccessible(true);
$result = $method->invokeArgs($this->fileZenTest->newInstance(), array($file));
}
catch(TypeError $e)
{
// 处理类型错误,比如null对象
return array(
'beforeCount' => $beforeCount,
'afterCount' => $beforeCount,
'called' => false,
'error' => 'type_error'
);
}
catch(Exception $e)
{
return array(
'beforeCount' => $beforeCount,
'afterCount' => $beforeCount,
'called' => false,
'error' => $e->getMessage()
);
}
// 检查调用后的状态
$afterCount = 0;
if(!empty($file) && isset($file->pathname))
{
$afterCount = $this->tester->dao->select('COUNT(1) as count')->from(TABLE_FILE)->where('pathname')->eq($file->pathname)->fetch('count');
}
if(dao::isError()) return dao::getError();
return array(
'beforeCount' => $beforeCount,
'afterCount' => $afterCount,
'called' => true
);
}
}
+55
View File
@@ -0,0 +1,55 @@
#!/usr/bin/env php
<?php
/**
title=测试 fileZen::unlinkRealFile();
timeout=0
cid=0
- 执行fileTest模块的unlinkRealFileZenTest方法,参数是$multiPathFile 属性called @1
- 执行fileTest模块的unlinkRealFileZenTest方法,参数是$uniquePathFile 属性called @1
- 执行fileTest模块的unlinkRealFileZenTest方法,参数是$emptyFile 属性error @type_error
- 执行fileTest模块的unlinkRealFileZenTest方法,参数是$emptyPathFile 属性called @1
- 执行fileTest模块的unlinkRealFileZenTest方法,参数是$nullPathFile 属性called @1
*/
// 1. 导入依赖(路径固定,不可修改)
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/lib/filezen.unittest.class.php';
// 2. zendata数据准备(根据需要配置)
zendata('file')->loadYaml('file_unlinkrealfile', false, 2)->gen(7);
// 3. 用户登录(选择合适角色)
su('admin');
// 4. 创建测试实例(变量名与模块名一致)
$fileTest = new fileZenTest();
// 5. 🔴 强制要求:必须包含至少5个测试步骤
// 步骤1:存在多个相同路径的文件记录时不删除物理文件
$multiPathFile = new stdclass();
$multiPathFile->pathname = 'test/path/file1.txt';
r($fileTest->unlinkRealFileZenTest($multiPathFile)) && p('called') && e('1');
// 步骤2:唯一路径文件记录时删除物理文件
$uniquePathFile = new stdclass();
$uniquePathFile->pathname = 'test/path/unique.txt';
r($fileTest->unlinkRealFileZenTest($uniquePathFile)) && p('called') && e('1');
// 步骤3:文件对象为空时的处理
$emptyFile = null;
r($fileTest->unlinkRealFileZenTest($emptyFile)) && p('error') && e('type_error');
// 步骤4:文件对象路径名为空时的处理
$emptyPathFile = new stdclass();
$emptyPathFile->pathname = '';
r($fileTest->unlinkRealFileZenTest($emptyPathFile)) && p('called') && e('1');
// 步骤5:文件对象路径名为null时的处理
$nullPathFile = new stdclass();
$nullPathFile->pathname = null;
r($fileTest->unlinkRealFileZenTest($nullPathFile)) && p('called') && e('1');
@@ -0,0 +1,96 @@
---
title: 测试 unlinkRealFile 方法的文件数据
desc: 为 unlinkRealFile 方法提供测试数据
fields:
- field: id
note: 文件ID
range: 1-10000
prefix: ""
postfix: ""
loop: 0
format: ""
- field: pathname
note: 文件路径
range: 'test/path/file1.txt{3},test/path/file2.txt{2},test/path/file3.txt{1},test/path/unique.txt{1}'
prefix: ""
postfix: ""
loop: 0
format: ""
- field: title
note: 文件标题
range: 'testfile1.txt,testfile2.txt,testfile3.txt,testfile4.txt,testfile5.txt,testfile6.txt,testfile7.txt'
prefix: ""
postfix: ""
loop: 0
format: ""
- field: extension
note: 文件扩展名
range: 'txt{5},doc{1},pdf{1}'
prefix: ""
postfix: ""
loop: 0
format: ""
- field: size
note: 文件大小
range: 100-50000:R
prefix: ""
postfix: ""
loop: 0
format: ""
- field: objectType
note: 对象类型
range: 'story{3},task{2},bug{2}'
prefix: ""
postfix: ""
loop: 0
format: ""
- field: objectID
note: 对象ID
range: 1-100:R
prefix: ""
postfix: ""
loop: 0
format: ""
- field: gid
note: 组ID
range: 'gid123{3},gid456{2},gid789{2}'
prefix: ""
postfix: ""
loop: 0
format: ""
- field: addedBy
note: 添加者
range: 'admin{3},user1{2},user2{2}'
prefix: ""
postfix: ""
loop: 0
format: ""
- field: addedDate
note: 添加日期
range: '2023-01-01 00:00:00,2023-02-01 00:00:00,2023-03-01 00:00:00,2023-04-01 00:00:00,2023-05-01 00:00:00,2023-06-01 00:00:00,2023-07-01 00:00:00'
prefix: ""
postfix: ""
loop: 0
format: ""
- field: downloads
note: 下载次数
range: 0-10:R
prefix: ""
postfix: ""
loop: 0
format: ""
- field: extra
note: 额外信息
range: '{2},[]{3},editor{2}'
prefix: ""
postfix: ""
loop: 0
format: ""
- field: deleted
note: 删除状态
range: '0{6},1{1}'
prefix: ""
postfix: ""
loop: 0
format: ""