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

Prevent DB warnings in unit tests #339

Merged
merged 5 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/php-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
needs: GetMatrix
strategy:
matrix:
php: [8.1]
php: [8.2]
wp-version: [latest]
# Please note that wc-versions is a string containing versions separated by commas.
# It will be split and loop within the run unit test step below to reduce the time spent.
Expand All @@ -54,7 +54,7 @@ jobs:
- php: 7.4
wp-version: ${{ fromJson(needs.GetMatrix.outputs.wp-versions)[2] }} # L-2 WP Version support
wc-versions: ${{ fromJson(needs.GetMatrix.outputs.wc-versions)[2] }} # L-2 WC Version support
- php: 8.2
- php: 8.3
wp-version: latest
wc-versions: latest

Expand Down
50 changes: 28 additions & 22 deletions tests/class-unittestsbootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ public function init() {
// load test function so tests_add_filter() is available
require_once $this->wp_tests_dir . '/includes/functions.php';

// Filter doing it wrong errors while bootstrapping.
tests_add_filter( 'doing_it_wrong_trigger_error', [ $this, 'filter_doing_it_wrong' ], 10, 2 );

// load WC
tests_add_filter( 'muplugins_loaded', array( $this, 'load_plugins' ) );
tests_add_filter( 'init', array( $this, 'install_wc' ) );
tests_add_filter( 'setup_theme', array( $this, 'install_wc' ) );
tests_add_filter( 'option_active_plugins', [ $this, 'filter_active_plugins' ] );

// load the WP testing environment
Expand All @@ -56,7 +59,7 @@ public function set_show_errors() {
* Set directory paths.
*/
public function set_path_props() {
$this->tests_dir = dirname( __FILE__ );
$this->tests_dir = __DIR__;
$this->plugin_dir = dirname( $this->tests_dir );
$this->plugins_dir = sys_get_temp_dir() . '/wordpress/wp-content/plugins';
$this->wp_tests_dir = sys_get_temp_dir() . '/wordpress-tests-lib';
Expand Down Expand Up @@ -85,7 +88,7 @@ public function filter_active_plugins( $option ) {
public function load_plugins() {
require_once $this->plugins_dir . '/woocommerce/woocommerce.php';
require_once $this->plugin_dir . '/woocommerce-google-analytics-integration.php';

update_option( 'woocommerce_db_version', WC()->version );
update_option( 'gmt_offset', -4 );
}
Expand All @@ -99,33 +102,15 @@ public function install_wc() {
echo 'Installing WooCommerce...' . PHP_EOL;

define( 'WP_UNINSTALL_PLUGIN', true );
define( 'WC_REMOVE_ALL_DATA', true );

include $this->plugins_dir . '/woocommerce/uninstall.php';

global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_attribute_taxonomies" );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_order_items" );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_order_itemmeta" );

\WC_Install::install();

// Ensure wc_category_lookup table exists (WC 6.5+)
add_action(
'init',
function() {
if ( class_exists( \Automattic\WooCommerce\Internal\Admin\CategoryLookup::class ) ) {
\Automattic\WooCommerce\Internal\Admin\CategoryLookup::instance()->regenerate();
}
},
11
);

new \WP_Roles();

WC()->init();

echo 'WooCommerce Finished Installing...' . PHP_EOL;

}

/**
Expand All @@ -149,4 +134,25 @@ public function includes() {
require_once $wc_tests_dir . '/framework/helpers/class-wc-helper-shipping.php';
require_once $wc_tests_dir . '/framework/helpers/class-wc-helper-customer.php';
}

/**
* Filter unwanted doing it wrong messages when installing WooCommerce.
*
* @since x.x.x
*
* @param bool $trigger Trigger error.
* @param string $function_name Calling function name.
* @return bool
*/
public function filter_doing_it_wrong( $trigger, $function_name ) {
// WooCommerce requires the FeaturesController to be used after a call to `woocommerce_init`,
// which is triggered by the WC_Install::install call. We are unable to call install later as
// that would prevent others early hooks into the `init` call from running, so instead we ignore
// this warning to preserve the same load order.
if ( 'FeaturesController::get_compatible_plugins_for_feature' === $function_name ) {
return false;
}

return $trigger;
}
}
Loading