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
*/
2020-06-23 12:04:41 +00:00
use Automattic\WooCommerce\Proxies\LegacyProxy ;
2020-05-18 09:31:59 +00:00
use Automattic\WooCommerce\Testing\Tools\CodeHacking\CodeHacker ;
use Automattic\WooCommerce\Testing\Tools\CodeHacking\Hacks\StaticMockerHack ;
2020-06-02 08:08:24 +00:00
use Automattic\WooCommerce\Testing\Tools\CodeHacking\Hacks\FunctionsMockerHack ;
use Automattic\WooCommerce\Testing\Tools\CodeHacking\Hacks\BypassFinalsHack ;
2020-06-23 12:04:41 +00:00
use Automattic\WooCommerce\Testing\Tools\DependencyManagement\MockableLegacyProxy ;
2020-04-20 13:59:45 +00:00
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 {
2016-03-23 12:14:13 +00:00
/** @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 */
2014-09-01 20:04:09 +00:00
public $wp_tests_dir ;
2014-09-01 06:03:52 +00:00
/** @var string testing directory */
2014-09-01 20:04:09 +00:00
public $tests_dir ;
2014-09-01 06:03:52 +00:00
/** @var string plugin directory */
2014-09-01 20:04:09 +00:00
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 () {
2020-06-23 12:04:41 +00:00
$this -> tests_dir = dirname ( __FILE__ );
$this -> plugin_dir = dirname ( dirname ( $this -> tests_dir ) );
2020-06-26 07:32:04 +00:00
2020-07-14 18:27:50 +00:00
$this -> register_autoloader_for_testing_tools ();
2020-06-02 08:08:24 +00:00
$this -> initialize_code_hacker ();
2020-04-20 13:59:45 +00:00
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
2016-03-23 15:35:06 +00:00
// Ensure server variable is set for WP email functions.
2018-01-22 03:34:13 +00:00
// phpcs:disable WordPress.VIP.SuperGlobalInputUsage.AccessDetected
2016-03-23 15:35:06 +00:00
if ( ! isset ( $_SERVER [ 'SERVER_NAME' ] ) ) {
$_SERVER [ 'SERVER_NAME' ] = 'localhost' ;
}
2018-01-22 03:34:13 +00:00
// phpcs:enable WordPress.VIP.SuperGlobalInputUsage.AccessDetected
2016-03-23 15:35:06 +00:00
2020-03-14 03:35:01 +00:00
$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.
2018-01-22 03:24:52 +00:00
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.
2014-09-01 20:04:09 +00:00
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.
2014-09-01 20:04:09 +00:00
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.
2018-01-22 03:24:52 +00:00
require_once $this -> wp_tests_dir . '/includes/bootstrap.php' ;
2014-09-01 20:04:09 +00:00
2019-11-21 00:23:27 +00:00
// load WC testing framework.
2014-09-01 06:03:52 +00:00
$this -> includes ();
2020-09-21 14:24:24 +00:00
// re-initialize dependency injection, this needs to be the last operation after everything else is in place.
$this -> initialize_dependency_injection ();
2014-09-01 06:03:52 +00:00
}
2020-07-14 18:27:50 +00:00
/**
* Register autoloader for the files in the 'tests/tools' directory , for the root namespace 'Automattic\WooCommerce\Testing\Tools' .
*/
protected static function register_autoloader_for_testing_tools () {
return spl_autoload_register (
function ( $class ) {
$prefix = 'Automattic\\WooCommerce\\Testing\\Tools\\' ;
$base_dir = dirname ( dirname ( __FILE__ ) ) . '/Tools/' ;
$len = strlen ( $prefix );
if ( strncmp ( $prefix , $class , $len ) !== 0 ) {
// no, move to the next registered autoloader.
return ;
}
$relative_class = substr ( $class , $len );
$file = $base_dir . str_replace ( '\\' , '/' , $relative_class ) . '.php' ;
if ( ! file_exists ( $file ) ) {
throw new \Exception ( 'Autoloader for unit tests: file not found: ' . $file );
}
require $file ;
}
);
}
2020-06-02 08:08:24 +00:00
/**
* Initialize the code hacker .
*
* @ throws Exception Error when initializing one of the hacks .
*/
private function initialize_code_hacker () {
2020-11-04 11:56:42 +00:00
$wp_dir = getenv ( 'WP_TESTS_WP_DIR' ) ? getenv ( 'WP_TESTS_WP_DIR' ) : sys_get_temp_dir () . '/wordpress' ;
CodeHacker :: initialize (
array (
$this -> plugin_dir . '/includes/' ,
$wp_dir . '/wp-includes/class-wp-customize-manager.php' ,
)
);
2020-06-02 08:08:24 +00:00
$replaceable_functions = include_once __DIR__ . '/mockable-functions.php' ;
if ( ! empty ( $replaceable_functions ) ) {
FunctionsMockerHack :: initialize ( $replaceable_functions );
CodeHacker :: add_hack ( FunctionsMockerHack :: get_hack_instance () );
}
$mockable_static_classes = include_once __DIR__ . '/classes-with-mockable-static-methods.php' ;
if ( ! empty ( $mockable_static_classes ) ) {
StaticMockerHack :: initialize ( $mockable_static_classes );
CodeHacker :: add_hack ( StaticMockerHack :: get_hack_instance () );
}
CodeHacker :: add_hack ( new BypassFinalsHack () );
CodeHacker :: enable ();
}
2020-09-21 14:24:24 +00:00
/**
* Re - initialize the dependency injection engine .
*
* The dependency injection engine has been already initialized as part of the Woo initialization , but we need
* to replace the registered read - only container with a fully configurable one for testing .
* To this end we hack a bit and use reflection to grab the underlying container that the read - only one stores
* in a private property .
*
* Additionally , we replace the legacy / function proxies with mockable versions to easily replace anything
* in tests as appropriate .
*
* @ throws \Exception The Container class doesn 't have a ' container ' property .
*/
private function initialize_dependency_injection () {
try {
$inner_container_property = new \ReflectionProperty ( \Automattic\WooCommerce\Container :: class , 'container' );
} catch ( ReflectionException $ex ) {
throw new \Exception ( " Error when trying to get the private 'container' property from the " . \Automattic\WooCommerce\Container :: class . ' class using reflection during unit testing bootstrap, has the property been removed or renamed?' );
}
$inner_container_property -> setAccessible ( true );
$inner_container = $inner_container_property -> getValue ( wc_get_container () );
$inner_container -> replace ( LegacyProxy :: class , MockableLegacyProxy :: class );
$inner_container -> reset_all_resolved ();
$GLOBALS [ 'wc_container' ] = $inner_container ;
}
2014-09-01 20:04:09 +00:00
/**
2015-11-03 13:31:20 +00:00
* Load WooCommerce .
2014-09-01 20:04:09 +00:00
*
* @ since 2.2
*/
public function load_wc () {
2017-11-06 21:25:02 +00:00
define ( 'WC_TAX_ROUNDING_MODE' , 'auto' );
2018-01-03 15:47:55 +00:00
define ( 'WC_USE_TRANSACTIONS' , false );
2018-01-22 03:24:52 +00:00
require_once $this -> plugin_dir . '/woocommerce.php' ;
2014-09-01 20:04:09 +00:00
}
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 () {
2017-01-23 17:37:26 +00:00
// Clean existing install first.
2014-09-01 20:04:09 +00:00
define ( 'WP_UNINSTALL_PLUGIN' , true );
2016-08-24 16:34:13 +00:00
define ( 'WC_REMOVE_ALL_DATA' , true );
2018-01-22 03:24:52 +00:00
include $this -> plugin_dir . '/uninstall.php' ;
2014-09-01 06:03:52 +00:00
2014-11-25 17:09:19 +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.
2017-01-23 17:37:26 +00:00
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
2017-01-23 17:37:26 +00:00
wp_roles ();
}
2014-09-01 06:03:52 +00:00
2018-01-22 03:34:13 +00:00
echo esc_html ( 'Installing WooCommerce...' . PHP_EOL );
2014-09-01 20:04:09 +00:00
}
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.
2018-01-22 03:24:52 +00:00
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' ;
2018-04-03 16:09:09 +00:00
require_once $this -> tests_dir . '/framework/class-wc-mock-payment-gateway.php' ;
2018-01-22 03:24:52 +00:00
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.
2018-07-19 17:17:06 +00:00
require_once $this -> tests_dir . '/includes/wp-http-testcase.php' ;
2018-01-22 03:24:52 +00:00
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' ;
2014-10-21 22:26:04 +00:00
2019-11-21 00:23:27 +00:00
// Helpers.
2018-01-22 03:24:52 +00:00
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' ;
2020-08-13 07:57:00 +00:00
// Traits.
require_once $this -> tests_dir . '/framework/traits/trait-wc-rest-api-complex-meta.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 ();