Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2/**
3 * A shim to select a config file based on the PHPUnit version installed.
4 *
5 * @package automattic/phpunit-select-config
6 */
7
8// Make sure this script is being run over the PHP CLI.
9if ( 'cli' !== php_sapi_name() && 'phpdbg' !== php_sapi_name() ) {
10    throw new RuntimeException( 'This file must be run from the command line.' );
11}
12
13if ( ! isset( $argv ) || ! is_array( $argv ) ) {
14    if ( ! isset( $_SERVER['argv'] ) || ! is_array( $_SERVER['argv'] ) ) {
15        throw new RuntimeException( 'Neither $argv nor $_SERVER[\'argv\'] is an array.' );
16    }
17    // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
18    $argv = $_SERVER['argv'];
19}
20
21if ( count( $argv ) < 2 ) {
22    fprintf( STDERR, "USAGE: %s pattern [args...]\n", $argv[0] );
23    fprintf( STDERR, "\n" );
24    fprintf( STDERR, "The `pattern` argument should contain a `#` character, which will be replaced\n" );
25    fprintf( STDERR, "by the major version of PHPUnit in use.\n" );
26    exit( 1 );
27}
28
29require_once $GLOBALS['_composer_autoload_path'];
30
31if ( ! class_exists( '\\PHPUnit\\Runner\\Version' ) ) {
32    // @phan-suppress-next-line PhanUndeclaredClassMethod -- May as well support super-old phpunit since it's simple to.
33    list( $version ) = explode( '.', PHPUnit_Runner_Version::id() ); // @codeCoverageIgnore
34} else {
35    list( $version ) = explode( '.', \PHPUnit\Runner\Version::id() );
36}
37array_shift( $argv );
38$pattern = array_shift( $argv );
39array_unshift( $argv, $GLOBALS['_composer_bin_dir'] . '/phpunit', '--configuration', str_replace( '#', $version, $pattern ) );
40
41// Handle pcov or xdebug being configured on the command line.
42foreach ( array( 'pcov', 'xdebug' ) as $ext ) {
43    if ( extension_loaded( $ext ) ) {
44        foreach ( ini_get_all( $ext, false ) as $k => $v ) {
45            array_unshift( $argv, "-d$k=$v" );
46        }
47    }
48}
49
50// Pass through auto_prepend_file if set
51$prepend = ini_get( 'auto_prepend_file' );
52if ( $prepend ) {
53    array_unshift( $argv, '-dauto_prepend_file=' . $prepend );
54}
55
56// Assume phpdbg is being run with -qrr.
57if ( 'phpdbg' === php_sapi_name() ) {
58    array_unshift( $argv, '-qrr' );
59}
60
61pcntl_exec( PHP_BINARY, $argv );
62fprintf( STDERR, "Failed to execute %s/phpunit\n", $GLOBALS['_composer_bin_dir'] );
63exit( 1 );