Completed
Pull Request — master (#2)
by Tobias
05:56
created

ExecutionTimeTestListener   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 74
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A startTest() 0 2 1
A endTest() 0 5 3
A addError() 0 2 1
A addFailure() 0 2 1
A addIncompleteTest() 0 2 1
A addRiskyTest() 0 2 1
A addSkippedTest() 0 2 1
A startTestSuite() 0 2 1
A endTestSuite() 0 6 2
1
<?php
2
3
namespace BootstrapComponents\Tests;
4
5
use Exception;
6
use PHPUnit_Framework_AssertionFailedError;
7
use PHPUnit_Framework_Test;
8
use PHPUnit_Framework_TestListener;
9
use PHPUnit_Framework_TestSuite;
10
11
/**
12
 * Class ExecutionTimeTestListener
13
 *
14
 * @package BootstrapComponents\Tests
15
 */
16
class ExecutionTimeTestListener implements PHPUnit_Framework_TestListener {
17
18
	protected $testCollector = array();
19
	protected $executionTimeThresholdInSeconds = 10;
20
	protected $isEnabledToListen = true;
21
22
	public function __construct( $isEnabledToListen, $executionTimeThresholdInSeconds ) {
23
		$this->isEnabledToListen = $isEnabledToListen;
24
		$this->executionTimeThresholdInSeconds = $executionTimeThresholdInSeconds;
25
	}
26
27
	/**
28
	 * @see PHPUnit_Framework_TestListener::startTest
29
	 */
30
	public function startTest( PHPUnit_Framework_Test $test ) {
31
	}
32
33
	/**
34
	 * @see PHPUnit_Framework_TestListener::endTest
35
	 */
36
	public function endTest( PHPUnit_Framework_Test $test, $length ) {
37
		if ( $this->isEnabledToListen && ( $length > $this->executionTimeThresholdInSeconds ) ) {
38
			$this->testCollector[$test->getName()] = round( $length, 3 );
39
		}
40
	}
41
42
	/**
43
	 * @see PHPUnit_Framework_TestListener::addError
44
	 */
45
	public function addError( PHPUnit_Framework_Test $test, Exception $e, $time ) {
46
	}
47
48
	/**
49
	 * @see PHPUnit_Framework_TestListener::addFailure
50
	 */
51
	public function addFailure( PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time ) {
52
	}
53
54
	/**
55
	 * @see PHPUnit_Framework_TestListener::addError
56
	 */
57
	public function addIncompleteTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
58
	}
59
60
	/**
61
	 * @see PHPUnit_Framework_TestListener::addRiskyTest
62
	 * @since 4.0.0
63
	 */
64
	public function addRiskyTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
65
	}
66
67
	/**
68
	 * @see PHPUnit_Framework_TestListener::addSkippedTest
69
	 */
70
	public function addSkippedTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
71
	}
72
73
	/**
74
	 * @see PHPUnit_Framework_TestListener::startTestSuite
75
	 */
76
	public function startTestSuite( PHPUnit_Framework_TestSuite $suite ) {
77
	}
78
79
	/**
80
	 * @see PHPUnit_Framework_TestListener::endTestSuite
81
	 */
82
	public function endTestSuite( PHPUnit_Framework_TestSuite $suite ) {
83
		foreach ( $this->testCollector as $name => $length ) {
84
			print ( "\n" . $suite->getName() . " {$name} ran for {$length} seconds" . "\n" );
85
			unset( $this->testCollector[$name] );
86
		}
87
	}
88
89
}
90