2008-08-13 03:47:05 +02:00
|
|
|
<?php
|
2016-01-25 22:27:52 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Dev;
|
|
|
|
|
|
|
|
use PHPUnit_Framework_Test;
|
|
|
|
|
|
|
|
if(!class_exists('SilverStripe\\Dev\\SapphireTestReporter')) {
|
2016-01-25 22:27:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-13 03:47:05 +02:00
|
|
|
/**
|
|
|
|
* Test reporter optimised for CLI (ie, plain-text) output
|
|
|
|
*/
|
|
|
|
class CliTestReporter extends SapphireTestReporter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display error bar if it exists
|
|
|
|
*/
|
|
|
|
public function writeResults() {
|
|
|
|
$passCount = 0;
|
2015-12-09 22:19:23 +01:00
|
|
|
$failCount = $this->currentSession['failures'];
|
2008-08-13 03:47:05 +02:00
|
|
|
$testCount = 0;
|
2015-12-09 22:19:23 +01:00
|
|
|
$incompleteCount = $this->currentSession['incomplete'];
|
|
|
|
$errorCount = $this->currentSession['errors'];
|
2012-04-14 13:11:54 +02:00
|
|
|
|
2008-08-13 03:47:05 +02:00
|
|
|
foreach($this->suiteResults['suites'] as $suite) {
|
|
|
|
foreach($suite['tests'] as $test) {
|
|
|
|
$testCount++;
|
2015-12-09 22:19:23 +01:00
|
|
|
switch($test['status']) {
|
|
|
|
case TEST_INCOMPLETE: {
|
|
|
|
$incompleteCount++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TEST_SUCCESS: {
|
|
|
|
$passCount++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TEST_ERROR: {
|
|
|
|
$errorCount++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
$failCount++;
|
|
|
|
break;
|
|
|
|
}
|
2012-04-14 13:11:54 +02:00
|
|
|
}
|
2008-08-13 03:47:05 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-04 07:41:15 +02:00
|
|
|
echo "\n\n";
|
2015-12-09 22:19:23 +01:00
|
|
|
$breakages = $errorCount + $failCount;
|
|
|
|
if ($breakages == 0 && $incompleteCount > 0) {
|
2016-09-09 08:43:05 +02:00
|
|
|
echo CLI::text(" OK, BUT INCOMPLETE TESTS! ", "black", "yellow");
|
2015-12-09 22:19:23 +01:00
|
|
|
} elseif ($breakages == 0) {
|
2016-09-09 08:43:05 +02:00
|
|
|
echo CLI::text(" ALL TESTS PASS ", "black", "green");
|
2008-10-04 07:41:15 +02:00
|
|
|
} else {
|
2016-09-09 08:43:05 +02:00
|
|
|
echo CLI::text(" AT LEAST ONE FAILURE ", "black", "red");
|
2008-10-04 07:41:15 +02:00
|
|
|
}
|
2012-04-14 13:11:54 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
echo sprintf("\n\n%d tests run: %s, %s, and %s\n", $testCount, CLI::text("$passCount passes"),
|
|
|
|
CLI::text("$breakages failures"), CLI::text("$incompleteCount incomplete"));
|
2012-04-14 13:11:54 +02:00
|
|
|
|
2012-04-16 03:11:21 +02:00
|
|
|
echo "Maximum memory usage: " . number_format(memory_get_peak_usage()/(1024*1024), 1) . "M\n\n";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-17 04:28:55 +02:00
|
|
|
// Use sake dev/tests/all --showslow to show slow tests
|
2012-09-26 23:34:00 +02:00
|
|
|
if((isset($_GET['args']) && is_array($_GET['args']) && in_array('--showslow', $_GET['args']))
|
|
|
|
|| isset($_GET['showslow'])) {
|
|
|
|
|
2008-10-17 04:28:55 +02:00
|
|
|
$avgSpeed = round(array_sum($this->testSpeeds) / count($this->testSpeeds), 3);
|
2010-04-12 04:03:16 +02:00
|
|
|
echo "Slow tests (more than the average $avgSpeed seconds):\n";
|
2008-10-17 04:28:55 +02:00
|
|
|
|
|
|
|
arsort($this->testSpeeds);
|
|
|
|
foreach($this->testSpeeds as $k => $v) {
|
|
|
|
// Ignore below-average speeds
|
2010-04-12 04:03:16 +02:00
|
|
|
if($v < $avgSpeed) break;
|
2008-10-17 04:28:55 +02:00
|
|
|
|
|
|
|
echo " - $k: " . round($v,3) . "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo "\n";
|
2008-08-13 03:47:05 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-13 03:47:05 +02:00
|
|
|
public function endTest( PHPUnit_Framework_Test $test, $time) {
|
2008-09-23 05:22:13 +02:00
|
|
|
// Status indicator, a la PHPUnit
|
|
|
|
switch($this->currentTest['status']) {
|
2016-09-09 08:43:05 +02:00
|
|
|
case TEST_FAILURE: echo CLI::text("F","red", null, true); break;
|
|
|
|
case TEST_ERROR: echo CLI::text("E","red", null, true); break;
|
|
|
|
case TEST_INCOMPLETE: echo CLI::text("I","yellow"); break;
|
|
|
|
case TEST_SUCCESS: echo CLI::text(".","green"); break;
|
|
|
|
default: echo CLI::text("?", "yellow"); break;
|
2008-09-23 05:22:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 04:23:35 +02:00
|
|
|
static $colCount = 0;
|
|
|
|
$colCount++;
|
2010-10-19 02:56:25 +02:00
|
|
|
if($colCount % 80 == 0) echo " - $colCount\n";
|
2008-10-03 04:23:35 +02:00
|
|
|
|
2008-08-13 03:47:05 +02:00
|
|
|
$this->writeTest($this->currentTest);
|
2012-09-19 11:41:34 +02:00
|
|
|
parent::endTest($test, $time);
|
2008-08-13 03:47:05 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
protected function addStatus($status, $message, $exception, $trace) {
|
|
|
|
if(!$this->currentTest && !$this->currentSuite) {
|
|
|
|
// Log non-test errors immediately
|
|
|
|
$statusResult = array(
|
|
|
|
'status' => $status,
|
|
|
|
'message' => $message,
|
|
|
|
'exception' => $exception,
|
|
|
|
'trace' => $trace
|
|
|
|
);
|
|
|
|
$this->writeTest($statusResult);
|
|
|
|
}
|
|
|
|
parent::addStatus($status, $message, $exception, $trace);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-13 03:47:05 +02:00
|
|
|
protected function writeTest($test) {
|
2012-09-19 11:41:34 +02:00
|
|
|
if ($test['status'] != TEST_SUCCESS) {
|
2008-10-09 03:45:36 +02:00
|
|
|
$filteredTrace = array();
|
|
|
|
foreach($test['trace'] as $item) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($item['file'])
|
2014-08-15 08:53:05 +02:00
|
|
|
&& strpos($item['file'], 'PHPUnit/Framework') === false
|
2015-07-16 11:32:42 +02:00
|
|
|
&& !isset($item['class'])) {
|
2008-10-09 03:45:36 +02:00
|
|
|
$filteredTrace[] = $item;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($item['class']) && isset($item['function']) && $item['class'] == 'PHPUnit_Framework_TestSuite'
|
|
|
|
&& $item['function'] == 'run') {
|
|
|
|
break;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-09 03:45:36 +02:00
|
|
|
}
|
2012-04-14 13:11:54 +02:00
|
|
|
|
2012-04-14 14:26:17 +02:00
|
|
|
$color = ($test['status'] == 2) ? 'yellow' : 'red';
|
2016-09-09 08:43:05 +02:00
|
|
|
echo "\n" . CLI::text($test['name'] . "\n". $test['message'] . "\n", $color, null);
|
|
|
|
echo Backtrace::get_rendered_backtrace($filteredTrace, true);
|
2012-04-14 14:07:15 +02:00
|
|
|
echo "--------------------\n";
|
2008-08-13 03:47:05 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 13:11:54 +02:00
|
|
|
}
|