|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * PHPUnit bootstrap file |
| 3 | + * Bootstrap the PHPUnit test suite(s). |
| 4 | + * |
| 5 | + * Since WooCommerce Custom Order Tables is meant to integrate seamlessly with WooCommerce itself, |
| 6 | + * the bootstrap relies heavily on the WooCommerce core test suite. |
4 | 7 | * |
5 | 8 | * @package Woocommerce_Order_Tables |
6 | 9 | */ |
7 | 10 |
|
8 | | -$_tests_dir = getenv( 'WP_TESTS_DIR' ); |
| 11 | +$_tests_dir = getenv( 'WP_TESTS_DIR' ) ? getenv( 'WP_TESTS_DIR' ) : rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; |
| 12 | +$_bootstrap = dirname( __DIR__ ) . '/vendor/woocommerce/woocommerce/tests/bootstrap.php'; |
9 | 13 |
|
10 | | -if ( ! $_tests_dir ) { |
11 | | - $_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; |
12 | | -} |
| 14 | +// Verify that Composer dependencies have been installed. |
| 15 | +if ( ! file_exists( $_bootstrap ) ) { |
| 16 | + echo "\033[0;31mUnable to find the WooCommerce test bootstrap file. Have you run `composer install`?\033[0;m" . PHP_EOL; |
| 17 | + exit( 1 ); |
13 | 18 |
|
14 | | -if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) { |
15 | | - echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; |
| 19 | +} elseif ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) { |
| 20 | + echo "\033[0;31mCould not find $_tests_dir/includes/functions.php, have you run `bin/install-wp-tests.sh`?\033[0;m" . PHP_EOL; |
16 | 21 | exit( 1 ); |
17 | 22 | } |
18 | 23 |
|
19 | | -// Give access to tests_add_filter() function. |
| 24 | +// Gives access to tests_add_filter() function. |
20 | 25 | require_once $_tests_dir . '/includes/functions.php'; |
21 | 26 |
|
22 | | -/** |
23 | | - * Manually load the plugin being tested. |
24 | | - */ |
| 27 | +// Manually load the plugin on muplugins_loaded. |
25 | 28 | function _manually_load_plugin() { |
26 | 29 | require dirname( dirname( __FILE__ ) ) . '/wc-custom-order-table.php'; |
27 | 30 | } |
28 | 31 | tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); |
29 | 32 |
|
30 | | -// Start up the WP testing environment. |
31 | | -require $_tests_dir . '/includes/bootstrap.php'; |
32 | | -require __DIR__ . '/testcase.php'; |
33 | | - |
34 | | -/* |
35 | | - * Automatically activate WooCommerce in the test environment. |
36 | | - * |
37 | | - * If WooCommerce cannot be activated, an error message will be thrown and the test execution |
38 | | - * halted, with a non-zero exit code. |
39 | | - */ |
40 | | -$activated = activate_plugin( 'woocommerce/woocommerce.php' ); |
41 | | - |
42 | | -// If the issue is that the plugin isn't installed, attempt to install it. |
43 | | -if ( is_wp_error( $activated ) && 'plugin_not_found' === $activated->get_error_code() ) { |
44 | | - include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
45 | | - |
46 | | - echo PHP_EOL . "WooCommerce is not currently installed in the test environment, attempting to install..."; |
47 | | - |
48 | | - // Retrieve information about WooCommerce. |
49 | | - $plugin_data = wp_remote_get( 'https://api.wordpress.org/plugins/info/1.0/woocommerce.json' ); |
50 | | - |
51 | | - if ( ! is_wp_error( $plugin_data ) ) { |
52 | | - $plugin_data = json_decode( wp_remote_retrieve_body( $plugin_data ) ); |
53 | | - $plugin_url = $plugin_data->download_link; |
54 | | - } else { |
55 | | - $plugin_url = false; |
56 | | - } |
57 | | - |
58 | | - // Download the plugin from the WordPress.org repository. |
59 | | - $upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() ); |
60 | | - $installed = $upgrader->install( $plugin_url ); |
61 | | - |
62 | | - if ( true === $installed ) { |
63 | | - echo "\033[0;32mOK\033[0;m" . PHP_EOL . PHP_EOL; |
64 | | - } else { |
65 | | - echo "\033[0;31mFAIL\033[0;m" . PHP_EOL; |
66 | | - |
67 | | - if ( is_wp_error( $installed ) ) { |
68 | | - printf( 'Unable to install WooCommerce: %s.', $installed->get_error_message() ); |
69 | | - } |
70 | | - |
71 | | - printf( |
72 | | - 'Please download and install WooCommerce into %s' . PHP_EOL, |
73 | | - trailingslashit(dirname( dirname( $_tests_dir ) ) ) |
74 | | - ); |
75 | | - |
76 | | - exit( 1 ); |
77 | | - } |
78 | | - |
79 | | - // Try once again to activate. |
80 | | - $activated = activate_plugin( 'woocommerce/woocommerce.php' ); |
81 | | -} |
82 | | - |
83 | | -// Nothing more we can do, unfortunately. |
84 | | -if ( is_wp_error( $activated ) ) { |
85 | | - echo PHP_EOL . 'WooCommerce could not automatically be activated in the test environment:'; |
86 | | - echo PHP_EOL . $activated->get_error_message(); |
87 | | - echo PHP_EOL . PHP_EOL . "\033[0;31mUnable to proceed with tests, aborting.\033[0m"; |
88 | | - echo PHP_EOL; |
89 | | - exit( 1 ); |
90 | | -} |
| 33 | +// Finally, Start up the WP testing environment. |
| 34 | +require_once $_bootstrap; |
| 35 | +require_once __DIR__ . '/testcase.php'; |
0 commit comments