From 752a2e15e902c491dc7571b17a526b2bede7bdfd Mon Sep 17 00:00:00 2001 From: liugang Date: Sun, 14 Sep 2025 18:14:11 +0800 Subject: [PATCH] + [misc] Add unit tests for fileZen::unlinkRealFile() method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../file/test/lib/filezen.unittest.class.php | 59 ++++++++++++ module/file/test/zen/unlinkrealfile.php | 55 +++++++++++ .../test/zen/yaml/file_unlinkrealfile.yaml | 96 +++++++++++++++++++ 3 files changed, 210 insertions(+) create mode 100755 module/file/test/zen/unlinkrealfile.php create mode 100644 module/file/test/zen/yaml/file_unlinkrealfile.yaml diff --git a/module/file/test/lib/filezen.unittest.class.php b/module/file/test/lib/filezen.unittest.class.php index 7568042c73..0615a4dc46 100644 --- a/module/file/test/lib/filezen.unittest.class.php +++ b/module/file/test/lib/filezen.unittest.class.php @@ -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 + ); + } } \ No newline at end of file diff --git a/module/file/test/zen/unlinkrealfile.php b/module/file/test/zen/unlinkrealfile.php new file mode 100755 index 0000000000..aac2cb1a97 --- /dev/null +++ b/module/file/test/zen/unlinkrealfile.php @@ -0,0 +1,55 @@ +#!/usr/bin/env php +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'); \ No newline at end of file diff --git a/module/file/test/zen/yaml/file_unlinkrealfile.yaml b/module/file/test/zen/yaml/file_unlinkrealfile.yaml new file mode 100644 index 0000000000..6e3b4f58c6 --- /dev/null +++ b/module/file/test/zen/yaml/file_unlinkrealfile.yaml @@ -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: "" \ No newline at end of file