phpphpunitcirclecicode-climate

A way to run phpunit with coverage on a set of files


Short form

I have a list of file names in a shell variable and need to run a single phpunit run with Clover coverage calculation on the entire list. How do I do that?

With extended context

I'm working on setting up a parallelized CircleCI test job for a php project. The approach is to split all tests into several groups and run each group on a dedicated runner.

Splitting is handled by CircleCI and the list of files to be executed on a runner is piped into a command that can be edited. The approach from CircleCI docs would use this piped list of filenames and run phpunit on each file separately.

TESTS_TO_RUN=$(./vendor/bin/phpunit-finder)
echo "$TESTS_TO_RUN" | circleci tests run --command="xargs -I{} -d\" \"  ./vendor/bin/phpunit {} " --verbose --split-by=timings

(note that circleci tests run would split TESTS_TO_RUN into several smaller lists and would pass those smaller ones into the --command string on particular runners)

This works ok for just the tests. Yet, when I want to calculate coverage it all goes to hell. Simply adding coverage instructions to the command above causes test-coverage calculation to be executed after each test file and the results would simply override themselves.

echo "$TESTS_TO_RUN" | circleci tests run --command="XDEBUG_MODE=coverage xargs -I{} -d\" \"  ./vendor/bin/phpunit {} --coverage-clover clover.xml" --verbose --split-by=timings

I'm looking for a way to only run phpunit with coverage calculation once for the entire set of files provided via xargs. How would I do that?


Solution

  • The solution I ended up on was creating a php file that generates a temporary phpunit config file for a particular test run.

    # first I swapped circleci tests run with circleci tests split command 
    # to get the small list of test files into a variable
    TESTS_TO_RUN =$(./vendor/bin/phpunit-finder | circleci tests split --split-by=timings)
    
    # phpunit-generator.php is a tiny php script that echoes a phpunit config 
    # with the specific test files provided by the input
    php .circleci/phpunit-generator.php $TESTS_TO_RUN > phpunit-split.xml
    
    XDEBUG_MODE=coverage php ./vendor/bin/phpunit --configuration phpunit-split.xml --coverage-clover clover.xml