Improve CLI use of Debugging tools and test execution.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60581 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-08-13 01:47:05 +00:00
parent 0e0fc7a1cb
commit 0bdc8fad81
5 changed files with 76 additions and 18 deletions

View File

@ -52,6 +52,19 @@ class CliDebugView extends DebugView {
Debug::backtrace(); Debug::backtrace();
} }
/**
* Render the information header for the view
*
* @param string $title
* @param string $title
*/
public function writeInfo($title, $subtitle, $description=false) {
echo wordwrap(strtoupper($title),100) . "\n";
echo wordwrap($subtitle,100) . "\n";
echo str_repeat('-',min(100,max(strlen($title),strlen($subtitle)))) . "\n";
echo wordwrap($description,100) . "\n\n";
}
} }
?> ?>

40
dev/CliTestReporter.php Normal file
View File

@ -0,0 +1,40 @@
<?php
/**
* Test reporter optimised for CLI (ie, plain-text) output
*/
class CliTestReporter extends SapphireTestReporter {
/**
* Display error bar if it exists
*/
public function writeResults() {
$passCount = 0;
$failCount = 0;
$testCount = 0;
$errorCount = 0;
foreach($this->suiteResults['suites'] as $suite) {
foreach($suite['tests'] as $test) {
$testCount++;
($test['status'] == 1) ? $passCount++ : $failCount++;
}
}
$result = ($failCount > 0) ? 'fail' : 'pass';
echo "$testCount tests run: $passCount passes, $failCount fails, and 0 exceptions\n\n";
}
public function endTest( PHPUnit_Framework_Test $test, $time) {
parent::endTest($test, $time);
$this->writeTest($this->currentTest);
}
protected function writeTest($test) {
if ($test['status'] != 1) {
echo $this->testNameToPhrase($test['name']) . "\n". $test['message'] . "\n";
echo "In line {$test['exception']['line']} of {$test['exception']['file']}" . "\n\n";
}
}
}

View File

