woocommerce/unit-tests/Bootstrap.php

156 lines
4.1 KiB
PHP
Raw Normal View History

2019-06-10 11:36:11 +00:00
<?php
/**
* PHPUnit bootstrap file
*
* @package WooCommerce/RestApi
*/
namespace WooCommerce\RestApi\UnitTests;
2019-06-10 15:32:12 +00:00
require __DIR__ . '/../src/Utilities/SingletonTrait.php';
use WooCommerce\Utilities\SingletonTrait;
2019-06-10 11:36:11 +00:00
class Bootstrap {
2019-06-10 15:32:12 +00:00
use SingletonTrait;
2019-06-10 11:36:11 +00:00
/**
* Directory path to WP core tests.
*
* @var string
*/
2019-06-10 15:32:12 +00:00
protected $wp_tests_dir;
2019-06-10 11:36:11 +00:00
/**
2019-06-11 10:57:20 +00:00
* unit-tests directory.
*
* @var string
*/
protected $tests_dir;
/**
* WC Core unit-tests directory.
2019-06-10 11:36:11 +00:00
*
* @var string
*/
2019-06-10 15:32:12 +00:00
protected $wc_tests_dir;
2019-06-11 10:57:20 +00:00
/**
* This plugin directory.
*
* @var string
*/
protected $plugin_dir;
/**
* Plugins directory.
*
* @var string
*/
protected $plugins_dir;
2019-06-10 11:36:11 +00:00
/**
* Init unit testing library.
*/
2019-06-10 15:32:12 +00:00
public function init() {
2019-06-11 11:08:56 +00:00
$this->wp_tests_dir = getenv( 'WP_TESTS_DIR' ) ? getenv( 'WP_TESTS_DIR' ) : rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
2019-06-11 10:57:20 +00:00
$this->tests_dir = dirname( __FILE__ );
$this->plugin_dir = dirname( $this->tests_dir );
2019-06-11 11:08:56 +00:00
if ( file_exists( dirname( $this->plugin_dir ) . '/woocommerce/woocommerce.php' ) ) {
// From plugin directory.
$this->plugins_dir = dirname( $this->plugin_dir );
} else {
// Travis.
$this->plugins_dir = getenv( 'WP_CORE_DIR' ) . 'wp-content/plugins';
}
2019-06-11 10:57:20 +00:00
$this->wc_tests_dir = $this->plugins_dir . '/woocommerce/tests';
2019-06-10 11:36:11 +00:00
2019-06-10 15:32:12 +00:00
$this->setup_hooks();
$this->load_framework();
}
/**
* Get tests dir.
*
* @return string
*/
public function get_dir() {
2019-06-11 09:44:54 +00:00
return dirname( __FILE__ );
2019-06-10 11:36:11 +00:00
}
2019-06-10 12:39:46 +00:00
/**
2019-06-10 14:08:29 +00:00
* Does WC Admin exist?
2019-06-10 12:39:46 +00:00
*
* @return boolean
*/
2019-06-10 15:32:12 +00:00
protected function wc_admin_exists() {
2019-06-11 10:57:20 +00:00
return file_exists( $this->plugins_dir . '/woocommerce-admin/woocommerce-admin.php' );
2019-06-10 12:39:46 +00:00
}
2019-06-10 11:36:11 +00:00
/**
* Setup hooks.
*/
2019-06-10 15:32:12 +00:00
protected function setup_hooks() {
2019-06-10 11:36:11 +00:00
// Give access to tests_add_filter() function.
2019-06-10 15:32:12 +00:00
require_once $this->wp_tests_dir . '/includes/functions.php';
2019-06-10 11:36:11 +00:00
\tests_add_filter( 'muplugins_loaded', function() {
2019-06-11 10:57:20 +00:00
require_once $this->plugins_dir . '/woocommerce/woocommerce.php';
require_once $this->plugin_dir . '/woocommerce-rest-api.php';
2019-06-10 12:39:46 +00:00
2019-06-10 15:32:12 +00:00
if ( $this->wc_admin_exists() ) {
2019-06-11 10:57:20 +00:00
require_once $this->plugins_dir . '/woocommerce-admin/woocommerce-admin.php';
2019-06-10 12:39:46 +00:00
}
2019-06-10 11:36:11 +00:00
} );
\tests_add_filter( 'setup_theme', function() {
echo \esc_html( 'Installing WooCommerce...' . PHP_EOL );
define( 'WP_UNINSTALL_PLUGIN', true );
define( 'WC_REMOVE_ALL_DATA', true );
2019-06-11 10:57:20 +00:00
include $this->plugins_dir . '/woocommerce/uninstall.php';
2019-06-10 11:36:11 +00:00
\WC_Install::install();
2019-06-10 15:32:12 +00:00
if ( $this->wc_admin_exists() ) {
2019-06-10 12:39:46 +00:00
echo esc_html( 'Installing WooCommerce Admin...' . PHP_EOL );
2019-06-11 10:57:20 +00:00
require_once $this->plugins_dir . '/woocommerce-admin/includes/class-wc-admin-install.php';
2019-06-10 12:39:46 +00:00
\WC_Admin_Install::create_tables();
\WC_Admin_Install::create_events();
}
2019-06-10 11:36:11 +00:00
$GLOBALS['wp_roles'] = null; // WPCS: override ok.
\wp_roles();
} );
}
/**
* Load the testing framework.
*/
2019-06-10 15:32:12 +00:00
protected function load_framework() {
2019-06-10 11:36:11 +00:00
// Start up the WP testing environment.
2019-06-10 15:32:12 +00:00
require_once $this->wp_tests_dir . '/includes/bootstrap.php';
2019-06-10 11:36:11 +00:00
// WooCommerce Core Testing Framework.
2019-06-10 15:32:12 +00:00
require_once $this->wc_tests_dir . '/framework/class-wc-unit-test-factory.php';
require_once $this->wc_tests_dir . '/framework/vendor/class-wp-test-spy-rest-server.php';
require_once $this->wc_tests_dir . '/includes/wp-http-testcase.php';
require_once $this->wc_tests_dir . '/framework/class-wc-unit-test-case.php';
require_once $this->wc_tests_dir . '/framework/class-wc-rest-unit-test-case.php';
2019-06-10 11:36:11 +00:00
2019-06-11 10:57:20 +00:00
require_once $this->tests_dir . '/Helpers/AdminNotesHelper.php';
require_once $this->tests_dir . '/Helpers/CouponHelper.php';
require_once $this->tests_dir . '/Helpers/CustomerHelper.php';
require_once $this->tests_dir . '/Helpers/OrderHelper.php';
require_once $this->tests_dir . '/Helpers/ProductHelper.php';
require_once $this->tests_dir . '/Helpers/ShippingHelper.php';
require_once $this->tests_dir . '/Helpers/SettingsHelper.php';
require_once $this->tests_dir . '/Helpers/QueueHelper.php';
require_once $this->tests_dir . '/AbstractRestApiTest.php';
require_once $this->tests_dir . '/AbstractReportsTest.php';
2019-06-10 11:36:11 +00:00
}
}
2019-06-10 15:32:12 +00:00
Bootstrap::instance()->init();