具有覆盖率的 PHPUnit 测试命令行脚本

PHPUnit test Command Line script with coverage

我正在开发一个使用 PHP Class 的命令行工具。 我对 Classes 进行了覆盖测试。

现在我想测试我在命令行中使用的 PHP 脚本。

我找到了如何使用以下线程触发命令行: How do I test a command-line program with PHPUnit?

我想知道如何覆盖命令行脚本中执行的行。

我试过像这样进行测试:

class CommandTest extends \PHPUnit_Framework_TestCase
{
    protected static $command = './src/Command.php ';
    protected static $testWorkingDir = 'tests/Command';

    public function testCLIInstall()
    {
        $command = self::$command . ' --help';
        $output = `$command`;
    }
}

执行成功,但文件 'Command.php'.

中未包含任何内容

首先,这可能吗? 那么,如果是,我该怎么做才能覆盖命令行脚本?

非常感谢大家。

此致,

Neoblaster。

更新:我在 GitHub 上打开了一个问题:https://github.com/sebastianbergmann/phpunit/issues/2817

当您开始测试时,您有一个 php 解释器实例,它当前正在处理您的测试脚本。

您的测试脚本调用命令行,它调用 php 解释器的第二个实例。对吗?

现在你有两个口译员运行,他们彼此完全分开,没有任何机会知道其他口译员现在在做什么。

所以您的测试中的 xdebug 不知道在您的命令脚本中使用了哪些代码行,哪些没有。

我认为最适合您的解决方案是将命令的 class:

分开
//Command.php
class Command
{
}

和您的命令的索引脚本:

//command_index.php
(new Command($argv))->run();

因此您可以在测试套件中测试命令的 class 并将 command_index.php 排除在覆盖范围之外。