Skip to content

Commit 8fdff5c

Browse files
authored
Install and use PHPUnit via composer for improved PHP Support. (#132)
Installs appropriate PHPUnit based on the test environment's PHP version: - Uses the same method of manual PHAR download for PHP < 7.1 - Uses PHPUnit installed via Composer for PHP 7.1+, with special handling for PHP 8 due to core support. - Downloads Composer PHAR to prepare directory when it is required and not available locally.
1 parent 03ce121 commit 8fdff5c

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

prepare.php

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
perform_operations( array(
3333
'mkdir -p ' . escapeshellarg( $WPT_PREPARE_DIR ),
3434
'git clone --depth=1 https://github.com/WordPress/wordpress-develop.git ' . escapeshellarg( $WPT_PREPARE_DIR ),
35-
'wget -O ' . escapeshellarg( $WPT_PREPARE_DIR . '/phpunit.phar' ) . ' https://phar.phpunit.de/phpunit-5.7.phar',
3635
'wget -O ' . escapeshellarg( $WPT_PREPARE_DIR . '/tests/phpunit/data/plugins/wordpress-importer.zip' ) . ' https://downloads.wordpress.org/plugin/wordpress-importer.zip',
3736
'cd ' . escapeshellarg( $WPT_PREPARE_DIR . '/tests/phpunit/data/plugins/' ) . '; unzip wordpress-importer.zip; rm wordpress-importer.zip',
3837
'cd ' . escapeshellarg( $WPT_PREPARE_DIR ) . '; npm install && npm run build',
@@ -104,6 +103,62 @@
104103
$contents = str_replace( array_keys( $search_replace ), array_values( $search_replace ), $contents );
105104
file_put_contents( $WPT_PREPARE_DIR . '/wp-tests-config.php', $contents );
106105

106+
// Now, install PHPUnit based on the test environment's PHP Version
107+
$php_version_cmd = $WPT_PHP_EXECUTABLE . " -r \"print PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION;\"";
108+
if ( ! empty( $WPT_SSH_CONNECT ) ) {
109+
$php_version_cmd = 'ssh ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' ' . escapeshellarg( $php_version_cmd );
110+
}
111+
112+
$retval = 0;
113+
$env_php_version = exec( $php_version_cmd, $output, $retval );
114+
if ( $retval !== 0 ) {
115+
error_message( 'Could not retrieve the environment PHP Version.' );
116+
}
117+
log_message( "Environment PHP Version: $env_php_version" );
118+
119+
// If PHP Version is 8.X.X, set PHP Version to 7.4 for compatibility with core PHPUnit tests.
120+
if ( substr( $env_php_version, 0 , 2 ) === '8.' ) {
121+
log_message( 'Version 8.x.x Found. Downloading PHPUnit for PHP 7.4 instead for compatibility.' );
122+
$env_php_version = '7.4';
123+
}
124+
125+
if ( version_compare( $env_php_version, '5.6', '<' ) ) {
126+
error_message( "The test runner is not compatible with PHP < 5.6." );
127+
}
128+
129+
// If PHP version is 5.6-7.0, download PHPUnit 5.7 phar directly.
130+
if ( version_compare( $env_php_version, '7.1', '<' ) ) {
131+
perform_operations( array(
132+
'wget -O ' . escapeshellarg( $WPT_PREPARE_DIR . '/phpunit.phar' ) . ' https://phar.phpunit.de/phpunit-5.7.phar',
133+
) );
134+
135+
// Otherwise, use Composer to download PHPUnit to get further necessary dependencies.
136+
} else {
137+
138+
// First, check if composer is available. Download if not.
139+
$composer_cmd = 'cd ' . escapeshellarg( $WPT_PREPARE_DIR ) . ' && ';
140+
141+
$retval = 0;
142+
$composer_path = escapeshellarg( system( 'which composer', $retval ) );
143+
if ( $retval === 0 ) {
144+
$composer_cmd .= $composer_path . ' ';
145+
} else {
146+
log_message( 'Local Composer not found. Downloading latest stable ...' );
147+
148+
perform_operations( array(
149+
'wget -O ' . escapeshellarg( $WPT_PREPARE_DIR . '/composer.phar' ) . ' https://getcomposer.org/composer-stable.phar',
150+
) );
151+
152+
$composer_cmd .= 'php composer.phar ';
153+
}
154+
155+
// Set Composer PHP environment, then run Composer.
156+
perform_operations( array(
157+
$composer_cmd . 'config platform.php ' . escapeshellarg( $env_php_version ),
158+
$composer_cmd . 'update',
159+
) );
160+
}
161+
107162
// Deliver all files to test environment.
108163
if ( ! empty( $WPT_SSH_CONNECT ) ) {
109164
$rsync_options = '-r';
@@ -113,7 +168,7 @@
113168
}
114169

115170
perform_operations( array(
116-
'rsync ' . $rsync_options . ' --exclude=".git/" --exclude="node_modules/" -e "ssh ' . $WPT_SSH_OPTIONS . '" ' . escapeshellarg( trailingslashit( $WPT_PREPARE_DIR ) ) . ' ' . escapeshellarg( $WPT_SSH_CONNECT . ':' . $WPT_TEST_DIR ),
171+
'rsync ' . $rsync_options . ' --exclude=".git/" --exclude="node_modules/" --exclude="composer.phar" -e "ssh ' . $WPT_SSH_OPTIONS . '" ' . escapeshellarg( trailingslashit( $WPT_PREPARE_DIR ) ) . ' ' . escapeshellarg( $WPT_SSH_CONNECT . ':' . $WPT_TEST_DIR ),
117172
) );
118173
}
119174

test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
$WPT_TEST_DIR = getenv( 'WPT_TEST_DIR' );
1414
$WPT_SSH_OPTIONS = getenv( 'WPT_SSH_OPTIONS' ) ? : '-o StrictHostKeyChecking=no';
1515
$WPT_PHP_EXECUTABLE = getenv( 'WPT_PHP_EXECUTABLE' ) ? : 'php';
16-
$WPT_PHPUNIT_CMD = getenv( 'WPT_PHPUNIT_CMD' ) ? : 'cd ' . escapeshellarg( $WPT_TEST_DIR ) . '; ' . $WPT_PHP_EXECUTABLE . ' phpunit.phar';
16+
17+
// This uses `||` to run PHPUnit when it is downloaded manually (like for PHP 5.6-7.0) rather than through Composer.
18+
$WPT_PHPUNIT_CMD = getenv( 'WPT_PHPUNIT_CMD' ) ? : 'cd ' . escapeshellarg( $WPT_TEST_DIR ) . ' && ' . $WPT_PHP_EXECUTABLE . ' ./vendor/phpunit/phpunit/phpunit || ' . $WPT_PHP_EXECUTABLE . ' phpunit.phar';
1719

1820
// Run phpunit in the test environment.
1921
if ( ! empty( $WPT_SSH_CONNECT ) ) {

0 commit comments

Comments
 (0)