1

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

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Marv255, My Command Line Script is not a PHP Class, but it use one. Please find here the corresponding script : Command.php It's not finished, but maybe it's the wrong way to make command line interface ? So I don't know how to play the following line (new Command($argv))->run()
Please check your github. And happy hacktoberfest :)
In addition, the best way to create command line scripts for php i've ever seen is symfony console. Please follow link to read more about it.
I would never succeed alone without your help from your first response. I will study in depth the operation and proceed in the same way for my other tools in command line. I am a self-taught man, so I did not know all best practices in development. More about making CLI tools. Thanks a lot for your contribution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.