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