Add a method by which testcases can load necessary files via include_once.

There are a number of files that are only loaded when another function is called (for example, load a reporting class when displaying a widget). An autoloader would be the ideal way to handle this (both in production and for tests), but until PHP compatibility is sorted out this commit adds an easy fix:

Testcases can now populate the protected, static $includes property with filepaths relative to the project root; when the testcase boots up, these files will automatically be included.
This commit is contained in:
Steve Grunwell 2018-01-12 20:24:38 +00:00
parent d0efd7251e
commit e68084d7b8
2 changed files with 27 additions and 5 deletions

View File

@ -12,6 +12,28 @@ class WC_Unit_Test_Case extends WP_UnitTestCase {
/** @var WC_Unit_Test_Factory instance */ /** @var WC_Unit_Test_Factory instance */
protected $factory; protected $factory;
/**
* Additional files, relative to the plugin's root directory, that should be explicitly included.
*
* @var array
*/
protected static $includes;
/**
* If a test has declared include files, load them before running the tests in the class.
*
* @beforeClass
*/
public static function include_dependencies() {
$base_dir = trailingslashit( dirname( dirname( __DIR__ ) ) );
if ( ! empty( static::$includes ) ) {
foreach ( (array) static::$includes as $include ) {
include_once $base_dir . $include;
}
}
}
/** /**
* Setup test case. * Setup test case.
* *

View File

@ -11,13 +11,13 @@
class WC_Tests_Admin_Report extends WC_Unit_Test_Case { class WC_Tests_Admin_Report extends WC_Unit_Test_Case {
/** /**
* Load the necessary files, as they're not automatically loaded by WooCommerce. * Additional files, relative to the plugin's root directory, that should be explicitly included.
* *
* @beforeClass * @var array
*/ */
public static function includes() { public static $includes = array(
include_once WC_Unit_Tests_Bootstrap::instance()->plugin_dir . '/includes/admin/reports/class-wc-admin-report.php'; 'includes/admin/reports/class-wc-admin-report.php',
} );
/** /**
* Clear cached report data. * Clear cached report data.