Completed
Push — update/genericize-testing ( 33c516 )
by
unknown
209:19 queued 199:10
created

bootstrap.php ➔ _manually_load_plugin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHPUnit bootstrap file to apply WordPress core's monkey-patching to versions
4
 * of PHPUnit that don't natively support PHP 8.0 for PHP 8.0.
5
 *
6
 * @package automattic/jetpack
7
 */
8
9
// Assume we're in tests/php/bootstrap.php.
10
$_plugin_root = dirname( dirname( __DIR__ ) );
11
12
// Locate WordPress or wordpress-develop. We look in several places.
13
if ( false !== getenv( 'WP_DEVELOP_DIR' ) ) {
14
	// Jetpack Monorepo environment variable.
15
	$_tests_dir = getenv( 'WP_DEVELOP_DIR' );
16
	if ( file_exists( "$_tests_dir/tests/phpunit/" ) ) {
17
		$_tests_dir .= '/tests/phpunit/';
18
	}
19
} elseif ( false !== getenv( 'WP_TESTS_DIR' ) ) {
20
	// WordPress core environment variable.
21
	$_tests_dir = getenv( 'WP_TESTS_DIR' );
22
} elseif ( file_exists( dirname( dirname( $_plugin_root ) ) . '/tests/phpunit/includes/bootstrap.php' ) ) {
23
	// Installed inside wordpress-develop.
24
	$_tests_dir = dirname( dirname( $_plugin_root ) ) . '/tests/phpunit/includes/bootstrap.php';
25
} elseif ( file_exists( '/vagrant/www/wordpress-develop/public_html/tests/phpunit/includes/bootstrap.php' ) ) {
26
	// VVV.
27
	$_tests_dir = '/vagrant/www/wordpress-develop/public_html/tests/phpunit';
28
} elseif ( file_exists( '/srv/www/wordpress-trunk/public_html/tests/phpunit/includes/bootstrap.php' ) ) {
29
	// VVV 3.0.
30
	$_tests_dir = '/srv/www/wordpress-trunk/public_html/tests/phpunit';
31
} elseif ( file_exists( '/tmp/wordpress-develop/tests/phpunit/includes/bootstrap.php' ) ) {
32
	// Manual checkout & Jetpack's docker environment.
33
	$_tests_dir = '/tmp/wordpress-develop/tests/phpunit';
34
} elseif ( file_exists( rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib/includes/bootstrap.php' ) ) {
35
	// Legacy tests.
36
	$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
37
}
38
39 View Code Duplication
if ( ! isset( $_tests_dir ) || ! file_exists( $_tests_dir . '/includes/bootstrap.php' ) ) {
40
	echo 'Failed to automatically locate WordPress or wordpress-develop to run tests.' . PHP_EOL;
41
	echo PHP_EOL;
42
	echo 'Set the WP_DEVELOP_DIR environment variable to point to a copy of WordPress' . PHP_EOL;
43
	echo 'or wordpress-develop.' . PHP_EOL;
44
	exit( 1 );
45
}
46
47
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
48
echo "Using test root $_tests_dir\n";
49
50
// WordPress requires PHPUnit 7.5 or earlier and hacks around a few things to
51
// make it work with PHP 8. Unfortunately for MockObjects they do it via
52
// composer.json rather than bootstrap.php, so we have to manually do it here.
53 View Code Duplication
if ( version_compare( PHP_VERSION, '8.0', '>=' ) &&
54
	( ! class_exists( PHPUnit\Runner\Version::class ) || version_compare( PHPUnit\Runner\Version::id(), '9.3', '<' ) )
55
) {
56
	if ( ! class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) &&
57
		file_exists( "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php" )
58
	) {
59
		require "$_tests_dir/includes/phpunit7/MockObject/Builder/NamespaceMatch.php";
60
		require "$_tests_dir/includes/phpunit7/MockObject/Builder/ParametersMatch.php";
61
		require "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php";
62
		require "$_tests_dir/includes/phpunit7/MockObject/MockMethod.php";
63
	} else {
64
		fprintf(
65
			STDOUT,
66
			"Warning: PHPUnit <9.3 is not compatible with PHP 8.0+, and the hack could not be loaded.\n  Class %s exists: %s\n  File %s exists: %s\n",
67
			PHPUnit\Framework\MockObject\InvocationMocker::class,
68
			class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) ? 'yes (bad)' : 'no (good)',
69
			"$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php",
70
			file_exists( "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php" ) ? 'yes (good)' : 'no (bad)'
71
		);
72
	}
73
}
74
75
// Give access to tests_add_filter() function.
76
require_once $_tests_dir . '/includes/functions.php';
77
78
/**
79
 * Manually load the plugin being tested.
80
 */
81
function _manually_load_plugin() {
82
	global $_plugin_root;
83
	require $_plugin_root . '/plugin.php';
84
}
85
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
86
87
// Start up the WP testing environment.
88
require $_tests_dir . '/includes/bootstrap.php';
89
90
// Load Composer autoloader.
91
require $_plugin_root . '/vendor/autoload.php';
92
93
// Using the Speed Trap Listener provided by WordPress Core testing suite to expose
94
// slowest running tests. See the configuration in phpunit.xml.dist.
95
require $_tests_dir . '/includes/listener-loader.php';
96