ThinkAdmin/plugin/think-plugs-install/tests/InstallCommandTest.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

134 lines
4.9 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 plugin\install\tests;
use PHPUnit\Framework\TestCase;
use plugin\install\command\project\InstallCommand;
use think\App;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
/**
* @internal
* @coversNothing
*/
class InstallCommandTest extends TestCase
{
public function testRunDiscoversServicesThenPublishesWithFlags(): void
{
$root = sys_get_temp_dir() . '/thinkadmin-install-command-' . bin2hex(random_bytes(6));
mkdir($root, 0777, true);
file_put_contents($root . '/think', "#!/usr/bin/env php\n");
$app = new App($root);
$console = $this->createConsole($app, 23);
$app->instance('console', $console);
$command = new InstallCommand();
$command->setApp($app);
$status = $command->run(new Input(['--force', '--migrate']), new Output('buffer'));
$this->assertSame(23, $status);
$this->assertSame([
['service:discover', []],
['xadmin:publish', ['--force', '--migrate']],
], $console->calls);
}
public function testRunSkipsMigrateWhenNoPluginDeclaresMigration(): void
{
$root = sys_get_temp_dir() . '/thinkadmin-install-command-' . bin2hex(random_bytes(6));
mkdir($root . '/vendor/composer', 0777, true);
file_put_contents($root . '/think', "#!/usr/bin/env php\n");
file_put_contents($root . '/vendor/composer/installed.json', json_encode(['packages' => []], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$app = new App($root);
$console = $this->createConsole($app);
$app->instance('console', $console);
$command = new InstallCommand();
$command->setApp($app);
$status = $command->run(new Input([]), new Output('buffer'));
$this->assertSame(0, $status);
$this->assertSame([
['service:discover', []],
['xadmin:publish', []],
], $console->calls);
}
private function createConsole(App $app, int $publishStatus = 0): object
{
return new class($app, $publishStatus) {
public array $calls = [];
public function __construct(private readonly App $app, private readonly int $publishStatus)
{
}
public function find(string $name): Command
{
$command = new class($this, $name, $name === 'xadmin:publish' ? $this->publishStatus : 0) extends Command {
public function __construct(
private readonly object $owner,
private readonly string $commandName,
private readonly int $status
) {
parent::__construct();
}
protected function configure()
{
$this->setName($this->commandName);
if ($this->commandName === 'xadmin:publish') {
$this->addOption('force', 'f', Option::VALUE_NONE);
$this->addOption('migrate', 'm', Option::VALUE_NONE);
}
}
protected function execute(Input $input, Output $output): int
{
$arguments = [];
if ($this->commandName === 'xadmin:publish') {
if ($input->getOption('force')) {
$arguments[] = '--force';
}
if ($input->getOption('migrate')) {
$arguments[] = '--migrate';
}
}
$this->owner->calls[] = [$this->commandName, $arguments];
return $this->status;
}
};
$command->setApp($this->app);
return $command;
}
};
}
}