123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- class SpeedTrapListener implements PHPUnit_Framework_TestListener {
-
- protected $suites = 0;
-
- protected $slow_threshold;
-
- protected $report_length;
-
- protected $slow = array();
-
- public function __construct( array $options = array() ) {
- $this->loadOptions( $options );
- }
-
- public function addError( PHPUnit_Framework_Test $test, Exception $e, $time ) {
- }
-
- public function addWarning( PHPUnit_Framework_Test $test, PHPUnit_Framework_Warning $e, $time ) {
- }
-
- public function addFailure( PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time ) {
- }
-
- public function addIncompleteTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
- }
-
- public function addRiskyTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
- }
-
- public function addSkippedTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
- }
-
- public function startTest( PHPUnit_Framework_Test $test ) {
- }
-
- public function endTest( PHPUnit_Framework_Test $test, $time ) {
- if ( ! $test instanceof PHPUnit_Framework_TestCase ) {
- return;
- }
- $time = $this->toMilliseconds( $time );
- $threshold = $this->getSlowThreshold( $test );
- if ( $this->isSlow( $time, $threshold ) ) {
- $this->addSlowTest( $test, $time );
- }
- }
-
- public function startTestSuite( PHPUnit_Framework_TestSuite $suite ) {
- $this->suites++;
- }
-
- public function endTestSuite( PHPUnit_Framework_TestSuite $suite ) {
- $this->suites--;
- if ( 0 === $this->suites && $this->hasSlowTests() ) {
- arsort( $this->slow );
- $this->renderHeader();
- $this->renderBody();
- $this->renderFooter();
- }
- }
-
- protected function isSlow( $time, $slow_threshold ) {
- return $time >= $slow_threshold;
- }
-
- protected function addSlowTest( PHPUnit_Framework_TestCase $test, $time ) {
- $label = $this->makeLabel( $test );
- $this->slow[ $label ] = $time;
- }
-
- protected function hasSlowTests() {
- return ! empty( $this->slow );
- }
-
- protected function toMilliseconds( $time ) {
- return (int) round( $time * 1000 );
- }
-
- protected function makeLabel( PHPUnit_Framework_TestCase $test ) {
- return sprintf( '%s:%s', get_class( $test ), $test->getName() );
- }
-
- protected function getReportLength() {
- return min( count( $this->slow ), $this->report_length );
- }
-
- protected function getHiddenCount() {
- $total = count( $this->slow );
- $showing = $this->getReportLength( $this->slow );
- $hidden = 0;
- if ( $total > $showing ) {
- $hidden = $total - $showing;
- }
- return $hidden;
- }
-
- protected function renderHeader() {
- echo sprintf( "\n\nYou should really fix these slow tests (>%sms)...\n", $this->slow_threshold );
- }
-
- protected function renderBody() {
- $slow_tests = $this->slow;
- $length = $this->getReportLength( $slow_tests );
- for ( $i = 1; $i <= $length; ++$i ) {
- $label = key( $slow_tests );
- $time = array_shift( $slow_tests );
- echo sprintf( " %s. %sms to run %s\n", $i, $time, $label );
- }
- }
-
- protected function renderFooter() {
- if ( $hidden = $this->getHiddenCount( $this->slow ) ) {
- echo sprintf( '...and there %s %s more above your threshold hidden from view', 1 === $hidden ? 'is' : 'are', $hidden );
- }
- }
-
- protected function loadOptions( array $options ) {
- $this->slow_threshold = isset( $options['slowThreshold'] ) ? $options['slowThreshold'] : 500;
- $this->report_length = isset( $options['reportLength'] ) ? $options['reportLength'] : 10;
- }
-
- protected function getSlowThreshold( PHPUnit_Framework_TestCase $test ) {
- $ann = $test->getAnnotations();
- return isset( $ann['method']['slowThreshold'][0] ) ? $ann['method']['slowThreshold'][0] : $this->slow_threshold;
- }
- }
|