From f5a2d58d6fa34097d57b49ee6ec73ae1cd634675 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 29 Sep 2017 06:56:14 -0700 Subject: [PATCH] Update scaffolded tests to latest --- composer.json | 2 +- features/bootstrap/FeatureContext.php | 8 +- features/bootstrap/Process.php | 22 +-- features/bootstrap/utils.php | 249 +++++++++++++++++++------- features/steps/given.php | 6 + 5 files changed, 213 insertions(+), 74 deletions(-) diff --git a/composer.json b/composer.json index 3032aedf5..aa6e88aef 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "wp-cli/shell-command", - "description": "Interactive PHP console.", + "description": "Opens an interactive PHP console for running and testing PHP code.", "type": "wp-cli-package", "homepage": "https://github.com/wp-cli/shell-command", "support": { diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index b46de6428..aaf39713b 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -699,11 +699,15 @@ public function get_php_binary() { return 'php'; } - public function start_php_server() { + public function start_php_server( $subdir = '' ) { + $dir = $this->variables['RUN_DIR'] . '/'; + if ( $subdir ) { + $dir .= trim( $subdir, '/' ) . '/'; + } $cmd = Utils\esc_cmd( '%s -S %s -t %s -c %s %s', $this->get_php_binary(), 'localhost:8080', - $this->variables['RUN_DIR'] . '/wordpress/', + $dir, get_cfg_var( 'cfg_file_path' ), $this->variables['RUN_DIR'] . '/vendor/wp-cli/server-command/router.php' ); diff --git a/features/bootstrap/Process.php b/features/bootstrap/Process.php index 8032a2415..5016580b7 100644 --- a/features/bootstrap/Process.php +++ b/features/bootstrap/Process.php @@ -87,15 +87,17 @@ public function run() { self::$run_times[ $this->command ][1]++; } - return new ProcessRun( array( - 'stdout' => $stdout, - 'stderr' => $stderr, - 'return_code' => $return_code, - 'command' => $this->command, - 'cwd' => $this->cwd, - 'env' => $this->env, - 'run_time' => $run_time, - ) ); + return new ProcessRun( + array( + 'stdout' => $stdout, + 'stderr' => $stderr, + 'return_code' => $return_code, + 'command' => $this->command, + 'cwd' => $this->cwd, + 'env' => $this->env, + 'run_time' => $run_time, + ) + ); } /** @@ -107,7 +109,7 @@ public function run_check() { $r = $this->run(); // $r->STDERR is incorrect, but kept incorrect for backwards-compat - if ( $r->return_code || !empty( $r->STDERR ) ) { + if ( $r->return_code || ! empty( $r->STDERR ) ) { throw new \RuntimeException( $r ); } diff --git a/features/bootstrap/utils.php b/features/bootstrap/utils.php index fc8d0df84..32caac15f 100644 --- a/features/bootstrap/utils.php +++ b/features/bootstrap/utils.php @@ -28,9 +28,11 @@ function extract_from_phar( $path ) { copy( $path, $tmp_path ); - register_shutdown_function( function() use ( $tmp_path ) { - @unlink( $tmp_path ); - } ); + register_shutdown_function( + function() use ( $tmp_path ) { + @unlink( $tmp_path ); + } + ); return $tmp_path; } @@ -55,9 +57,9 @@ function load_dependencies() { } } - if ( !$has_autoload ) { + if ( ! $has_autoload ) { fputs( STDERR, "Internal error: Can't find Composer autoloader.\nTry running: composer install\n" ); - exit(3); + exit( 3 ); } } @@ -113,7 +115,7 @@ function iterator_map( $it, $fn ) { $it = new \ArrayIterator( $it ); } - if ( !method_exists( $it, 'add_transform' ) ) { + if ( ! method_exists( $it, 'add_transform' ) ) { $it = new Transform( $it ); } @@ -150,7 +152,7 @@ function find_file_upward( $files, $dir = null, $stop_check = null ) { } $parent_dir = dirname( $dir ); - if ( empty($parent_dir) || $parent_dir === $dir ) { + if ( empty( $parent_dir ) || $parent_dir === $dir ) { break; } $dir = $parent_dir; @@ -160,8 +162,9 @@ function find_file_upward( $files, $dir = null, $stop_check = null ) { function is_path_absolute( $path ) { // Windows - if ( isset($path[1]) && ':' === $path[1] ) + if ( isset( $path[1] ) && ':' === $path[1] ) { return true; + } return $path[0] === '/'; } @@ -188,9 +191,13 @@ function assoc_args_to_str( $assoc_args ) { foreach ( $assoc_args as $key => $value ) { if ( true === $value ) { $str .= " --$key"; - } elseif( is_array( $value ) ) { - foreach( $value as $_ => $v ) { - $str .= assoc_args_to_str( array( $key => $v ) ); + } elseif ( is_array( $value ) ) { + foreach ( $value as $_ => $v ) { + $str .= assoc_args_to_str( + array( + $key => $v, + ) + ); } } else { $str .= " --$key=" . escapeshellarg( $value ); @@ -205,8 +212,9 @@ function assoc_args_to_str( $assoc_args ) { * returns the final command, with the parameters escaped. */ function esc_cmd( $cmd ) { - if ( func_num_args() < 2 ) + if ( func_num_args() < 2 ) { trigger_error( 'esc_cmd() requires at least two arguments.', E_USER_WARNING ); + } $args = func_get_args(); @@ -219,15 +227,17 @@ function locate_wp_config() { static $path; if ( null === $path ) { - if ( file_exists( ABSPATH . 'wp-config.php' ) ) + if ( file_exists( ABSPATH . 'wp-config.php' ) ) { $path = ABSPATH . 'wp-config.php'; - elseif ( file_exists( ABSPATH . '../wp-config.php' ) && ! file_exists( ABSPATH . '/../wp-settings.php' ) ) + } elseif ( file_exists( ABSPATH . '../wp-config.php' ) && ! file_exists( ABSPATH . '/../wp-settings.php' ) ) { $path = ABSPATH . '../wp-config.php'; - else + } else { $path = false; + } - if ( $path ) + if ( $path ) { $path = realpath( $path ); + } } return $path; @@ -358,7 +368,7 @@ function launch_editor_for_input( $input, $filename = 'WP-CLI' ) { if ( $fp ) { fclose( $fp ); } - } while( ! $tmpfile ); + } while ( ! $tmpfile ); if ( ! $tmpfile ) { \WP_CLI::error( 'Error creating temporary file.' ); @@ -368,11 +378,12 @@ function launch_editor_for_input( $input, $filename = 'WP-CLI' ) { file_put_contents( $tmpfile, $input ); $editor = getenv( 'EDITOR' ); - if ( !$editor ) { - if ( isset( $_SERVER['OS'] ) && false !== strpos( $_SERVER['OS'], 'indows' ) ) + if ( ! $editor ) { + if ( isset( $_SERVER['OS'] ) && false !== strpos( $_SERVER['OS'], 'indows' ) ) { $editor = 'notepad'; - else + } else { $editor = 'vi'; + } } $descriptorspec = array( STDIN, STDOUT, STDERR ); @@ -386,8 +397,9 @@ function launch_editor_for_input( $input, $filename = 'WP-CLI' ) { unlink( $tmpfile ); - if ( $output === $input ) + if ( $output === $input ) { return false; + } return $output; } @@ -406,7 +418,7 @@ function mysql_host_to_cli_args( $raw_host ) { if ( is_numeric( $extra ) ) { $assoc_args['port'] = intval( $extra ); $assoc_args['protocol'] = 'tcp'; - } else if ( $extra !== '' ) { + } elseif ( $extra !== '' ) { $assoc_args['socket'] = $extra; } } else { @@ -419,8 +431,9 @@ function mysql_host_to_cli_args( $raw_host ) { function run_mysql_command( $cmd, $assoc_args, $descriptors = null ) { check_proc_available( 'run_mysql_command' ); - if ( !$descriptors ) + if ( ! $descriptors ) { $descriptors = array( STDIN, STDOUT, STDERR ); + } if ( isset( $assoc_args['host'] ) ) { $assoc_args = array_merge( $assoc_args, mysql_host_to_cli_args( $assoc_args['host'] ) ); @@ -435,14 +448,17 @@ function run_mysql_command( $cmd, $assoc_args, $descriptors = null ) { $final_cmd = force_env_on_nix_systems( $cmd ) . assoc_args_to_str( $assoc_args ); $proc = proc_open( $final_cmd, $descriptors, $pipes ); - if ( !$proc ) - exit(1); + if ( ! $proc ) { + exit( 1 ); + } $r = proc_close( $proc ); putenv( 'MYSQL_PWD=' . $old_pass ); - if ( $r ) exit( $r ); + if ( $r ) { + exit( $r ); + } } /** @@ -451,14 +467,18 @@ function run_mysql_command( $cmd, $assoc_args, $descriptors = null ) { * IMPORTANT: Automatic HTML escaping is disabled! */ function mustache_render( $template_name, $data = array() ) { - if ( ! file_exists( $template_name ) ) + if ( ! file_exists( $template_name ) ) { $template_name = WP_CLI_ROOT . "/templates/$template_name"; + } $template = file_get_contents( $template_name ); - $m = new \Mustache_Engine( array( - 'escape' => function ( $val ) { return $val; }, - ) ); + $m = new \Mustache_Engine( + array( + 'escape' => function ( $val ) { + return $val; }, + ) + ); return $m->render( $template, $data ); } @@ -492,8 +512,9 @@ function mustache_render( $template_name, $data = array() ) { * @return cli\progress\Bar|WP_CLI\NoOp */ function make_progress_bar( $message, $count ) { - if ( \cli\Shell::isPiped() ) + if ( \cli\Shell::isPiped() ) { return new \WP_CLI\NoOp; + } return new \cli\progress\Bar( $message, $count ); } @@ -501,7 +522,7 @@ function make_progress_bar( $message, $count ) { function parse_url( $url ) { $url_parts = \parse_url( $url ); - if ( !isset( $url_parts['scheme'] ) ) { + if ( ! isset( $url_parts['scheme'] ) ) { $url_parts = parse_url( 'http://' . $url ); } @@ -514,7 +535,7 @@ function parse_url( $url ) { * @return bool */ function is_windows() { - return false !== ( $test_is_windows = getenv( 'WP_CLI_TEST_IS_WINDOWS' ) ) ? (bool) $test_is_windows : strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; + return false !== ( $test_is_windows = getenv( 'WP_CLI_TEST_IS_WINDOWS' ) ) ? (bool) $test_is_windows : strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN'; } /** @@ -563,23 +584,24 @@ function http_request( $method, $url, $data = null, $headers = array(), $options if ( inside_phar() ) { // cURL can't read Phar archives $options['verify'] = extract_from_phar( - WP_CLI_VENDOR_DIR . $cert_path ); + WP_CLI_VENDOR_DIR . $cert_path + ); } else { - foreach( get_vendor_paths() as $vendor_path ) { + foreach ( get_vendor_paths() as $vendor_path ) { if ( file_exists( $vendor_path . $cert_path ) ) { $options['verify'] = $vendor_path . $cert_path; break; } } - if ( empty( $options['verify'] ) ){ - WP_CLI::error( "Cannot find SSL certificate." ); + if ( empty( $options['verify'] ) ) { + WP_CLI::error( 'Cannot find SSL certificate.' ); } } try { $request = \Requests::request( $url, $headers, $data, $method, $options ); return $request; - } catch( \Requests_Exception $ex ) { + } catch ( \Requests_Exception $ex ) { // CURLE_SSL_CACERT_BADFILE only defined for PHP >= 7. if ( 'curlerror' !== $ex->getType() || ! in_array( curl_errno( $ex->getData() ), array( CURLE_SSL_CONNECT_ERROR, CURLE_SSL_CERTPROBLEM, 77 /*CURLE_SSL_CACERT_BADFILE*/ ), true ) ) { \WP_CLI::error( sprintf( "Failed to get url '%s': %s.", $url, $ex->getMessage() ) ); @@ -589,7 +611,7 @@ function http_request( $method, $url, $data = null, $headers = array(), $options $options['verify'] = false; try { return \Requests::request( $url, $headers, $data, $method, $options ); - } catch( \Requests_Exception $ex ) { + } catch ( \Requests_Exception $ex ) { \WP_CLI::error( sprintf( "Failed to get non-verified url '%s' %s.", $url, $ex->getMessage() ) ); } } @@ -676,7 +698,7 @@ function get_named_sem_ver( $new_version, $original_version ) { if ( ! is_null( $minor ) && Semver::satisfies( $new_version, "{$major}.{$minor}.x" ) ) { return 'patch'; - } else if ( Semver::satisfies( $new_version, "{$major}.x.x" ) ) { + } elseif ( Semver::satisfies( $new_version, "{$major}.x.x" ) ) { return 'minor'; } else { return 'major'; @@ -780,7 +802,7 @@ function get_temp_dir() { function parse_ssh_url( $url, $component = -1 ) { preg_match( '#^((docker|docker\-compose|ssh|vagrant):)?(([^@:]+)@)?([^:/~]+)(:([\d]*))?((/|~)(.+))?$#', $url, $matches ); $bits = array(); - foreach( array( + foreach ( array( 2 => 'scheme', 4 => 'user', 5 => 'host', @@ -849,17 +871,19 @@ function report_batch_operation_results( $noun, $verb, $total, $successes, $fail * @return array */ function parse_str_to_argv( $arguments ) { - preg_match_all ('/(?<=^|\s)([\'"]?)(.+?)(? 'reset' ); + static $irregular = array( + 'reset' => 'reset', + ); if ( isset( $irregular[ $verb ] ) ) { return $irregular[ $verb ]; } diff --git a/features/steps/given.php b/features/steps/given.php index e568bff3f..f2eae113f 100644 --- a/features/steps/given.php +++ b/features/steps/given.php @@ -211,3 +211,9 @@ function ( $world ) { $world->start_php_server(); } ); + +$steps->Given( "/^a PHP built-in web server to serve '([^\s]+)'$/", + function ( $world, $subdir ) { + $world->start_php_server( $subdir ); + } +);