Neoblaster
Neoblaster

Reputation: 13

PHPUnit test Command Line script with coverage

I am developping a command line tool that use PHP Class. I made tests for my Classes with coverage.

Now I would like to tests my PHP Script which used in command line.

I found how to trigger command line with the following thread : How do I test a command-line program with PHPUnit?

I would like to know how can I cover lines executed in the command line script.

I tried to make test as like :

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`;
    }
}

The execution done successfully but nothing is cover in file 'Command.php'.

First, Is it possible ? Then, if it is, how can I do for cover Command Line Script ?

Thanks a lot for all.

Best Regards,

Neoblaster.

Update : I open an issue on GitHub : https://github.com/sebastianbergmann/phpunit/issues/2817

Upvotes: 1

Views: 841

Answers (1)

marv255
marv255

Reputation: 818

When you start your tests you have one instance of the php interpreter which is currently handling your tests' scripts.

Your test script calls command line, which calls second instance of the php interpreter. Right?

Now you have two interpreters running and they are totally separated from each other and don't have any chanses to know what other interpreter is doing now.

So xdebug from your test doesn't know which code lines were used in you command's script and which were not.

I think the best solution for you is to separate your command's class:

//Command.php
class Command
{
}

and your command's index script:

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

So you can test your command's class in your test suite and exclude command_index.php from coverage.

Upvotes: 0

Related Questions