ThinkAdmin/plugin/think-plugs-wuma/tests/CodeControllerTest.php
Anyon e634118a22 refactor(plugin): 迁移 v8 插件化组件体系
将 v6 中直接放在本地 app 的后台与微信能力迁移为 v8 插件组件,并把运行时基础能力沉淀到独立插件包。

主要内容:

- 新增 think-library、system、worker、static、install 等基础插件包。

- 新增 account、payment、wechat-client、wechat-service、wemall、wuma 等业务插件包。

- 移除 v6 的 app/admin 与 app/wechat 本地应用实现,改由插件分发接管。

- 将 Helper 能力彻底并入 System,统一为 plugin\system\helper\* 命名空间。

- 同步插件迁移发布清单与根 route 占位,保证安装发布流程可复现。
2026-05-08 15:30:46 +08:00

139 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
/**
* +----------------------------------------------------------------------
* | ThinkAdmin Plugin for ThinkAdminDeveloper
* +----------------------------------------------------------------------
* | Copyright (c) 2014~2026 ThinkAdmin [ thinkadmin.top ]
* +----------------------------------------------------------------------
* | Official Website: https://thinkadmin.top
* +----------------------------------------------------------------------
* | Licensed: https://mit-license.org
* | Disclaimer: https://thinkadmin.top/disclaimer
* | Vip Rights: https://thinkadmin.top/vip-introduce
* +----------------------------------------------------------------------
* | Gitee Repository: https://gitee.com/zoujingli/ThinkAdmin
* | Github Repository: https://github.com/zoujingli/ThinkAdmin
* +----------------------------------------------------------------------
*/
namespace think\admin\tests;
use plugin\wuma\controller\Code as CodeController;
use plugin\wuma\service\CodeService;
use think\admin\runtime\RequestContext;
use think\admin\tests\Support\SqliteIntegrationTestCase;
use think\exception\HttpResponseException;
use think\Request;
/**
* @internal
* @coversNothing
*/
class CodeControllerTest extends SqliteIntegrationTestCase
{
public function testAddControllerReturnsStandardHttpCodePayload(): void
{
$response = $this->callCodeController('add', [
'type' => 1,
'sns_length' => 0,
'max_length' => 0,
'mid_length' => 0,
'min_length' => 8,
'hex_length' => 0,
'ver_length' => 0,
'max_mid' => 0,
'mid_min' => 0,
'max_number' => 0,
'mid_number' => 0,
'remark' => '测试批次',
'number' => 2,
'template' => '{min}',
]);
$this->assertSame(200, intval($response['code'] ?? 0));
$this->assertSame('物码数码生成成功!', $response['info'] ?? '');
$this->assertNotEmpty($response['data']['batch'] ?? '');
$this->assertSame(
CodeService::withFile(strval($response['data']['batch'] ?? '')),
strval($response['data']['file'] ?? '')
);
$this->assertSame(1, intval($this->app->db->table('plugin_wuma_code_rule')->count()));
$this->assertSame(1, intval($this->app->db->table('plugin_wuma_code_rule_range')->count()));
}
protected function defineSchema(): void
{
$this->executeStatements([
<<<'SQL'
CREATE TABLE plugin_wuma_code_rule (
id INTEGER PRIMARY KEY AUTOINCREMENT,
type INTEGER DEFAULT 1,
batch TEXT DEFAULT '',
max_mid INTEGER DEFAULT 0,
mid_min INTEGER DEFAULT 0,
sns_start INTEGER DEFAULT 0,
sns_after INTEGER DEFAULT 0,
sns_length INTEGER DEFAULT 0,
max_length INTEGER DEFAULT 0,
mid_length INTEGER DEFAULT 0,
min_length INTEGER DEFAULT 0,
hex_length INTEGER DEFAULT 0,
ver_length INTEGER DEFAULT 0,
mid_number INTEGER DEFAULT 0,
max_number INTEGER DEFAULT 0,
template TEXT DEFAULT '',
number INTEGER DEFAULT 0,
remark TEXT DEFAULT '',
status INTEGER DEFAULT 1,
create_time TEXT DEFAULT NULL,
update_time TEXT DEFAULT NULL,
delete_time TEXT DEFAULT NULL
)
SQL,
<<<'SQL'
CREATE TABLE plugin_wuma_code_rule_range (
id INTEGER PRIMARY KEY AUTOINCREMENT,
type INTEGER DEFAULT 1,
batch TEXT DEFAULT '',
code_type TEXT DEFAULT '',
code_length INTEGER DEFAULT 0,
range_start INTEGER DEFAULT 0,
range_after INTEGER DEFAULT 0,
range_number INTEGER DEFAULT 0
)
SQL,
]);
}
private function callCodeController(string $action, array $data): array
{
$request = (new Request())
->withGet($data)
->withPost($data)
->setMethod('POST')
->setController('code')
->setAction($action);
$this->setRequestPayload($request, $data);
RequestContext::clear();
$this->activateApplicationContext($request);
try {
$controller = new CodeController($this->app);
$controller->{$action}();
self::fail("Expected CodeController::{$action} to throw HttpResponseException.");
} catch (HttpResponseException $exception) {
return json_decode($exception->getResponse()->getContent(), true) ?: [];
}
}
private function setRequestPayload(Request $request, array $data): void
{
$property = new \ReflectionProperty(Request::class, 'request');
$property->setAccessible(true);
$property->setValue($request, $data);
}
}