Skip to content

Instantly share code, notes, and snippets.

@joehoyle
Created October 11, 2011 22:41
Show Gist options
  • Save joehoyle/1279697 to your computer and use it in GitHub Desktop.
Save joehoyle/1279697 to your computer and use it in GitHub Desktop.
<?php
/*
* Author: Joe Hoyle
* URL: joehoyle.co.uk
* Contact: [email protected]
*/
// Add the command to the wp-cli, only if the plugin is loaded
if ( function_exists( 'wp_unit_add_pages' ) ) {
WP_CLI::addCommand( 'test', 'WPUnitCommand' );
}
/**
* WP Unit
*
* @package wp-cli
* @subpackage commands/community
* @author Joe Hoyle
*/
class WPUnitCommand extends WP_CLI_Command {
var $testCases;
public static function get_description() {
return 'Run unit tests using WP Unit.';
}
public function show( $args = array() ) {
$files = wptest_get_all_test_files(DIR_TESTCASE);
foreach ($files as $file) {
require_once($file);
}
$tests = wptest_get_all_test_cases();
WP_CLI::line( '' );
foreach( $tests as $t )
WP_CLI::line( $t );
WP_CLI::line( '' );
}
public function run( $args = array() ) {
$test = isset( $args[0] ) ? $args[0] : null;
$files = wptest_get_all_test_files(DIR_TESTCASE);
foreach ($files as $file) {
require_once($file);
}
if( $test == 'all' || $test == null )
$this->testCases = wptest_get_all_test_cases();
else
$this->testCases = array( $test );
// run the tests and print the results
$result = new PHPUnit_Framework_TestResult;
$printer = new PHPUnit_TextUI_ResultPrinter;
list ($result, $printer) = $this->_run_tests($this->testCases, @$opts['t']);
$this->_output_results( $result );
}
private function _run_tests($classes, $classname='') {
$suite = new PHPUnit_Framework_TestSuite();
foreach ($classes as $testcase)
{
if (!$classname or strtolower($testcase) == strtolower($classname)) {
$suite->addTestSuite($testcase);
}
}
#return PHPUnit::run($suite);
$result = new PHPUnit_Framework_TestResult;
require_once('PHPUnit/TextUI/ResultPrinter.php');
$printer = new PHPUnit_TextUI_ResultPrinter();
return array($suite->run($result), $printer);
}
private function _output_results( $result ) {
$pass = $result->passed();
$number_of_tests = $result->count();
$detected_failures = $result->failureCount();
$failures = $result->failures();
$incompleted_tests = $result->notImplemented();
$skipped = $result->skipped();
$number_skipped = $result->skippedCount();
$passedKeys = array_keys($pass);
$skippedKeys = array_keys($skipped);
$incompletedKeys = array_keys($incompleted_tests);
$tests = array();
$_tests = array_merge( $passedKeys, $skippedKeys, $incompletedKeys );
foreach ( $_tests as $_test )
$tests[ reset( explode( '::', $_test ) ) ][] = array( 'method' => end( explode( '::', $_test ) ), 'status' => 'OK' );
foreach ( $failures as $failure ) {
$failedTest = $failure->failedTest();
if ($failedTest instanceof PHPUnit_Framework_SelfDescribing)
$_test = $failedTest->toString();
else
$_test = get_class($failedTest);
$tests[ reset( explode( '::', $_test ) ) ][] = array( 'method' => end( explode( '::', $_test ) ), 'status' => 'Failed', 'message' => $failure->getExceptionAsString() );
}
WP_CLI::line( '' );
$failed_count = 0;
foreach ( $tests as $test_case => $test_case_tests ) {
WP_CLI::line( $test_case );
foreach ( $test_case_tests as $test_case_test ) {
switch ( $test_case_test['status'] ) :
case 'OK' :
WP_CLI::success( $test_case_test['method'] );
break;
case 'Failed' :
$failed_count++;
WP_CLI::error( ' ' . $test_case_test['method'] );
WP_CLI::error( ' ' . $test_case_test['message'] );
break;
endswitch;
}
WP_CLI::line( '' );
}
if( $failed_count )
WP_CLI::error( 'Ran ' . $number_of_tests . ' tests. ' . $failed_count . ' Failed.' );
else
WP_CLI::success( 'Ran ' . $number_of_tests . ' tests. ' . $failed_count . ' Failed.' );
}
}
@duncanjbrown
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment