Skip to content

Instantly share code, notes, and snippets.

@coreymckrill
Created April 25, 2013 18:43
Show Gist options
  • Save coreymckrill/5462060 to your computer and use it in GitHub Desktop.
Save coreymckrill/5462060 to your computer and use it in GitHub Desktop.
Custom command for wp-cli, based on `wp core init-tests`. For some reason, `\WP_CLI\Utils\run_mysql_query()` comes up as 'undefined'
<?php
/**
* Custom WP-CLI commands
*
* Usage (from within a WP directory):
* $ wp --require=/path/to/wp-cli-custom.php vvv [function name] [parameters]
*
* @package wp-cli
*/
class VVV_Command extends WP_CLI_Command {
/**
* Alternate method for setting up the official test suite
* using the current WordPress instance. Uses git instead of svn
*
* @subcommand init-tests
*
* @synopsis [<path>] --dbname=<name> --dbuser=<user> [--dbpass=<password>]
*/
function init_tests( $args, $assoc_args ) {
if ( isset( $args[0] ) )
$tests_dir = trailingslashit( $args[0] );
else
$tests_dir = ABSPATH . 'unit-tests/';
$assoc_args = wp_parse_args( $assoc_args, array(
'dbpass' => '',
) );
// Download the test suite
//WP_CLI::launch( 'svn co https://unit-test.svn.wordpress.org/trunk/ ' . escapeshellarg( $tests_dir ) );
WP_CLI::launch( 'git clone -v https://github.com/kurtpayne/wordpress-unit-tests.git ' . escapeshellarg( $tests_dir ) );
// Create the database
$query = sprintf( 'CREATE DATABASE IF NOT EXISTS `%s`', $assoc_args['dbname'] );
\WP_CLI\Utils\run_mysql_query( $query, array(
'host' => 'localhost',
'user' => $assoc_args['dbuser'],
'pass' => $assoc_args['dbpass'],
) );
// Create the wp-tests-config.php file
$config_file = file_get_contents( $tests_dir . 'wp-tests-config-sample.php' );
$replacements = array(
"dirname( __FILE__ ) . '/wordpress/'" => "'" . ABSPATH . "'",
"yourdbnamehere" => $assoc_args['dbname'],
"yourusernamehere" => $assoc_args['dbuser'],
"yourpasswordhere" => $assoc_args['dbpass'],
);
$config_file = str_replace( array_keys( $replacements ), array_values( $replacements ), $config_file );
$config_file_path = $tests_dir . 'wp-tests-config.php';
file_put_contents( $config_file_path, $config_file );
WP_CLI::success( "Created $config_file_path" );
}
}
WP_CLI::add_command( 'vvv', 'VVV_Command' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment