woocommerce/tests/legacy/bootstrap.php

172 lines
5.8 KiB
PHP
Raw Normal View History

2014-09-01 06:03:52 +00:00
<?php
/**
2015-11-03 13:53:50 +00:00
* WooCommerce Unit Tests Bootstrap
2014-09-01 06:03:52 +00:00
*
* @since 2.2
2019-11-21 00:23:27 +00:00
* @package WooCommerce Tests
*/
use Automattic\WooCommerce\Testing\CodeHacking\CodeHacker;
use Automattic\WooCommerce\Testing\CodeHacking\StaticWrapper;
use Automattic\WooCommerce\Testing\CodeHacking\Hacks\StaticMockerHack;
2019-11-21 00:23:27 +00:00
/**
* Class WC_Unit_Tests_Bootstrap
2014-09-01 06:03:52 +00:00
*/
class WC_Unit_Tests_Bootstrap {
/** @var WC_Unit_Tests_Bootstrap instance */
2014-09-01 06:03:52 +00:00
protected static $instance = null;
/** @var string directory where wordpress-tests-lib is installed */
public $wp_tests_dir;
2014-09-01 06:03:52 +00:00
/** @var string testing directory */
public $tests_dir;
2014-09-01 06:03:52 +00:00
/** @var string plugin directory */
public $plugin_dir;
2014-09-01 06:03:52 +00:00
/**
2015-11-03 13:31:20 +00:00
* Setup the unit testing environment.
2014-09-01 06:03:52 +00:00
*
* @since 2.2
*/
public function __construct() {
$this->tests_dir = dirname( __FILE__ );
$this->plugin_dir = dirname( dirname( $this->tests_dir ) );
$hacking_base = $this->plugin_dir . '/src/Testing/CodeHacking';
require_once $hacking_base . '/StaticWrapper.php';
require_once $hacking_base . '/CodeHacker.php';
require_once $hacking_base . '/Hacks/CodeHack.php';
require_once $hacking_base . '/Hacks/StaticMockerHack.php';
// Define a static wrapper for all the classes that need it.
$classes_that_need_static_wrapper = include_once __DIR__ . '/classes-that-need-static-wrapper.php';
foreach ( $classes_that_need_static_wrapper as $class ) {
$wrapper_class = StaticWrapper::define_for( $class );
CodeHacker::add_hack( new StaticMockerHack( $class, $wrapper_class, true ), true );
}
CodeHacker::enable();
2020-02-05 14:55:19 +00:00
ini_set( 'display_errors', 'on' ); // phpcs:ignore WordPress.PHP.IniSet.display_errors_Blacklisted
error_reporting( E_ALL ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.prevent_path_disclosure_error_reporting, WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting
2014-09-01 06:03:52 +00:00
// 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->wp_tests_dir = getenv( 'WP_TESTS_DIR' ) ? getenv( 'WP_TESTS_DIR' ) : sys_get_temp_dir() . '/wordpress-tests-lib';
2014-09-01 06:03:52 +00:00
2019-11-21 00:23:27 +00:00
// load test function so tests_add_filter() is available.
require_once $this->wp_tests_dir . '/includes/functions.php';
2014-09-01 06:03:52 +00:00
2019-11-21 00:23:27 +00:00
// load WC.
tests_add_filter( 'muplugins_loaded', array( $this, 'load_wc' ) );
2014-09-01 06:03:52 +00:00
2019-11-21 00:23:27 +00:00
// install WC.
tests_add_filter( 'setup_theme', array( $this, 'install_wc' ) );
2014-09-01 06:03:52 +00:00
2019-11-21 00:23:27 +00:00
// load the WP testing environment.
require_once $this->wp_tests_dir . '/includes/bootstrap.php';
2019-11-21 00:23:27 +00:00
// load WC testing framework.
2014-09-01 06:03:52 +00:00
$this->includes();
}
/**
2015-11-03 13:31:20 +00:00
* Load WooCommerce.
*
* @since 2.2
*/
public function load_wc() {
define( 'WC_TAX_ROUNDING_MODE', 'auto' );
2018-01-03 15:47:55 +00:00
define( 'WC_USE_TRANSACTIONS', false );
require_once $this->plugin_dir . '/woocommerce.php';
}
2014-09-01 06:03:52 +00:00
/**
2015-11-03 13:31:20 +00:00
* Install WooCommerce after the test environment and WC have been loaded.
2014-09-01 06:03:52 +00:00
*
* @since 2.2
*/
public function install_wc() {
// Clean existing install first.
define( 'WP_UNINSTALL_PLUGIN', true );
2016-08-24 16:34:13 +00:00
define( 'WC_REMOVE_ALL_DATA', true );
include $this->plugin_dir . '/uninstall.php';
2014-09-01 06:03:52 +00:00
WC_Install::install();
2014-09-01 06:03:52 +00:00
2019-11-11 02:08:02 +00:00
// Initialize the WC API extensions.
\Automattic\WooCommerce\Admin\Install::create_tables();
\Automattic\WooCommerce\Admin\Install::create_events();
2019-11-21 00:23:27 +00:00
// 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 {
2020-02-05 14:55:19 +00:00
$GLOBALS['wp_roles'] = null; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
wp_roles();
}
2014-09-01 06:03:52 +00:00
echo esc_html( 'Installing WooCommerce...' . PHP_EOL );
}
2014-09-01 06:03:52 +00:00
/**
2015-11-03 13:31:20 +00:00
* Load WC-specific test cases and factories.
2014-09-01 06:03:52 +00:00
*
* @since 2.2
*/
public function includes() {
2019-11-21 00:23:27 +00:00
// framework.
require_once $this->tests_dir . '/framework/class-wc-unit-test-factory.php';
require_once $this->tests_dir . '/framework/class-wc-mock-session-handler.php';
require_once $this->tests_dir . '/framework/class-wc-mock-wc-data.php';
require_once $this->tests_dir . '/framework/class-wc-mock-wc-object-query.php';
require_once $this->tests_dir . '/framework/class-wc-mock-payment-gateway.php';
require_once $this->tests_dir . '/framework/class-wc-payment-token-stub.php';
require_once $this->tests_dir . '/framework/vendor/class-wp-test-spy-rest-server.php';
2014-09-05 18:34:51 +00:00
2019-11-21 00:23:27 +00:00
// test cases.
require_once $this->tests_dir . '/includes/wp-http-testcase.php';
require_once $this->tests_dir . '/framework/class-wc-unit-test-case.php';
require_once $this->tests_dir . '/framework/class-wc-api-unit-test-case.php';
require_once $this->tests_dir . '/framework/class-wc-rest-unit-test-case.php';
2019-11-21 00:23:27 +00:00
// Helpers.
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-product.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-coupon.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-fee.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-shipping.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-customer.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-order.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-shipping-zones.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-payment-token.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-settings.php';
2014-09-01 06:03:52 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Get the single class instance.
2014-09-01 06:03:52 +00:00
*
* @since 2.2
* @return WC_Unit_Tests_Bootstrap
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
}
WC_Unit_Tests_Bootstrap::instance();