init
This commit is contained in:
+49
@@ -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();
|
||||
Reference in New Issue
Block a user