From 67e7607ee37164c0809bca70273b47a1d6cd78fe Mon Sep 17 00:00:00 2001 From: dhruvang21 Date: Tue, 30 Jun 2026 17:21:52 +0530 Subject: [PATCH] fix: lint errors --- cleanup.php | 22 +++++---- functions.php | 126 +++++++++++++++++++++++++------------------------- prepare.php | 90 ++++++++++++++++++++---------------- report.php | 31 +++++++------ test.php | 46 +++++++++--------- 5 files changed, 166 insertions(+), 149 deletions(-) diff --git a/cleanup.php b/cleanup.php index a0d8ad0..e6fad20 100644 --- a/cleanup.php +++ b/cleanup.php @@ -2,7 +2,7 @@ /** * This script is responsible for cleaning up the test environment after a run of the WordPress PHPUnit Test Runner. * It ensures that temporary directories and files created during the test process are properly deleted. - * + * * @link https://github.com/wordpress/phpunit-test-runner/ Original source repository * @package WordPress */ @@ -27,11 +27,13 @@ * Forcefully deletes only the .git directory and the node_modules cache. * Afterward, the entire preparation directory is removed to ensure a clean state for the next test run. */ -perform_operations( array( - 'rm -rf ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/.git' ), - 'rm -rf ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/node_modules/.cache' ), - 'rm -r ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ), -) ); +perform_operations( + array( + 'rm -rf ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/.git' ), + 'rm -rf ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/node_modules/.cache' ), + 'rm -r ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ), + ) +); /** * Cleans up the test directory on a remote server. @@ -41,7 +43,9 @@ * of shell commands as its input. */ if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { - perform_operations( array( - 'ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' ' . escapeshellarg( $runner_vars['WPT_RM_TEST_DIR_CMD'] ), - ) ); + perform_operations( + array( + 'ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' ' . escapeshellarg( $runner_vars['WPT_RM_TEST_DIR_CMD'] ), + ) + ); } diff --git a/functions.php b/functions.php index 68e875a..1adc55a 100644 --- a/functions.php +++ b/functions.php @@ -26,7 +26,7 @@ function check_required_env( $check_db = true ) { 'WPT_DB_PASSWORD', 'WPT_DB_HOST', ); - foreach( $required as $var ) { + foreach ( $required as $var ) { if ( ! $check_db && 0 === strpos( $var, 'WPT_DB_' ) ) { continue; } @@ -68,29 +68,26 @@ function check_required_env( $check_db = true ) { * } */ function setup_runner_env_vars() { - // Set the test directory first as it's needed for processing other variables. + $test_dir = trim( getenv( 'WPT_TEST_DIR' ) ); + $prepare_dir = trim( getenv( 'WPT_PREPARE_DIR' ) ); + $ssh_options = trim( getenv( 'WPT_SSH_OPTIONS' ) ); + $php_exec = trim( getenv( 'WPT_PHP_EXECUTABLE' ) ); + $rm_test_dir = trim( getenv( 'WPT_RM_TEST_DIR_CMD' ) ); + $runner_configuration = array( - 'WPT_TEST_DIR' => trim( getenv( 'WPT_TEST_DIR' ) ) ?: '/tmp/wp-test-runner', + 'WPT_TEST_DIR' => '' !== $test_dir ? $test_dir : '/tmp/wp-test-runner', ); - - return array_merge( $runner_configuration, array( - // Directory configuration - 'WPT_PREPARE_DIR' => trim( getenv( 'WPT_PREPARE_DIR' ) ) ?: '/tmp/wp-test-runner', - // SSH connection configuration - 'WPT_SSH_CONNECT' => trim( getenv( 'WPT_SSH_CONNECT' ) ), - 'WPT_SSH_OPTIONS' => trim( getenv( 'WPT_SSH_OPTIONS' ) ) ?: '-o StrictHostKeyChecking=no', - // Test execution configuration - 'WPT_PHP_EXECUTABLE' => trim( getenv( 'WPT_PHP_EXECUTABLE' ) ) ?: 'php', - // Cleanup configuration - 'WPT_RM_TEST_DIR_CMD' => trim( getenv( 'WPT_RM_TEST_DIR_CMD' ) ) ?: 'rm -r ' . $runner_configuration['WPT_TEST_DIR'], - // Reporting configuration - 'WPT_REPORT_API_KEY' => trim( getenv( 'WPT_REPORT_API_KEY' ) ), - // Miscellaneous - 'WPT_DEBUG' => (bool) getenv( 'WPT_DEBUG' ), + 'WPT_PREPARE_DIR' => '' !== $prepare_dir ? $prepare_dir : '/tmp/wp-test-runner', + 'WPT_SSH_CONNECT' => trim( getenv( 'WPT_SSH_CONNECT' ) ), + 'WPT_SSH_OPTIONS' => '' !== $ssh_options ? $ssh_options : '-o StrictHostKeyChecking=no', + 'WPT_PHP_EXECUTABLE' => '' !== $php_exec ? $php_exec : 'php', + 'WPT_RM_TEST_DIR_CMD' => '' !== $rm_test_dir ? $rm_test_dir : 'rm -r ' . $runner_configuration['WPT_TEST_DIR'], + 'WPT_REPORT_API_KEY' => trim( getenv( 'WPT_REPORT_API_KEY' ) ), + 'WPT_DEBUG' => (bool) getenv( 'WPT_DEBUG' ), ) ); } @@ -114,7 +111,7 @@ function setup_runner_env_vars() { * @uses error_message() to display an error message if a shell command fails. The execution stops at the first failure. */ function perform_operations( $operations ) { - foreach( $operations as $operation ) { + foreach ( $operations as $operation ) { log_message( $operation ); passthru( $operation, $return_code ); if ( 0 !== $return_code ) { @@ -178,8 +175,8 @@ function error_message( $message ) { * @uses rtrim() to remove any existing trailing slashes from the input string before appending a new trailing slash. * This ensures that the result consistently has exactly one trailing slash, regardless of the input string's initial state. */ -function trailingslashit( $string ) { - return rtrim( $string, '/' ) . '/'; +function trailingslashit( $str ) { + return rtrim( $str, '/' ) . '/'; } /** @@ -202,16 +199,15 @@ function trailingslashit( $string ) { * @uses xpath() to query specific elements within the XML structure, particularly to find test suites with failures or errors. * @uses json_encode() to convert the array structure containing the test results into a JSON formatted string. */ -function process_junit_xml( $xml_string ) -{ +function process_junit_xml( $xml_string ) { if ( empty( $xml_string ) ) { return ''; } - $xml = simplexml_load_string( $xml_string ); + $xml = simplexml_load_string( $xml_string ); $xml_string = null; - $project = $xml->testsuite; - $results = array(); + $project = $xml->testsuite; + $results = array(); $results = array( 'tests' => (string) $project['tests'], @@ -228,7 +224,7 @@ function process_junit_xml( $xml_string ) 'name' => (string) $testsuite['name'], 'tests' => (string) $testsuite['tests'], 'failures' => (string) $testsuite['failures'], - 'errors' => (string) $testsuite['errors'] + 'errors' => (string) $testsuite['errors'], ); if ( empty( $result['failures'] ) && empty( $result['errors'] ) ) { continue; @@ -236,7 +232,7 @@ function process_junit_xml( $xml_string ) $failures = array(); foreach ( $testsuite->testcase as $testcase ) { // Capture both failure and error children. - foreach ( array( 'failure', 'error') as $key ) { + foreach ( array( 'failure', 'error' ) as $key ) { if ( isset( $testcase->{$key} ) ) { $failures[ (string) $testcase['name'] ] = array( 'name' => (string) $testcase['name'], @@ -246,7 +242,7 @@ function process_junit_xml( $xml_string ) } } if ( $failures ) { - $results['testsuites'][ (string) $testsuite['name'] ] = $result; + $results['testsuites'][ (string) $testsuite['name'] ] = $result; $results['testsuites'][ (string) $testsuite['name'] ]['testcases'] = $failures; } } @@ -277,19 +273,19 @@ function process_junit_xml( $xml_string ) * @uses base64_encode() to encode the API key for HTTP Basic Authentication in the Authorization header. */ function upload_results( $results, $rev, $message, $env, $api_key ) { - $WPT_REPORT_URL = getenv( 'WPT_REPORT_URL' ); - if ( ! $WPT_REPORT_URL ) { - $WPT_REPORT_URL = 'https://make.wordpress.org/hosting/wp-json/wp-unit-test-api/v1/results'; + $wpt_report_url = getenv( 'WPT_REPORT_URL' ); + if ( ! $wpt_report_url ) { + $wpt_report_url = 'https://make.wordpress.org/hosting/wp-json/wp-unit-test-api/v1/results'; } - $process = curl_init( $WPT_REPORT_URL ); + $process = curl_init( $wpt_report_url ); $access_token = base64_encode( $api_key ); - $data = array( + $data = array( 'results' => $results, 'commit' => $rev, 'message' => $message, 'env' => $env, ); - $data_string = json_encode( $data ); + $data_string = json_encode( $data ); curl_setopt( $process, CURLOPT_TIMEOUT, 30 ); curl_setopt( $process, CURLOPT_POST, 1 ); @@ -297,13 +293,17 @@ function upload_results( $results, $rev, $message, $env, $api_key ) { curl_setopt( $process, CURLOPT_USERAGENT, 'WordPress PHPUnit Test Runner' ); curl_setopt( $process, CURLOPT_POSTFIELDS, $data_string ); curl_setopt( $process, CURLOPT_RETURNTRANSFER, true ); - curl_setopt( $process, CURLOPT_HTTPHEADER, array( - "Authorization: Basic $access_token", - 'Content-Type: application/json', - 'Content-Length: ' . strlen( $data_string ) - )); + curl_setopt( + $process, + CURLOPT_HTTPHEADER, + array( + "Authorization: Basic $access_token", + 'Content-Type: application/json', + 'Content-Length: ' . strlen( $data_string ), + ) + ); - $return = curl_exec( $process ); + $return = curl_exec( $process ); $status_code = curl_getinfo( $process, CURLINFO_HTTP_CODE ); curl_close( $process ); @@ -335,23 +335,23 @@ function upload_results( $results, $rev, $message, $env, $api_key ) { function get_env_details() { $gd_info = array(); - if( extension_loaded( 'gd' ) ) { + if ( extension_loaded( 'gd' ) ) { $gd_info = gd_info(); } $imagick_info = array(); - if( extension_loaded( 'imagick' ) ) { + if ( extension_loaded( 'imagick' ) ) { $imagick_info = Imagick::queryFormats(); } $env = array( - 'php_version' => phpversion(), - 'php_modules' => array(), - 'gd_info' => $gd_info, - 'imagick_info' => $imagick_info, - 'mysql_version' => trim( shell_exec( 'mysql --version' ) ), - 'system_utils' => array(), - 'os_name' => trim( shell_exec( 'uname -s' ) ), - 'os_version' => trim( shell_exec( 'uname -r' ) ), + 'php_version' => phpversion(), + 'php_modules' => array(), + 'gd_info' => $gd_info, + 'imagick_info' => $imagick_info, + 'mysql_version' => trim( shell_exec( 'mysql --version' ) ), + 'system_utils' => array(), + 'os_name' => trim( shell_exec( 'uname -s' ) ), + 'os_version' => trim( shell_exec( 'uname -r' ) ), ); unset( $gd_info, $imagick_info ); @@ -392,23 +392,25 @@ function get_env_details() { 'zip', 'zlib', ); - foreach( $php_modules as $php_module ) { + foreach ( $php_modules as $php_module ) { $env['php_modules'][ $php_module ] = phpversion( $php_module ); } - function curl_selected_bits($k) { return in_array($k, array('version', 'ssl_version', 'libz_version')); } - $curl_bits = curl_version(); - $env['system_utils']['curl'] = implode(' ',array_values(array_filter($curl_bits, 'curl_selected_bits',ARRAY_FILTER_USE_KEY) )); + function curl_selected_bits( $k ) { + return in_array( $k, array( 'version', 'ssl_version', 'libz_version' ), true ); + } + $curl_bits = curl_version(); + $env['system_utils']['curl'] = implode( ' ', array_values( array_filter( $curl_bits, 'curl_selected_bits', ARRAY_FILTER_USE_KEY ) ) ); - $WPT_DB_HOST = trim( getenv( 'WPT_DB_HOST' ) ); - if( ! $WPT_DB_HOST ) { - $WPT_DB_HOST = 'localhost'; + $wpt_db_host = trim( getenv( 'WPT_DB_HOST' ) ); + if ( ! $wpt_db_host ) { + $wpt_db_host = 'localhost'; } - $WPT_DB_USER = trim( getenv( 'WPT_DB_USER' ) ); - $WPT_DB_PASSWORD = trim( getenv( 'WPT_DB_PASSWORD' ) ); - $WPT_DB_NAME = trim( getenv( 'WPT_DB_NAME' ) ); + $wpt_db_user = trim( getenv( 'WPT_DB_USER' ) ); + $wpt_db_password = trim( getenv( 'WPT_DB_PASSWORD' ) ); + $wpt_db_name = trim( getenv( 'WPT_DB_NAME' ) ); - //$mysqli = new mysqli( $WPT_DB_HOST, $WPT_DB_USER, $WPT_DB_PASSWORD, $WPT_DB_NAME ); + //$mysqli = new mysqli( $wpt_db_host, $wpt_db_user, $wpt_db_password, $wpt_db_name ); //$env['mysql_version'] = $mysqli->query("SELECT VERSION()")->fetch_row()[0]; //$mysqli->close(); diff --git a/prepare.php b/prepare.php index b58d7d6..b87d428 100644 --- a/prepare.php +++ b/prepare.php @@ -33,10 +33,9 @@ * @throws Exception If there is an issue creating the .ssh directory or writing the key file. */ // Set the SSH private key if it's provided in the environment. -$WPT_SSH_PRIVATE_KEY_BASE64 = trim( getenv( 'WPT_SSH_PRIVATE_KEY_BASE64' ) ); +$wpt_ssh_private_key_base64 = trim( getenv( 'WPT_SSH_PRIVATE_KEY_BASE64' ) ); -// Check if the private key variable is not empty. -if ( ! empty( $WPT_SSH_PRIVATE_KEY_BASE64 ) ) { +if ( ! empty( $wpt_ssh_private_key_base64 ) ) { // Log the action of securely extracting the private key. log_message( 'Securely extracting WPT_SSH_PRIVATE_KEY_BASE64 into ~/.ssh/id_rsa' ); @@ -48,24 +47,27 @@ } // Write the decoded private key into the id_rsa file within the .ssh directory. - file_put_contents( getenv( 'HOME' ) . '/.ssh/id_rsa', base64_decode( $WPT_SSH_PRIVATE_KEY_BASE64 ) ); + file_put_contents( getenv( 'HOME' ) . '/.ssh/id_rsa', base64_decode( $wpt_ssh_private_key_base64 ) ); // Define the array of operations to perform, depending on the SSH connection availability. // If no SSH connection string is provided, add a local operation to the array. // If an SSH connection string is provided, add a remote operation to the array. // Execute the operations defined in the operations array. - if( empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { - perform_operations( array( - 'chmod 600 ~/.ssh/id_rsa', - 'wp cli info' - ) ); + if ( empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { + perform_operations( + array( + 'chmod 600 ~/.ssh/id_rsa', + 'wp cli info', + ) + ); } else { - perform_operations( array( - 'chmod 600 ~/.ssh/id_rsa', - 'ssh -q ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' wp cli info' - ) ); + perform_operations( + array( + 'chmod 600 ~/.ssh/id_rsa', + 'ssh -q ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' wp cli info', + ) + ); } - } @@ -74,26 +76,28 @@ * cloning the WordPress development repository, and preparing the environment with npm. */ // Prepare an array of shell commands to set up the testing environment. -perform_operations( array( +perform_operations( + array( - // Create the preparation directory if it doesn't exist. The '-p' flag creates intermediate directories as required. - 'mkdir -p ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ), + // Create the preparation directory if it doesn't exist. The '-p' flag creates intermediate directories as required. + 'mkdir -p ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ), - // Clone the WordPress develop repository from GitHub into the preparation directory. - // The '--depth=1' flag creates a shallow clone with a history truncated to the last commit. - 'git clone --depth=1 https://github.com/WordPress/wordpress-develop.git ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ), + // Clone the WordPress develop repository from GitHub into the preparation directory. + // The '--depth=1' flag creates a shallow clone with a history truncated to the last commit. + 'git clone --depth=1 https://github.com/WordPress/wordpress-develop.git ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ), - // Change directory to the preparation directory, install npm dependencies, and build the project. - 'cd ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '; npm install && npm run build' + // Change directory to the preparation directory, install npm dependencies, and build the project. + 'cd ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '; npm install && npm run build', -) ); + ) +); // Log a message indicating the start of the variable replacement process for configuration. log_message( 'Replacing variables in wp-tests-config.php' ); /** * Reads the contents of the WordPress test configuration sample file. - * This file contains template placeholders that need to be replaced with actual values + * This file contains template placeholders that need to be replaced with actual values * from environment variables to configure the WordPress test environment. */ $contents = file_get_contents( $runner_vars['WPT_PREPARE_DIR'] . '/wp-tests-config-sample.php' ); @@ -104,7 +108,7 @@ * It then collects various pieces of system information including PHP version, loaded PHP modules, * MySQL version, operating system details, and versions of key utilities like cURL and OpenSSL. * This information is collected in an array and written to a JSON file in the log directory. - * Additionally, if running from the command line during a WordPress installation process, + * Additionally, if running from the command line during a WordPress installation process, * it outputs the PHP version and executable path. */ $system_logger = << trim( getenv( 'WPT_TABLE_PREFIX' ) ) ? : 'wptests_', +$wpt_table_prefix = trim( getenv( 'WPT_TABLE_PREFIX' ) ); +$search_replace = array( + 'wptests_' => '' !== $wpt_table_prefix ? $wpt_table_prefix : 'wptests_', 'youremptytestdbnamehere' => trim( getenv( 'WPT_DB_NAME' ) ), 'yourusernamehere' => trim( getenv( 'WPT_DB_USER' ) ), 'yourpasswordhere' => trim( getenv( 'WPT_DB_PASSWORD' ) ), @@ -234,7 +239,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve $php_version_cmd = $runner_vars['WPT_PHP_EXECUTABLE'] . " -r \"print PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION;\""; /** - * If an SSH connection string is provided, the command to determine the PHP version is modified + * If an SSH connection string is provided, the command to determine the PHP version is modified * to execute remotely over SSH. This is required if the test environment is not the local machine. */ if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { @@ -253,8 +258,7 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve $env_php_version = exec( $php_version_cmd, $output, $retval ); // Check if the command execution was successful by inspecting the return value. -if ( $retval !== 0 ) { - // If the return value is not zero, an error occurred, and a message is logged. +if ( 0 !== $retval ) { error_message( 'Could not retrieve the environment PHP Version.' ); } @@ -277,11 +281,11 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve */ // Check if Composer is installed and available in the PATH. -$composer_cmd = 'cd ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . ' && '; -$retval = 0; +$composer_cmd = 'cd ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . ' && '; +$retval = 0; $composer_path = escapeshellarg( system( 'which composer', $retval ) ); -if ( $retval === 0 ) { +if ( 0 === $retval ) { // If Composer is available, prepare the command to use the Composer binary. $composer_cmd .= $composer_path . ' '; @@ -291,19 +295,23 @@ function curl_selected_bits(\$k) { return in_array(\$k, array('version', 'ssl_ve // If Composer is not available, download the Composer phar file. log_message( 'Local Composer not found. Downloading latest stable ...' ); - perform_operations( array( - 'wget -O ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/composer.phar' ) . ' https://getcomposer.org/composer-stable.phar', - ) ); + perform_operations( + array( + 'wget -O ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] . '/composer.phar' ) . ' https://getcomposer.org/composer-stable.phar', + ) + ); // Update the command to use the downloaded Composer phar file. $composer_cmd .= $runner_vars['WPT_PHP_EXECUTABLE'] . ' composer.phar '; } // Set the PHP version for Composer to ensure compatibility and update dependencies. -perform_operations( array( - $composer_cmd . 'config platform.php ' . escapeshellarg( $env_php_version ), - $composer_cmd . 'update', -) ); +perform_operations( + array( + $composer_cmd . 'config platform.php ' . escapeshellarg( $env_php_version ), + $composer_cmd . 'update', + ) +); /** * If an SSH connection is configured, use rsync to transfer the prepared files to the remote test environment. diff --git a/report.php b/report.php index 4265db0..7a5a36d 100644 --- a/report.php +++ b/report.php @@ -29,8 +29,8 @@ * WPT_PREPARE_DIR environment variable, retrieves the latest commit message, * and extracts the SVN revision number using a combination of grep and cut commands. */ -log_message('Getting SVN Revision'); -$rev = exec('git --git-dir=' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '/.git log -1 --pretty=%B | grep "git-svn-id:" | cut -d " " -f 2 | cut -d "@" -f 2'); +log_message( 'Getting SVN Revision' ); +$rev = exec( 'git --git-dir=' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '/.git log -1 --pretty=%B | grep "git-svn-id:" | cut -d " " -f 2 | cut -d "@" -f 2' ); /** * Retrieves the latest SVN commit message from the git repository log. @@ -38,8 +38,8 @@ * that accesses the git directory specified by the WPT_PREPARE_DIR environment variable, * fetches the latest commit message, and trims any whitespace from the message. */ -log_message('Getting SVN message'); -$message = trim( exec('git --git-dir=' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '/.git log -1 --pretty=%B | head -1') ); +log_message( 'Getting SVN message' ); +$message = trim( exec( 'git --git-dir=' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '/.git log -1 --pretty=%B | head -1' ) ); /** * Prepares the file path for copying the junit.xml results. @@ -48,7 +48,7 @@ * making use of the WPT_TEST_DIR environment variable. The path is sanitized to be * safely used in shell commands. */ -log_message('Copying junit.xml results'); +log_message( 'Copying junit.xml results' ); $junit_location = escapeshellarg( $runner_vars['WPT_TEST_DIR'] ) . '/tests/phpunit/build/logs/*'; /** * Modifies the junit.xml results file path for a remote location if an SSH connection is available. @@ -81,9 +81,11 @@ * the junit.xml files from the source to the destination directory. */ $junit_exec = 'rsync ' . $rsync_options . ' ' . $junit_location . ' ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ); -perform_operations( array( - $junit_exec, -) ); +perform_operations( + array( + $junit_exec, + ) +); /** * Processes and uploads the junit.xml file. @@ -93,7 +95,7 @@ * it for upload or to extract relevant test run information. */ log_message( 'Processing and uploading junit.xml' ); -$xml = file_get_contents( $runner_vars['WPT_PREPARE_DIR'] . '/junit.xml' ); +$xml = file_get_contents( $runner_vars['WPT_PREPARE_DIR'] . '/junit.xml' ); $results = process_junit_xml( $xml ); /** @@ -118,34 +120,33 @@ * message is logged along with the HTTP status. If no API key is provided, it logs the test results * and environment details locally. */ -if( ! empty( $runner_vars['WPT_REPORT_API_KEY'] ) ) { +if ( ! empty( $runner_vars['WPT_REPORT_API_KEY'] ) ) { // Upload the results and capture the HTTP status and response body list( $http_status, $response_body ) = upload_results( $results, $rev, $message, $env, $runner_vars['WPT_REPORT_API_KEY'] ); // Decode the JSON response body $response = json_decode( $response_body, true ); - if ( 20 == substr( $http_status, 0, 2 ) ) { + if ( 20 == substr( $http_status, 0, 2 ) ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual // Construct and log a success message with a link if provided in the response - $message = 'Results successfully uploaded'; + $message = 'Results successfully uploaded'; $message .= isset( $response['link'] ) ? ': ' . $response['link'] : ''; log_message( $message ); } else { // Construct and log an error message with additional details if provided in the response - $message = 'Error uploading results'; + $message = 'Error uploading results'; $message .= isset( $response['message'] ) ? ': ' . $response['message'] : ''; $message .= ' (HTTP status ' . (int) $http_status . ')'; error_message( $message ); } - } else { // Log the test results and environment details locally if no API key is provided - log_message( '[+] TEST RESULTS' . "\n\n" . $results. "\n\n" ); + log_message( '[+] TEST RESULTS' . "\n\n" . $results . "\n\n" ); log_message( '[+] ENVIRONMENT' . "\n\n" . $env . "\n\n" ); } diff --git a/test.php b/test.php index 67cf058..f5bd251 100644 --- a/test.php +++ b/test.php @@ -3,7 +3,7 @@ * Executes the PHPUnit test suite within the WordPress testing environment. * This script is designed to run tests either locally or on a remote server based on the environment setup. * It dynamically constructs the command to run PHPUnit and then executes it. - * + * * @link https://github.com/wordpress/phpunit-test-runner/ Original source repository * @package WordPress */ @@ -22,40 +22,40 @@ $runner_vars = setup_runner_env_vars(); // Uses the flavor (usually to test WordPress Multisite) -$WPT_FLAVOR_INI = trim( getenv( 'WPT_FLAVOR' ) ); -switch( $WPT_FLAVOR_INI ) { +$wpt_flavor_ini = trim( getenv( 'WPT_FLAVOR' ) ); +switch ( $wpt_flavor_ini ) { case 0: - $WPT_FLAVOR_TXT = ''; // Simple WordPress + $wpt_flavor_txt = ''; // Simple WordPress break; case 1: - $WPT_FLAVOR_TXT = ' -c tests/phpunit/multisite.xml'; // WordPress Multisite + $wpt_flavor_txt = ' -c tests/phpunit/multisite.xml'; // WordPress Multisite break; default: - $WPT_FLAVOR_TXT = ''; + $wpt_flavor_txt = ''; break; } -unset( $WPT_FLAVOR_INI ); +unset( $wpt_flavor_ini ); // Uses the extra tests group (e.g., ajax, ms-files, external-http) -$WPT_EXTRATESTS_INI = trim( getenv( 'WPT_EXTRATESTS' ) ); -switch( $WPT_EXTRATESTS_INI ) { +$wpt_extratests_ini = trim( getenv( 'WPT_EXTRATESTS' ) ); +switch ( $wpt_extratests_ini ) { case 0: - $WPT_EXTRATESTS_TXT = ''; // no extra tests + $wpt_extratests_txt = ''; // no extra tests break; case 1: - $WPT_EXTRATESTS_TXT = ' --group ajax'; // ajax tests + $wpt_extratests_txt = ' --group ajax'; // ajax tests break; case 2: - $WPT_EXTRATESTS_TXT = ' --group ms-files'; // ms-files tests + $wpt_extratests_txt = ' --group ms-files'; // ms-files tests break; case 3: - $WPT_EXTRATESTS_TXT = ' --group external-http'; // external-http tests + $wpt_extratests_txt = ' --group external-http'; // external-http tests break; default: - $WPT_EXTRATESTS_TXT = ''; + $wpt_extratests_txt = ''; break; } -unset( $WPT_EXTRATESTS_INI ); +unset( $wpt_extratests_ini ); /** * Determines the PHPUnit command to execute the test suite. @@ -64,17 +64,19 @@ * the test directory path from environment variables, appending parameters to the PHPUnit call to * avoid reporting useless tests. */ -$WPT_PHPUNIT_CMD = trim( getenv( 'WPT_PHPUNIT_CMD' ) ); -if( empty( $WPT_PHPUNIT_CMD ) ) { - $WPT_PHPUNIT_CMD = 'cd ' . escapeshellarg( $runner_vars['WPT_TEST_DIR'] ) . ' && ' . $runner_vars['WPT_PHP_EXECUTABLE'] . ' ./vendor/phpunit/phpunit/phpunit --dont-report-useless-tests' . $WPT_FLAVOR_TXT . $WPT_EXTRATESTS_TXT; +$wpt_phpunit_cmd = trim( getenv( 'WPT_PHPUNIT_CMD' ) ); +if ( empty( $wpt_phpunit_cmd ) ) { + $wpt_phpunit_cmd = 'cd ' . escapeshellarg( $runner_vars['WPT_TEST_DIR'] ) . ' && ' . $runner_vars['WPT_PHP_EXECUTABLE'] . ' ./vendor/phpunit/phpunit/phpunit --dont-report-useless-tests' . $wpt_flavor_txt . $wpt_extratests_txt; } // If an SSH connection string is provided, prepend the SSH command to the PHPUnit execution command. if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) { - $WPT_PHPUNIT_CMD = 'ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' ' . escapeshellarg( $WPT_PHPUNIT_CMD ); + $wpt_phpunit_cmd = 'ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . ' ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] ) . ' ' . escapeshellarg( $wpt_phpunit_cmd ); } // Execute the PHPUnit command. -perform_operations( array( - $WPT_PHPUNIT_CMD -) ); +perform_operations( + array( + $wpt_phpunit_cmd, + ) +);