1
1
<?php
2
-
3
- /**
4
- * Utility functions for the test runner.
5
- */
6
-
7
2
/**
8
- * Check for required environment variables.
3
+ * Validates the presence of essential environment variables necessary for the application to run correctly.
4
+ * Specifically checks for variables related to directories and database configuration. It also ensures that
5
+ * the test and preparation directories are the same when running locally without SSH connection requirements.
6
+ *
7
+ * This function will issue error messages through `error_message()` for any missing environment variables
8
+ * and logs a message upon successful validation of all required variables.
9
+ *
10
+ * @param bool $check_db Optional. Whether to include database-related environment variables in the check. Defaults to true.
11
+ * If set to false, database variables (prefixed with 'WPT_DB_') are not checked.
12
+ *
13
+ * @return void This function does not return a value but will halt execution if any required environment variable is missing.
14
+ *
15
+ * @uses getenv() to retrieve environment variable values.
16
+ * @uses error_message() to display error messages for missing variables.
17
+ * @uses log_message() to log a success message when all checks pass.
9
18
*/
10
19
function check_required_env ( $ check_db = true ) {
11
20
@@ -35,9 +44,22 @@ function check_required_env( $check_db = true ) {
35
44
}
36
45
37
46
/**
38
- * Perform some number of shell operations
47
+ * Executes a series of shell commands provided in the operations array. Each operation is logged before execution.
48
+ * If any command fails (indicated by a non-zero return code), an error message is displayed. This function is
49
+ * useful for automating batch shell tasks within a PHP script, with error handling for each operation.
50
+ *
51
+ * @param array $operations An array of shell commands (strings) to be executed. Each command should be
52
+ * a valid shell command and properly escaped for safety. The commands are executed
53
+ * in the order they appear in the array.
39
54
*
40
- * @param array $operations
55
+ * @return void This function does not return a value. However, it will output the result of each shell command
56
+ * to the standard output and log the execution. It will also halt on the first command that fails,
57
+ * displaying an error message.
58
+ *
59
+ * @uses log_message() to log each operation before execution. This can be used for debugging or auditing purposes.
60
+ * @uses passthru() to execute the shell command, which provides direct output to the browser. Be aware that using
61
+ * this function with untrusted input can lead to security vulnerabilities, such as command injection attacks.
62
+ * @uses error_message() to display an error message if a shell command fails. The execution stops at the first failure.
41
63
*/
42
64
function perform_operations ( $ operations ) {
43
65
foreach ( $ operations as $ operation ) {
@@ -50,40 +72,83 @@ function perform_operations( $operations ) {
50
72
}
51
73
52
74
/**
53
- * Log a message to STDOUT
75
+ * Writes a message followed by a newline to the standard output (STDOUT). This function is commonly used for logging purposes,
76
+ * providing feedback during script execution, or debugging. The message is appended with the PHP end-of-line constant (PHP_EOL)
77
+ * to ensure proper line breaks on different operating systems.
78
+ *
79
+ * @param string $message The message to be logged. This should be a string, and it will be output exactly as provided,
80
+ * followed by a system-specific newline character.
81
+ *
82
+ * @return void This function does not return a value. It directly writes the message to STDOUT, which is typically
83
+ * visible in the console or terminal where the PHP script is executed.
54
84
*
55
- * @param string $message
85
+ * @uses fwrite() to write the message to STDOUT. This is a low-level file operation function that works with various
86
+ * file streams, including standard output, standard error, and regular files.
56
87
*/
57
88
function log_message ( $ message ) {
58
89
fwrite ( STDOUT , $ message . PHP_EOL );
59
90
}
60
91
61
92
/**
62
- * Log an error message to STDERR
93
+ * Writes an error message prefixed with "Error: " to the standard error output (STDERR) and terminates the script
94
+ * with a status code of 1. This function is typically used to report errors during script execution, where an
95
+ * immediate halt is necessary due to unrecoverable conditions. The message is appended with the PHP end-of-line
96
+ * constant (PHP_EOL) to ensure it is properly displayed on all operating systems.
63
97
*
64
- * @param string $message
98
+ * @param string $message The error message to be logged. This string will be output as provided, but prefixed
99
+ * with "Error: " to indicate its nature, followed by a system-specific newline character.
100
+ *
101
+ * @return void This function does not return a value. It directly writes the error message to STDERR and then
102
+ * terminates the script execution using `exit(1)`, indicating an error condition to the environment.
103
+ *
104
+ * @uses fwrite() to write the error message to STDERR. This function is used for low-level writing to file streams
105
+ * or output streams, in this case, STDERR, which is specifically for error reporting.
106
+ * @uses exit() to terminate the script execution with a status code of 1, indicating an error has occurred. This is
107
+ * a common practice in command-line scripts and applications to signal failure to the calling process or environment.
65
108
*/
66
109
function error_message ( $ message ) {
67
110
fwrite ( STDERR , 'Error: ' . $ message . PHP_EOL );
68
111
exit ( 1 );
69
112
}
70
113
71
114
/**
72
- * Add a trailing slash to the string
115
+ * Ensures a single trailing slash is present at the end of a given string. This function first removes any existing
116
+ * trailing slashes from the input string to avoid duplication and then appends a single slash. It's commonly used
117
+ * to normalize file paths or URLs to ensure consistency in format, especially when concatenating paths or performing
118
+ * file system operations that expect a trailing slash.
119
+ *
120
+ * @param string $string The input string to which a trailing slash will be added. This could be a file path, URL,
121
+ * or any other string that requires a trailing slash for proper formatting or usage.
73
122
*
74
- * @param string
75
- * @return string
123
+ * @return string The modified string with a single trailing slash appended at the end. If the input string already
124
+ * has one or more trailing slashes, they will be trimmed to a single slash.
125
+ *
126
+ * @uses rtrim() to remove any existing trailing slashes from the input string before appending a new trailing slash.
127
+ * This ensures that the result consistently has exactly one trailing slash, regardless of the input string's initial state.
76
128
*/
77
129
function trailingslashit ( $ string ) {
78
130
return rtrim ( $ string , '/ ' ) . '/ ' ;
79
131
}
80
132
81
133
/**
82
- * Process JUnit test results and return JSON. The resulting JSON will only
83
- * include failures.
134
+ * Parses JUnit XML formatted string to extract test results, focusing specifically on test failures and errors.
135
+ * The function converts the XML data into a structured JSON format that summarizes the overall test outcomes,
136
+ * including the total number of tests, failures, errors, and execution time. Only test suites and cases that
137
+ * contain failures or errors are included in the final JSON output. This function is useful for automated test
138
+ * result analysis, continuous integration reporting, or any scenario where a quick summary of test failures and
139
+ * errors is needed.
140
+ *
141
+ * @param string $xml_string The JUnit XML data as a string. This should be well-formed XML representing the results
142
+ * of test executions, typically generated by testing frameworks compatible with JUnit reporting.
143
+ *
144
+ * @return string A JSON encoded string that represents a summary of the test results, including overall metrics and
145
+ * detailed information about each failed or errored test case. The JSON structure will include keys
146
+ * for 'tests', 'failures', 'errors', 'time', and 'testsuites', where 'testsuites' is an array of test
147
+ * suites that contains the failures or errors.
84
148
*
85
- * @param string $xml String containing JUnit results.
86
- * @return string
149
+ * @uses simplexml_load_string() to parse the JUnit XML data into an object for easy access and manipulation of the XML elements.
150
+ * @uses xpath() to query specific elements within the XML structure, particularly to find test suites with failures or errors.
151
+ * @uses json_encode() to convert the array structure containing the test results into a JSON formatted string.
87
152
*/
88
153
function process_junit_xml ( $ xml_string )
89
154
{
@@ -138,14 +203,26 @@ function process_junit_xml( $xml_string )
138
203
}
139
204
140
205
/**
141
- * Upload the results to the reporting API.
142
- *
143
- * @param string $content The processed JUnit XML.
144
- * @param string $rev The SVN revision.
145
- * @param string $message The SVN message.
146
- * @param string $env The environment data in JSON format
147
- * @param string $api_key The API key for the reporting API.
148
- * @return array Response from the reporting API.
206
+ * Submits test results along with associated metadata to a specified reporting API. The function constructs
207
+ * a POST request containing the test results, SVN revision, SVN message, environment data, and uses an API key
208
+ * for authentication. The reporting API's URL is retrieved from an environment variable; if not found, a default
209
+ * URL is used. This function is typically used to automate the reporting of test outcomes to a centralized system
210
+ * for analysis, tracking, and historical record-keeping.
211
+ *
212
+ * @param string $results The test results in a processed format (e.g., JSON) ready for submission to the reporting API.
213
+ * @param string $rev The SVN revision associated with the test results. This often corresponds to a specific code
214
+ * commit or build identifier.
215
+ * @param string $message The SVN commit message associated with the revision, providing context or notes about the changes.
216
+ * @param string $env The environment data in JSON format, detailing the conditions under which the tests were run,
217
+ * such as operating system, PHP version, etc.
218
+ * @param string $api_key The API key for authenticating with the reporting API, ensuring secure and authorized access.
219
+ *
220
+ * @return array An array containing two elements: the HTTP status code of the response (int) and the body of the response
221
+ * (string) from the reporting API. This can be used to verify successful submission or to handle errors.
222
+ *
223
+ * @uses curl_init(), curl_setopt(), and curl_exec() to perform the HTTP POST request to the reporting API.
224
+ * @uses json_encode() to encode the data payload as a JSON string for submission.
225
+ * @uses base64_encode() to encode the API key for HTTP Basic Authentication in the Authorization header.
149
226
*/
150
227
function upload_results ( $ results , $ rev , $ message , $ env , $ api_key ) {
151
228
$ WPT_REPORT_URL = getenv ( 'WPT_REPORT_URL ' );
@@ -181,7 +258,26 @@ function upload_results( $results, $rev, $message, $env, $api_key ) {
181
258
}
182
259
183
260
/**
184
- * Get the environmental details
261
+ * Collects and returns an array of key environment details relevant to the application's context. This includes
262
+ * the PHP version, installed PHP modules with their versions, system utilities like curl and OpenSSL versions,
263
+ * MySQL version, and operating system details. This function is useful for diagnostic purposes, ensuring
264
+ * compatibility, or for reporting system configurations in debugging or error logs.
265
+ *
266
+ * The function checks for the availability of specific PHP modules and system utilities and captures their versions.
267
+ * It uses shell commands to retrieve system information, which requires the PHP environment to have access to these
268
+ * commands and appropriate permissions.
269
+ *
270
+ * @return array An associative array containing detailed environment information. The array includes:
271
+ * - 'php_version': The current PHP version.
272
+ * - 'php_modules': An associative array of selected PHP modules and their versions.
273
+ * - 'system_utils': Versions of certain system utilities such as 'curl', 'imagemagick', 'graphicsmagick', and 'openssl'.
274
+ * - 'mysql_version': The version of MySQL installed.
275
+ * - 'os_name': The name of the operating system.
276
+ * - 'os_version': The version of the operating system.
277
+ *
278
+ * @uses phpversion() to get the PHP version and module versions.
279
+ * @uses shell_exec() to execute system commands for retrieving MySQL version, OS details, and versions of utilities like curl and OpenSSL.
280
+ * @uses class_exists() to check for the availability of the Imagick and Gmagick classes for version detection.
185
281
*/
186
282
function get_env_details () {
187
283
$ env = array (
0 commit comments