+ [misc] Add unit tests for productZen::responseAfterEdit() 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:59 +08:00
co-authored by Claude
parent 6830cdf010
commit 49cf24f194
2 changed files with 120 additions and 0 deletions
@@ -3150,4 +3150,75 @@ class productTest
return $result;
}
/**
* Test responseAfterEdit method.
*
* @param int $productID
* @param int $programID
* @param string $hookMessage
* @access public
* @return array
*/
public function responseAfterEditTest(int $productID, int $programID, string $hookMessage = ''): array
{
global $tester;
// 创建productZen实例
$productZen = new productZen();
$productZen->lang = $tester->lang;
$productZen->config = $tester->config;
$productZen->app = $tester->app;
$productZen->session = $tester->session;
// 创建一个扩展的productZen类来模拟executeHooks和getEditedLocate方法
$extendedProductZen = new class($productZen, $hookMessage) extends productZen {
private $mockHookMessage;
private $originalZen;
public function __construct($originalZen, $hookMessage)
{
$this->originalZen = $originalZen;
$this->mockHookMessage = $hookMessage;
$this->lang = $originalZen->lang;
$this->config = $originalZen->config;
$this->app = $originalZen->app;
$this->session = $originalZen->session;
$this->moduleName = 'product';
}
public function executeHooks(int $objectID): string
{
return $this->mockHookMessage ?: '';
}
protected function getEditedLocate($productID, $programID): array
{
$moduleName = $programID ? 'program' : 'product';
$methodName = $programID ? 'product' : 'view';
$param = $programID ? "programID=$programID" : "product=$productID";
$location = "test_link_{$moduleName}_{$methodName}";
if(!$programID) {
// 模拟session设置
$this->session->set('productList', "test_link_product_browse", 'product');
}
return array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $location);
}
public function createLink(string $moduleName, string $methodName = 'index', array|string $vars = [], string $viewType = '', bool $onlybody = false): string
{
return "test_link_{$moduleName}_{$methodName}";
}
};
// 使用反射调用被测试的protected方法
$method = $this->objectZen->getMethod('responseAfterEdit');
$method->setAccessible(true);
$result = $method->invokeArgs($extendedProductZen, array($productID, $programID));
if(dao::isError()) return dao::getError();
return $result;
}
}
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env php
<?php
/**
title=测试 productZen::responseAfterEdit();
timeout=0
cid=0
- 步骤1:正常产品编辑,无钩子消息
- 属性result @success
- 属性load @test_link_product_view
- 步骤2:程序集产品编辑,无钩子消息
- 属性result @success
- 属性load @test_link_program_product
- 步骤3:正常产品编辑,有钩子消息
- 属性result @success
- 属性message @自定义钩子消息
- 步骤4:程序集产品编辑,有钩子消息
- 属性result @success
- 属性load @test_link_program_product
- 步骤5:无效productID测试属性result @success
*/
// 1. 导入依赖(路径固定,不可修改)
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/lib/product.unittest.class.php';
// 2. zendata数据准备(根据需要配置)
$table = zenData('product');
$table->id->range('1-10');
$table->name->range('产品A,产品B,产品C');
$table->status->range('normal');
$table->program->range('0,1,2');
$table->gen(5);
// 3. 用户登录(选择合适角色)
su('admin');
// 4. 创建测试实例(变量名与模块名一致)
$productTest = new productTest();
// 5. 🔴 强制要求:必须包含至少5个测试步骤
r($productTest->responseAfterEditTest(1, 0, '')) && p('result,load') && e('success,test_link_product_view'); // 步骤1:正常产品编辑,无钩子消息
r($productTest->responseAfterEditTest(2, 1, '')) && p('result,load') && e('success,test_link_program_product'); // 步骤2:程序集产品编辑,无钩子消息
r($productTest->responseAfterEditTest(3, 0, '自定义钩子消息')) && p('result,message') && e('success,自定义钩子消息'); // 步骤3:正常产品编辑,有钩子消息
r($productTest->responseAfterEditTest(4, 2, '钩子执行成功')) && p('result,load') && e('success,test_link_program_product'); // 步骤4:程序集产品编辑,有钩子消息
r($productTest->responseAfterEditTest(0, 0, '')) && p('result') && e('success'); // 步骤5:无效productID测试