Directory

Error : No code coverage driver available in PHPunit test · Issue #42286 · WordPress/gutenberg · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error : No code coverage driver available in PHPunit test #42286

Open
akasunil opened this issue Jul 9, 2022 · 9 comments
Open

Error : No code coverage driver available in PHPunit test #42286

akasunil opened this issue Jul 9, 2022 · 9 comments
Labels
Developer Experience Ideas about improving block and theme developer experience Needs Technical Feedback Needs testing from a developer perspective. [Package] Env /packages/env [Type] Enhancement A suggestion for improvement.

Comments

@akasunil
Copy link
Contributor

akasunil commented Jul 9, 2022

Description

while running PHPUnit test in gutenberg with docker setup, we cant able to generate code coverage report. Looked through all documentation and followed step as provided. still received error of code coverage driver. It must be issue with gutenberg docker image.

Step-by-step reproduction instructions

  1. Setup gutenberg with docker
  2. Start docker with command npm run wp-env start -- --xdebug=develop,coverage
  3. Run Unit test with --coverage-text or --coverage-html CLI arguments.

Screenshots, screen recording, code snippet

CleanShot 2022-07-09 at 11 06 18@2x

Environment info

  • Running test in Gutenberg trunk with docker setup.

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@akasunil akasunil changed the title No code coverage driver available Error : No code coverage driver available in PHPunit test Jul 9, 2022
@akasunil
Copy link
Contributor Author

akasunil commented Jul 9, 2022

CC: @andreiglingeanu

@kathrynwp kathrynwp added Needs Technical Feedback Needs testing from a developer perspective. Developer Experience Ideas about improving block and theme developer experience labels Jul 11, 2022
@kasparsd
Copy link
Contributor

The problem is caused by the --xdebug flag being applied only to the wordpress image config here:

await writeFile(
path.resolve( config.workDirectoryPath, 'Dockerfile' ),
dockerFileContents(
dockerComposeConfig.services.wordpress.image,
config
)
);

We should either (1) update the logic to apply the xdebug steps to all containers or (2) update all containers with PHP to include the xdebug extension and have it enabled by the additional flags.

@akasunil
Copy link
Contributor Author

Thanks @kasparsd. If you have any solution for this, Please do suggest.

@kasparsd
Copy link
Contributor

Looks like wpdev-docker-images containers actually do support Xdebug as long as LOCAL_PHP_XDEBUG environment variable is set to true.

So we need to update the env package with a way to pass environment variables to all containers. Technically, it mean updating the logic that sets environment: definitions for all containers in the generated docker-compose.yml file:

services: {
mysql: {
image: 'mariadb',
ports: [ '3306' ],
environment: {
MYSQL_ROOT_PASSWORD:
dbEnv.credentials.WORDPRESS_DB_PASSWORD,
MYSQL_DATABASE: dbEnv.development.WORDPRESS_DB_NAME,
},
volumes: [ 'mysql:/var/lib/mysql' ],
},
'tests-mysql': {
image: 'mariadb',
ports: [ '3306' ],
environment: {
MYSQL_ROOT_PASSWORD:
dbEnv.credentials.WORDPRESS_DB_PASSWORD,
MYSQL_DATABASE: dbEnv.tests.WORDPRESS_DB_NAME,
},
volumes: [ 'mysql-test:/var/lib/mysql' ],
},
wordpress: {
build: '.',
depends_on: [ 'mysql' ],
image: developmentWpImage,
ports: [ developmentPorts ],
environment: {
...dbEnv.credentials,
...dbEnv.development,
},
volumes: developmentMounts,
},
'tests-wordpress': {
depends_on: [ 'tests-mysql' ],
image: testsWpImage,
ports: [ testsPorts ],
environment: {
...dbEnv.credentials,
...dbEnv.tests,
},
volumes: testsMounts,
},
cli: {
depends_on: [ 'wordpress' ],
image: developmentWpCliImage,
volumes: developmentMounts,
user: cliUser,
environment: {
...dbEnv.credentials,
...dbEnv.development,
},
},
'tests-cli': {
depends_on: [ 'tests-wordpress' ],
image: testsWpCliImage,
volumes: testsMounts,
user: cliUser,
environment: {
...dbEnv.credentials,
...dbEnv.tests,
},
},
composer: {
image: 'composer',
volumes: [ `${ config.configDirectoryPath }:/app` ],
},
phpunit: {
image: phpunitImage,
depends_on: [ 'tests-wordpress' ],
volumes: [
...testsMounts,
...( ! isMappingTestUploads
? [ 'phpunit-uploads:/var/www/html/wp-content/uploads' ]
: [] ),
],
environment: {
LOCAL_DIR: 'html',
WP_PHPUNIT__TESTS_CONFIG:
'/var/www/html/phpunit-wp-config.php',
...dbEnv.credentials,
...dbEnv.tests,
},
},
},

