Version: current

     1 <?php
     2 /**
     3  * Test reporter optimised for CLI (ie, plain-text) output
     4  *
     5  * @package sapphire
     6  * @subpackage testing
     7  */
     8 class CliTestReporter extends SapphireTestReporter {
     9
    10     /**
    11      * Display error bar if it exists
    12      */
    13     public function writeResults() {
    14         $passCount = 0;
    15         $failCount = 0;
    16         $testCount = 0;
    17         $errorCount = 0;
    18
    19         foreach($this->suiteResults['suites'] as $suite) {
    20             foreach($suite['tests'] as $test) {
    21                 $testCount++;
    22                 ($test['status'] == 1) ? $passCount++ : $failCount++;
    23             }
    24         }
    25
    26         echo "\n\n";
    27         if ($failCount == 0) {
    28             echo SS_Cli::text(" ALL TESTS PASS ", "white", "green");
    29         }  else {
    30             echo SS_Cli::text(" AT LEAST ONE FAILURE ", "white", "red");
    31         }
    32         echo "\n\n$testCount tests run: " . SS_Cli::text("$passCount passes", null) . ", ". SS_Cli::text("$failCount fails", null) . ", and 0 exceptions\n";
    33
    34         if(function_exists('memory_get_peak_usage')) {
    35             echo "Maximum memory usage: " . number_format(memory_get_peak_usage()/(1024*1024), 1) . "M\n\n";
    36         }
    37
    38         // Use sake dev/tests/all --showslow to show slow tests
    39         if((isset($_GET['args']) && is_array($_GET['args']) && in_array('--showslow', $_GET['args'])) || isset($_GET['showslow'])) {
    40             $avgSpeed = round(array_sum($this->testSpeeds) / count($this->testSpeeds), 3);
    41             echo "Slow tests (more than the average $avgSpeed seconds):\n";
    42
    43             arsort($this->testSpeeds);
    44             foreach($this->testSpeeds as $k => $v) {
    45                 // Ignore below-average speeds
    46                 if($v < $avgSpeed) break;
    47
    48                 echo " - $k: " . round($v,3) . "\n";
    49             }
    50         }
    51         echo "\n";
    52     }
    53
    54     public function endTest( PHPUnit_Framework_Test $test, $time) {
    55         // Status indicator, a la PHPUnit
    56         switch($this->currentTest['status']) {
    57             case TEST_FAILURE: echo SS_Cli::text("F","red", null, true); break;
    58             case TEST_ERROR: echo SS_Cli::text("E","red", null, true); break;
    59             case TEST_INCOMPLETE: echo SS_Cli::text("I","yellow"); break;
    60             case TEST_SUCCESS: echo SS_Cli::text(".","green"); break;
    61             default: echo SS_Cli::text("?", "yellow"); break;
    62         }
    63
    64         static $colCount = 0;
    65         $colCount++;
    66         if($colCount % 80 == 0) echo " - $colCount\n";
    67
    68         parent::endTest($test, $time);
    69         $this->writeTest($this->currentTest);
    70     }
    71
    72
    73     protected function writeTest($test) {
    74         if ($test['status'] != 1) {
    75
    76             $filteredTrace = array();
    77             $ignoredClasses = array('TestRunner');
    78             foreach($test['trace'] as $item) {
    79                 if(
    80                     isset($item['file'])
    81                     && strpos($item['file'], 'PHPUnit/Framework') === false
    82                     && (!isset($item['class']) || !in_array($item['class'], $ignoredClasses))) {
    83
    84                     $filteredTrace[] = $item;
    85                 }
    86
    87                 if(isset($item['class']) && isset($item['function']) && $item['class'] == 'PHPUnit_Framework_TestSuite'
    88                     && $item['function'] == 'run') break;
    89
    90             }
    91
    92             echo "\n\n" . SS_Cli::text($this->testNameToPhrase($test['name']) . "\n". $test['message'] . "\n", 'red', null, true);
    93             echo SS_Cli::text("In line {$test['exception']['line']} of {$test['exception']['file']}" . "\n\n", 'red');
    94             echo SS_Backtrace::get_rendered_backtrace($filteredTrace, true);
    95             echo "\n--------------------\n";
    96         }
    97     }
    98
    99 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -