Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

pcov is an alternative to XDebug. It is much more performant. For users with VMs or Docker images, these steps each take place within the image.

To get it running:

  1. Install the pcov extension with

    Code Block
    pecl install pcov
    When running this command, it will output the path it saved itself in. This will be something like

This command generates a unique path to the pcov.so file that will need to be copied, e.g. /usr/local/lib/php/extensions/no-debug-non-zts-20230831/pcov.so

  1. Only for Totara 12: Install pcov/clobber which adds pcov support to PHPUnit 7

    Code Block
    composer require pcov/clobber
    vendor/bin/pcov clobber
  2. Ensure PHP has enough memory in php.ini. Give it heaps, as code coverage is memory intensive.

    Code Block
    memory_limit=8G
  3. Enable and configure the extension in the php.ini

...

  1. . The

...

  1. path/to/pcov.

...

  1. so will be the one you noted down from step 1.

    Code Block
    [pcov]
    extension="path/to/pcov.so" 
    pcov.enabled=1
    pcov.exclude='~(vendor|tests|node_modules|.git|client|.scannerwork)~'
    pcov.initial.memory=1073741824
    pcov.initial.files=30000
  2. Generate the code coverage report

Totara 13 and above

Replace server/path/to/plugin/to/test with the directory path for the plugin you want to test. For example, if I were . e.g. for generating a code coverage report for Totara hierarchy, this would be use server/totara/hierarchy.

Code Block
phpunit --coverage-html /var/www/html/coverage_report /path/to/plugin/to/test

...

Code Block
cd /path/to/code
XDEBUG_MODE=off php -d pcov.directory=`pwd` vendor/bin/phpunit --filter=totara_core --coverage-html /var/www/html/coverage_report

This will create a nicely an XML-formatted report for you to inspect, located at /var/www/html/coverage_report.

...

If using our Totara Docker Development environment - /wiki/spaces/IN/pages/108151852 - you can replace /var/www/html/coverage_report with /var/www/totara/src/coverage_report. Once you’ve done this, execute the following steps to make it accessible:

  1. Once the coverage _ report has been created, set the permissions to allow nginx to access the generated files by running the following in your PHP container:

...