Skip to content
This repository was archived by the owner on Jun 15, 2022. It is now read-only.

Commit 21ad543

Browse files
committed
Simplify the test bootstrapping tremendously by using WooCommerce's test framework.
Fundamentally, this plugin should seamlessly integrate with WooCommerce; by loading the WooCommerce test framework, we can explicitly run the platform's entire test suite against an active instance of our plugin.
1 parent 086aa82 commit 21ad543

File tree

3 files changed

+27
-89
lines changed

3 files changed

+27
-89
lines changed

phpunit.xml.dist

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66
convertErrorsToExceptions="true"
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
9+
defaultTestSuite="plugin"
910
>
1011
<testsuites>
11-
<testsuite>
12+
<!-- Plugin-specific tests. -->
13+
<testsuite name="plugin">
1214
<directory prefix="test-" suffix=".php">./tests/</directory>
1315
<exclude>tests/test-sample.php</exclude>
1416
</testsuite>
17+
18+
<!-- WooCommerce core test suite. -->
19+
<testsuite name="core">
20+
<directory suffix=".php">./vendor/woocommerce/woocommerce/tests/unit-tests</directory>
21+
</testsuite>
1522
</testsuites>
1623
</phpunit>

tests/bootstrap.php

Lines changed: 17 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,35 @@
11
<?php
22
/**
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.
47
*
58
* @package Woocommerce_Order_Tables
69
*/
710

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';
913

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 );
1318

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;
1621
exit( 1 );
1722
}
1823

19-
// Give access to tests_add_filter() function.
24+
// Gives access to tests_add_filter() function.
2025
require_once $_tests_dir . '/includes/functions.php';
2126

22-
/**
23-
* Manually load the plugin being tested.
24-
*/
27+
// Manually load the plugin on muplugins_loaded.
2528
function _manually_load_plugin() {
2629
require dirname( dirname( __FILE__ ) ) . '/wc-custom-order-table.php';
2730
}
2831
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
2932

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';

tests/test-bootstrap.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,11 @@
88

99
class BootstrapTest extends TestCase {
1010

11-
/**
12-
* Tear down the plugin after each test run.
13-
*
14-
* @before
15-
*/
16-
public function tear_down_plugin() {
17-
global $wc_custom_order_table;
18-
19-
// Destroy the global $wc_custom_order_table instance.
20-
unset( $wc_custom_order_table );
21-
}
22-
2311
public function test_plugin_only_loads_after_woocommerce() {
2412
global $wc_custom_order_table;
2513

26-
$this->assertNull(
27-
$wc_custom_order_table,
28-
'Before bootstrapping, $wc_custom_order_table should be empty.'
29-
);
14+
// Test bootstrapping may have initialized it for us.
15+
$wc_custom_order_table = null;
3016

3117
do_action( 'woocommerce_init' );
3218

0 commit comments

Comments
 (0)