This commit is contained in:
zimoyin
2026-04-01 15:07:15 +08:00
commit ac95dbb1c8
55 changed files with 11634 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
<?php
class PHPUnit
{
/**
* 执行所有PHPUnit测试
* @param string $testClass 测试的文件路径以 tests/ 开头
* @return int 命令执行返回码(0表示成功,非0表示失败)
*/
public static function test(string $testClass = ''): int
{
// 1. 适配不同系统的路径分隔符和PHPUnit可执行文件
$isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
$vendorDir = __DIR__ . DIRECTORY_SEPARATOR . 'vendor';
$phpunitPath = $vendorDir . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'phpunit';
// Windows下需要追加.bat后缀
if ($isWindows) {
$phpunitPath .= '.bat';
}
// 2. 检查PHPUnit文件是否存在
if (!file_exists($phpunitPath)) {
echo "错误:PHPUnit可执行文件不存在,请先执行 composer require --dev phpunit/phpunit\n";
echo "期望路径:{$phpunitPath}\n";
return 1;
}
// 3. 执行PHPUnit命令(使用绝对路径避免当前目录问题)
$cmd = escapeshellcmd($phpunitPath); // 转义命令避免注入风险
$cmd .= " {$testClass}";
exec($cmd, $output, $return_var);
// 4. 输出测试结果(方便调试)
echo "PHPUnit执行结果:\n";
echo implode("\n", $output) . "\n";
echo "返回码:{$return_var}0=成功,非0=失败)\n";
return $return_var;
}
}
function main(): void
{
echo "启动需要 mb_string 拓展 \n";
PHPUnit::test();
}
main();