So I created a custom plugin and implemented unit tests.
So far, it has been easy to add a WooCommerce dependency and a private plugin dependency (Iconic Plugin).
The problem is that Iconic plugin is dependent on WooCommerce. At each test, it thinks that WooCommerce is not activated.
Therefore, it does not instantiate correctly.
Iconic plugin
class Iconic_Private_Plugin() {
/**
* Constructor
*/
public function __construct() {
if ( ! Iconic_WDS_Core_Helpers::is_plugin_active( 'woocommerce/woocommerce.php' ) && ! Iconic_WDS_Core_Helpers::is_plugin_active( 'woocommerce-old/woocommerce.php' ) ) {
return;
// It stops right here!!!
}
}
}
global $iconic_private_plugin; // Methods can be accessed from global variable $iconic_private_plugin.
$iconic_private_plugin = new Iconic_Private_Plugin();
So here what I did from bash commands, bootstrap & unit tests.
install-wp-tests.sh
install_dependencies() {
WP_SITE_URL="http://localhost:8080"
WP_PLUGIN_DIR=$(pwd)
WP_DB_DATA="$WP_PLUGIN_DIR/tests/data/db.sql"
cd "$WP_CORE_DIR"
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar core config --dbname=$DB_NAME --dbuser=$DB_USER --dbpass=$DB_PASS --dbhost=$DB_HOST --dbprefix=wptests_
php wp-cli.phar db import $WP_DB_DATA
php wp-cli.phar search-replace "http://local.wordpress.test" "$WP_SITE_URL"
php wp-cli.phar theme install twentyseventeen --activate
php wp-cli.phar plugin install https://downloads.wordpress.org/plugin/woocommerce.${WC_VERSION}.zip --activate
php wp-cli.phar plugin install https://downloads.wordpress.org/plugin/posts-to-posts.${P2P_VERSION}.zip --activate
php wp-cli.phar plugin install $WP_PLUGIN_DIR/plugins/iconic-private-plugin.zip --activate
php wp-cli.phar plugin list
}
bootstrap.php
/**
* Setup the unit testing environment.
*/
public function __construct() {
// phpcs:disable WordPress.PHP.DiscouragedPHPFunctions, WordPress.PHP.DevelopmentFunctions
ini_set( 'display_errors', 'on' );
error_reporting( E_ALL );
// phpcs:enable WordPress.PHP.DiscouragedPHPFunctions, WordPress.PHP.DevelopmentFunctions
// Ensure server variable is set for WP email functions.
// phpcs:disable WordPress.VIP.SuperGlobalInputUsage.AccessDetected
if ( ! isset( $_SERVER['SERVER_NAME'] ) ) {
$_SERVER['SERVER_NAME'] = 'localhost';
}
// phpcs:enable WordPress.VIP.SuperGlobalInputUsage.AccessDetected
$this->tests_dir = dirname( __FILE__ );
$this->plugin_dir = dirname( $this->tests_dir );
$this->wp_tests_dir = getenv( 'WP_TESTS_DIR' ) ? getenv( 'WP_TESTS_DIR' ) : rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
// load test function so tests_add_filter() is available.
require_once $this->wp_tests_dir . '/includes/functions.php';
// load Dependencies.
tests_add_filter( 'muplugins_loaded', array( $this, 'my_custom_plugin_manually_load_plugin' ) );
// install WC.
tests_add_filter( 'setup_theme', array( $this, 'install_wc' ) );
// load the WP testing environment.
require_once $this->wp_tests_dir . '/includes/bootstrap.php';
}
/**
* Install WooCommerce after the test environment and WC have been loaded.
*/
public function install_wc() {
// Clean existing install first.
define( 'WP_UNINSTALL_PLUGIN', true );
define( 'WC_REMOVE_ALL_DATA', true );
include (ABSPATH . 'wp-content/plugins/woocommerce/uninstall.php');
WC_Install::install();
// Reload capabilities after install, see https://core.trac.wordpress.org/ticket/28374
if ( version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) {
$GLOBALS['wp_roles']->reinit();
} else {
$GLOBALS['wp_roles'] = null; // WPCS: override ok.
wp_roles();
}
echo esc_html( 'Installing WooCommerce...' . PHP_EOL );
}
/**
* Load Dependencies
*/
public function my_custom_plugin_manually_load_plugin() {
$this->plugins_dir = ABSPATH . str_replace( site_url() . '/', '', plugins_url() ) . '/';
// Load dependencies.
require_once $this->plugins_dir . 'woocommerce/woocommerce.php';
require_once $this->plugins_dir . 'iconic/iconic-private-plugin.php';
// Load plugin.
require_once $this->plugin_dir . '/my-custom-plugin.php';
global $my_custom_plugin;
$my_custom_plugin = new My_Custom_Plugin_Class();
}
My unit tests
<?php
/**
* Sql test case.
*/
class SqlTest extends WP_UnitTestCase {
/**
* Set up fixtures before class for all tests in SqlTest Class.
*
* @param object $factory passed by WP_UnitTestCase.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::createFixtures();
self::createSettings();
}
public function setUp() {
self::set_active_plugins();
}
public static function set_active_plugins() {
activate_plugin(ABSPATH . 'wp-content/plugins/woocommerce/woocommerce.php');
activate_plugin(ABSPATH . 'wp-content/plugins/iconic/iconic-private-plugin.php');
}
/**
* Warehouses fixtures
*/
public static function createFixtures() {
// [...]
}
/**
* Create timeslots
*/
public static function createSettings() {
$settings = array (
// [...]
);
update_option( 'iconic_private_plugin_settings', $settings );
global $iconic_private_plugin;
$iconic_private_plugin->settings = $settings;
}
/**
* Test for custom_function.
*/
public function test_custom_function() {
global $my_custom_plugin, $iconic_private_plugin;
print_r($iconic_private_plugin);
// Class properties are empty, since construct method stop at WooCommerce activation check.
$this->assertTrue(true);
}
}
As you can see I even tried to activate straight in the database the plugins between each test (setUp function).
So i guess the problem could come from bootstrap.php
I could not find any documentation or similar issue through the web.
The only thing that was close enough :
WordPress plugin phpunit tests with dependencies
I also get inspired from WooCommerce tests setup.
Any ideas of what i'm doing wrong ?
from PHPUnit With a custom wordpress plugin having plugins dependencies
No comments:
Post a Comment