@ -75,7 +75,7 @@ class Debug {
if(!Director::isLive()) { if(!Director::isLive()) {
if($showHeader) { if($showHeader) {
$caller = Debug::caller(); $caller = Debug::caller();
if(Director::is_ajax()) if(Director::is_ajax() || Director::is_cli())
echo "Debug ($caller[class]$caller[type]$caller[function]() in line $caller[line] of " . basename($caller['file']) . ")\n"; echo "Debug ($caller[class]$caller[type]$caller[function]() in line $caller[line] of " . basename($caller['file']) . ")\n";
else else
echo "<div style=\"background-color: white; text-align: left;\">\n<hr>\n<h3>Debug <span style=\"font-size: 65%\">($caller[class]$caller[type]$caller[function]() \n<span style=\"font-weight:normal\">in line</span> $caller[line] \n<span style=\"font-weight:normal\">of</span> " . basename($caller['file']) . ")</span>\n</h3>\n"; echo "<div style=\"background-color: white; text-align: left;\">\n<hr>\n<h3>Debug <span style=\"font-size: 65%\">($caller[class]$caller[type]$caller[function]() \n<span style=\"font-weight:normal\">in line</span> $caller[line] \n<span style=\"font-weight:normal\">of</span> " . basename($caller['file']) . ")</span>\n</h3>\n";
@ -83,7 +83,8 @@ class Debug {
echo Debug::text($val); echo Debug::text($val);
if(!Director::is_ajax()) echo "</div>"; if(!Director::is_ajax() && !Director::is_cli()) echo "</div>";
else echo "\n\n";
} }
} }
@ -153,7 +154,7 @@ class Debug {
} else if (is_object($val)) { } else if (is_object($val)) {
$val = var_export($val, true); $val = var_export($val, true);
} else { } else {
if(true || !Director::is_ajax()) { if(!Director::is_cli() && !Director::is_ajax()) {
$val = "<pre style=\"font-family: Courier new\">" . htmlentities($val) . "</pre>\n"; $val = "<pre style=\"font-family: Courier new\">" . htmlentities($val) . "</pre>\n";
} }
} }

View File

@ -42,27 +42,27 @@ class SapphireTestReporter implements PHPUnit_Framework_TestListener {
* Holds array of suites and total number of tests run * Holds array of suites and total number of tests run
* @var array * @var array
*/ */
private $suiteResults; protected $suiteResults;
/** /**
* Holds data of current suite that is been run * Holds data of current suite that is been run
* @var array * @var array
*/ */
private $currentSuite; protected $currentSuite;
/** /**
* Holds data of current test that is been run * Holds data of current test that is been run
* @var array * @var array
*/ */
private $currentTest; protected $currentTest;
/** /**
* Whether PEAR Benchmark_Timer is available for timing * Whether PEAR Benchmark_Timer is available for timing
* @var boolean * @var boolean
*/ */
private $hasTimer; protected $hasTimer;
/** /**
* Holds the PEAR Benchmark_Timer object * Holds the PEAR Benchmark_Timer object
* @var obj Benchmark_Timer * @var obj Benchmark_Timer
*/ */
private $timer; protected $timer;
/** /**
* Constructor, checks to see availability of PEAR Benchmark_Timer and * Constructor, checks to see availability of PEAR Benchmark_Timer and
@ -277,7 +277,7 @@ class SapphireTestReporter implements PHPUnit_Framework_TestListener {
} }
private function testNameToPhrase($name) { protected function testNameToPhrase($name) {
return ucfirst(preg_replace("/([a-z])([A-Z])/", "$1 $2", $name)); return ucfirst(preg_replace("/([a-z])([A-Z])/", "$1 $2", $name));
} }

View File

@ -47,7 +47,7 @@ class TestRunner extends Controller {
function init() { function init() {
parent::init(); parent::init();
if (!self::$default_reporter) self::set_reporter('DebugView'); if (!self::$default_reporter) self::set_reporter(Director::is_cli() ? 'CliDebugView' : 'DebugView');
} }
public function Link() { public function Link() {
@ -126,7 +126,16 @@ class TestRunner extends Controller {
restore_error_handler(); restore_error_handler();
/*, array("reportDirectory" => "/Users/sminnee/phpunit-report")*/ /*, array("reportDirectory" => "/Users/sminnee/phpunit-report")*/
$reporter = new SapphireTestReporter(); if(Director::is_cli()) $reporter = new CliTestReporter();
else $reporter = new SapphireTestReporter();
self::$default_reporter->writeHeader("Sapphire Test Runner");
if (count($classList) > 1) {
self::$default_reporter->writeInfo("All Tests", "Running test cases: " . implode(", ", $classList));
} else {
self::$default_reporter->writeInfo($classList[0], "");
}
$results = new PHPUnit_Framework_TestResult(); $results = new PHPUnit_Framework_TestResult();
$results->addListener($reporter); $results->addListener($reporter);
@ -139,12 +148,6 @@ class TestRunner extends Controller {
//$testResult = PHPUnit_TextUI_TestRunner::run($suite); //$testResult = PHPUnit_TextUI_TestRunner::run($suite);
} }
self::$default_reporter->writeHeader();
if (count($classList) > 1) {
self::$default_reporter->writeInfo("All Tests", "Running test cases: " . implode(", ", $classList) .")");
} else {
self::$default_reporter->writeInfo($classList[0], "");
}
echo '<div class="trace">'; echo '<div class="trace">';
$reporter->writeResults(); $reporter->writeResults();
@ -154,8 +157,9 @@ class TestRunner extends Controller {
Debug::loadErrorHandlers(); Debug::loadErrorHandlers();
if(!Director::is_cli()) self::$default_reporter->writeFooter(); if(!Director::is_cli()) self::$default_reporter->writeFooter();
// Todo: we should figure out how to pass this data back through Director more cleanly // Todo: we should figure out how to pass this data back through Director more cleanly
if(Director::is_cli() && ($testResult->failureCount() + $testResult->errorCount()) > 0) exit(2); if(Director::is_cli() && ($results->failureCount() + $results->errorCount()) > 0) exit(2);
} }
} }