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