From 0bdc8fad81e7a8f368a7807b9975af8732fc34d6 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 13 Aug 2008 01:47:05 +0000 Subject: [PATCH] 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 --- dev/CliDebugView.php | 13 ++++++++++++ dev/CliTestReporter.php | 40 ++++++++++++++++++++++++++++++++++++ dev/Debug.php | 7 ++++--- dev/SapphireTestReporter.php | 12 +++++------ dev/TestRunner.php | 22 ++++++++++++-------- 5 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 dev/CliTestReporter.php diff --git a/dev/CliDebugView.php b/dev/CliDebugView.php index 301ae220d..f22895d8a 100644 --- a/dev/CliDebugView.php +++ b/dev/CliDebugView.php @@ -51,6 +51,19 @@ class CliDebugView extends DebugView { function writeTrace() { 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"; + } } diff --git a/dev/CliTestReporter.php b/dev/CliTestReporter.php new file mode 100644 index 000000000..27d6a4474 --- /dev/null +++ b/dev/CliTestReporter.php @@ -0,0 +1,40 @@ +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"; + } + } + +} \ No newline at end of file diff --git a/dev/Debug.php b/dev/Debug.php index f6cf4beba..41c4fe5f3 100644 --- a/dev/Debug.php +++ b/dev/Debug.php @@ -75,7 +75,7 @@ class Debug { if(!Director::isLive()) { if($showHeader) { $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"; else echo "
\n
\n

Debug ($caller[class]$caller[type]$caller[function]() \nin line $caller[line] \nof " . basename($caller['file']) . ")\n

\n"; @@ -83,7 +83,8 @@ class Debug { echo Debug::text($val); - if(!Director::is_ajax()) echo "
"; + if(!Director::is_ajax() && !Director::is_cli()) echo ""; + else echo "\n\n"; } } @@ -153,7 +154,7 @@ class Debug { } else if (is_object($val)) { $val = var_export($val, true); } else { - if(true || !Director::is_ajax()) { + if(!Director::is_cli() && !Director::is_ajax()) { $val = "
" . htmlentities($val) . "
\n"; } } diff --git a/dev/SapphireTestReporter.php b/dev/SapphireTestReporter.php index 679ea347b..b53263d8a 100644 --- a/dev/SapphireTestReporter.php +++ b/dev/SapphireTestReporter.php @@ -42,27 +42,27 @@ class SapphireTestReporter implements PHPUnit_Framework_TestListener { * Holds array of suites and total number of tests run * @var array */ - private $suiteResults; + protected $suiteResults; /** * Holds data of current suite that is been run * @var array */ - private $currentSuite; + protected $currentSuite; /** * Holds data of current test that is been run * @var array */ - private $currentTest; + protected $currentTest; /** * Whether PEAR Benchmark_Timer is available for timing * @var boolean */ - private $hasTimer; + protected $hasTimer; /** * Holds the PEAR Benchmark_Timer object * @var obj Benchmark_Timer */ - private $timer; + protected $timer; /** * 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)); } diff --git a/dev/TestRunner.php b/dev/TestRunner.php index cec1e9fe7..7519445a2 100644 --- a/dev/TestRunner.php +++ b/dev/TestRunner.php @@ -47,7 +47,7 @@ class TestRunner extends Controller { function 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() { @@ -126,7 +126,16 @@ class TestRunner extends Controller { restore_error_handler(); /*, 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->addListener($reporter); @@ -139,12 +148,6 @@ class TestRunner extends Controller { //$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 '
'; $reporter->writeResults(); @@ -154,8 +157,9 @@ class TestRunner extends Controller { Debug::loadErrorHandlers(); if(!Director::is_cli()) self::$default_reporter->writeFooter(); + // 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); } }