Wordpress plugin + phpunit + custom db tables and constants

Viewed 157

I'm currently trying to set up testing for a WordPress plugin I'm developing. I've followed the guide at https://make.wordpress.org/cli/handbook/misc/plugin-unit-tests, installed dependencies, PHPUnit 7, and generated the test files via the bin/install-wp-tests.sh script. All is good so far.

I can get PHPUnit to run, but my plugin both installs custom database tables (upon activation, if they're not already installed) and also uses some custom constants loaded into the wp-config.php file. However it seems PHPUnit doesn't "activate" the plugin so this hook doesn't fire, thus the DB table installs are never performed and so MySQL necessarily spits out an error that the wp_xxxxx table doesn't exist. Additionally, I get errors saying Use of undefined constant XXXXX

I've been searching around for a while but can't find much regarding unit testing with this sort of custom plugin setup.

So, how can you run plugin activation upon bootstrapping PHPUnit, so that the custom plugin DB tables are installed into the testing database prior to running tests?

Also, how can you pull in custom constants/config (possibly in a separate file with test values for the constants), so that they're available to the plugin like they would be if included in wp-config.php

0 Answers
Related