@iandunn iandunn added the [Package] Env /packages/env label Nov 15, 2022
@akasunil
Copy link
Contributor Author

do you think it because of this command in package.json
"pretest:unit:php": "wp-env start",

It is stated that we have pass Xdebug argument along with env command as below. But when we execute npm run test:unit:php command, docker env command executed without xdebug arguments.

npm run wp-env start -- --xdebug=develop,coverage

@jrtashjian
Copy link
Contributor

Today, I spent some time attempting to resolve this issue for a project. In order to generate an HTML coverage report, I added the following commands to my package.json file:

"pretest:unit:php": "wp-env start --xdebug=develop,coverage",
"test:unit:php": "wp-env run tests-wordpress /var/www/html/wp-content/plugins/$(basename $(pwd))/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/$(basename $(pwd))/phpunit.xml.dist --verbose --coverage-html /var/www/html/wp-content/plugins/$(basename $(pwd))/coverage",

Run npm run test:unit:php and you'll see a coverage/ folder created with the coverage report generated in HTML.

@akasunil
Copy link
Contributor Author

@jrtashjian I tried your solution with my docker setup but no luck. coverage report still not generated.

@jordesign jordesign added the [Type] Enhancement A suggestion for improvement. label Sep 5, 2023
@jrtashjian
Copy link
Contributor

It appears there are irregularities in how the wp-env run command executes the phpunit call within the container. While I continue to investigate, could you please try this to verify if Xdebug coverage is functioning correctly inside the container?

  1. wp-env start --xdebug=develop,coverage
  2. wp-env run --env-cwd="wp-content/plugins/${PWD##*/}" tests-wordpress bash
  3. (inside the container now) phpunit --coverage-text

I was able to see coverage output.

@jrtashjian
Copy link
Contributor

Here's an updated solution

  1. Install PHPUnit Binary:

    You have the option to install the PHPUnit binary either via Yoast's PHPUnit Polyfills or the official PHPUnit package. Depending on your preference, you can choose one of the following commands:

    Using Yoast's PHPUnit Polyfills:

    composer require --dev yoast/phpunit-polyfills
    

    Using PHPUnit:

    composer require --dev phpunit/phpunit
    

    These commands will add the necessary PHPUnit dependencies to your project.

  2. Start wp-env with Xdebug:

    To enable Xdebug for code coverage, you can use the following command:

    npx wp-env start --xdebug=develop,coverage
    

    This command starts your WordPress environment with Xdebug enabled, which is essential for capturing coverage data.

  3. Run PHPUnit Tests:

    To execute PHPUnit tests within your WordPress environment, you can use the following command:

    npx wp-env run --env-cwd="wp-content/plugins/${PWD##*/}" tests-wordpress vendor/bin/phpunit -- -c phpunit.xml.dist
    

    This command runs PHPUnit tests and specifies the configuration file phpunit.xml.dist.

  4. Run PHPUnit with Coverage:

    To generate code coverage reports, you can use this command:

    npx wp-env run --env-cwd="wp-content/plugins/${PWD##*/}" tests-wordpress vendor/bin/phpunit -- -c phpunit.xml.dist --coverage-html phpunit/coverage
    

    This command runs PHPUnit with code coverage enabled and generates HTML coverage reports in the phpunit/coverage directory.

By following these steps, you should be able to set up and run PHPUnit tests with code coverage in your project. Once #54508 is merged, you should be able to run the globally available phpunit inside the container. However, PHPUnit advises adding the dependency to your project for better control and compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Developer Experience Ideas about improving block and theme developer experience Needs Technical Feedback Needs testing from a developer perspective. [Package] Env /packages/env [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

6 participants