Created
September 25, 2018 14:34
-
-
Save jeremyharris/1e7b73fc0cdd29a8bd57a009fbf02acd to your computer and use it in GitHub Desktop.
Revisions
-
jeremyharris created this gist
Sep 25, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,95 @@ <?php namespace App\TestSuite; use Cake\Collection\Collection; use PHPUnit\Framework\Test; use PHPUnit\Util\Test as TestUtil; use PHPUnit\TextUI\ResultPrinter; class TimerPrinter extends ResultPrinter { /** * @var array */ public static $timers = []; /** * @param Test $test Test * @param float $time Time in microseconds */ public function endTest(Test $test, $time) { $fullTestName = TestUtil::describe($test); list($testName, $method) = explode('::', $fullTestName); self::$timers[] = [ 'test' => $testName, 'method' => $method, 'time' => $time, ]; parent::endTest($test, $time); } /** * @param \PHPUnit\Framework\TestResult $result TestResult * @return void */ public function printFooter(\PHPUnit\Framework\TestResult $result) { $timers = collection(self::$timers) ->sortBy('time', SORT_DESC, SORT_NUMERIC) ->take(10) ->groupBy('test'); if ($this->verbose) { $this->writeTimersVerbose($timers); } else { $this->writeTimers($timers); } parent::printFooter($result); } /** * @param Collection $timers Timers */ private function writeTimersVerbose(Collection $timers) { foreach ($timers as $testClassName => $methodTimers) { $this->write($testClassName); $this->writeNewLine(); foreach ($methodTimers as $methodResults) { $timeAsString = number_format($methodResults['time'] * 1000) . 'ms'; $classParts = namespaceSplit($methodResults['test']); $methodName = substr($classParts[1] . '::' . $methodResults['method'], 0, 70); $pad = 78 - (strlen($methodName) + strlen($timeAsString)); $this->write(' ' . $methodName . str_pad('', $pad, '.') . $timeAsString); $this->writeNewLine(); } } $this->writeNewLine(); } /** * @param Collection $timers Timers */ private function writeTimers(Collection $timers) { foreach ($timers as $testClassName => $methodTimers) { $totalTime = (new Collection($methodTimers))->sumOf('time'); $timeAsString = number_format($totalTime * 1000) . 'ms'; $testClassName = substr($testClassName, 0, 70); $pad = 80 - (strlen($testClassName) + strlen($timeAsString)); $this->write($testClassName . str_pad('', $pad, '.') . $timeAsString); $this->writeNewLine(); } $this->writeNewLine(); } }