|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * PHPUnit bootstrap file. |
| 3 | + * PHPUnit bootstrap file for Rewrite Rules Inspector plugin tests. |
4 | 4 | * |
5 | 5 | * @package Automattic\RewriteRulesInspector\Tests |
6 | 6 | */ |
7 | 7 |
|
8 | | -// Detect which testsuite is being run based on environment variable or default. |
9 | | -$testsuite = getenv( 'TESTSUITE' ); |
10 | | -if ( ! $testsuite ) { |
11 | | - $testsuite = 'Unit'; |
| 8 | +declare( strict_types=1 ); |
| 9 | + |
| 10 | +namespace Automattic\RewriteRulesInspector\Tests; |
| 11 | + |
| 12 | +use Yoast\WPTestUtils\WPIntegration; |
| 13 | + |
| 14 | +$vendor_dir = dirname( __DIR__ ) . '/vendor'; |
| 15 | + |
| 16 | +require_once $vendor_dir . '/yoast/wp-test-utils/src/WPIntegration/bootstrap-functions.php'; |
| 17 | + |
| 18 | +// Check for a `--testsuite WP_Tests` or `--testsuite=WP_Tests` arg when calling phpunit, |
| 19 | +// and use it to conditionally load up WordPress. |
| 20 | +$argv_local = $GLOBALS['argv'] ?? []; |
| 21 | +$key = (int) array_search( '--testsuite', $argv_local, true ); |
| 22 | +$is_integration = false; |
| 23 | + |
| 24 | +// Check for --testsuite WP_Tests (two separate args). |
| 25 | +if ( $key && isset( $argv_local[ $key + 1 ] ) && 'WP_Tests' === $argv_local[ $key + 1 ] ) { |
| 26 | + $is_integration = true; |
| 27 | +} |
| 28 | + |
| 29 | +// Check for --testsuite=WP_Tests (single arg with equals). |
| 30 | +foreach ( $argv_local as $arg ) { |
| 31 | + if ( '--testsuite=WP_Tests' === $arg ) { |
| 32 | + $is_integration = true; |
| 33 | + break; |
| 34 | + } |
12 | 35 | } |
13 | 36 |
|
14 | | -if ( 'Unit' === $testsuite ) { |
15 | | - require_once __DIR__ . '/Unit/bootstrap.php'; |
| 37 | +if ( $is_integration ) { |
| 38 | + $_tests_dir = WPIntegration\get_path_to_wp_test_dir(); |
| 39 | + |
| 40 | + if ( empty( $_tests_dir ) ) { |
| 41 | + echo 'ERROR: Could not find WordPress test library directory.' . PHP_EOL; |
| 42 | + echo 'Make sure wp-env is running: npm run wp-env start' . PHP_EOL; |
| 43 | + exit( 1 ); |
| 44 | + } |
| 45 | + |
| 46 | + // Give access to tests_add_filter() function. |
| 47 | + require_once $_tests_dir . '/includes/functions.php'; |
| 48 | + |
| 49 | + /** |
| 50 | + * Manually load the plugin being tested. |
| 51 | + */ |
| 52 | + \tests_add_filter( |
| 53 | + 'muplugins_loaded', |
| 54 | + function (): void { |
| 55 | + require dirname( __DIR__ ) . '/rewrite-rules-inspector.php'; |
| 56 | + } |
| 57 | + ); |
| 58 | + |
| 59 | + // Make sure the Composer autoload file has been generated. |
| 60 | + WPIntegration\check_composer_autoload_exists(); |
| 61 | + |
| 62 | + // Start up the WP testing environment. |
| 63 | + require $_tests_dir . '/includes/bootstrap.php'; |
| 64 | + |
| 65 | + /* |
| 66 | + * Register the custom autoloader to overload the PHPUnit MockObject classes when running on PHP 8. |
| 67 | + * |
| 68 | + * This function has to be called _last_, after the WP test bootstrap to make sure it registers |
| 69 | + * itself in FRONT of the Composer autoload (which also prepends itself to the autoload queue). |
| 70 | + */ |
| 71 | + WPIntegration\register_mockobject_autoloader(); |
| 72 | + |
| 73 | + // Add custom test case. |
| 74 | + require __DIR__ . '/Integration/TestCase.php'; |
16 | 75 | } else { |
17 | | - require_once __DIR__ . '/Integration/bootstrap.php'; |
| 76 | + // Unit tests: load Brain Monkey bootstrap. |
| 77 | + require_once $vendor_dir . '/yoast/wp-test-utils/src/BrainMonkey/bootstrap.php'; |
| 78 | + |
| 79 | + // Load Composer autoloader. |
| 80 | + require_once $vendor_dir . '/autoload.php'; |
| 81 | + |
| 82 | + // Stub WordPress functions needed to load the plugin. |
| 83 | + if ( ! function_exists( 'plugin_basename' ) ) { |
| 84 | + /** |
| 85 | + * Stub for plugin_basename to allow loading the plugin. |
| 86 | + * |
| 87 | + * @param string $file Plugin file path. |
| 88 | + * @return string Plugin basename. |
| 89 | + */ |
| 90 | + function plugin_basename( $file ) { |
| 91 | + return basename( dirname( $file ) ) . '/' . basename( $file ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if ( ! defined( 'ABSPATH' ) ) { |
| 96 | + define( 'ABSPATH', '/tmp/' ); |
| 97 | + } |
| 98 | + |
| 99 | + // Load the base test case. |
| 100 | + require_once __DIR__ . '/Unit/TestCase.php'; |
18 | 101 | } |
0 commit comments