Files
2026-03-08 22:58:56 +08:00

49 lines
1.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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();