forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerTest.php
More file actions
124 lines (98 loc) · 3.19 KB
/
Copy pathTimerTest.php
File metadata and controls
124 lines (98 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Debug;
use CodeIgniter\Test\CIUnitTestCase;
/**
* @internal
*/
final class TimerTest extends CIUnitTestCase
{
/**
* We do most of our tests in this one method. While I usually frown
* on this, it's handy here so that we don't stall the tests any
* longer then needed.
*
* @timeLimit 1.5
*/
public function testStoresTimers()
{
$timer = new Timer();
$timer->start('test1');
sleep(1);
$timer->stop('test1');
$timers = $timer->getTimers();
$this->assertCount(1, $timers, 'No timers were stored.');
$this->assertArrayHasKey('test1', $timers, 'No "test1" array found.');
$this->assertArrayHasKey('start', $timers['test1'], 'No "start" value found.');
$this->assertArrayHasKey('end', $timers['test1'], 'No "end" value found.');
// Since the timer has been stopped - it will have a value. In this
// case it should be over 1 second.
$this->assertArrayHasKey('duration', $timers['test1'], 'No duration was calculated.');
$this->assertGreaterThanOrEqual(1.0, $timers['test1']['duration']);
}
/**
* @timeLimit 1.5
*/
public function testAutoCalcsTimerEnd()
{
$timer = new Timer();
$timer->start('test1');
sleep(1);
$timers = $timer->getTimers();
$this->assertArrayHasKey('duration', $timers['test1'], 'No duration was calculated.');
$this->assertGreaterThanOrEqual(1.0, $timers['test1']['duration']);
}
/**
* @timeLimit 1.5
*/
public function testElapsedTimeGivesSameResultAsTimersArray()
{
$timer = new Timer();
$timer->start('test1');
sleep(1);
$timer->stop('test1');
$timers = $timer->getTimers();
$expected = $timers['test1']['duration'];
$this->assertSame($expected, $timer->getElapsedTime('test1'));
}
public function testThrowsExceptionStoppingNonTimer()
{
$this->expectException('RunTimeException');
$timer = new Timer();
$timer->stop('test1');
}
public function testLongExecutionTime()
{
$timer = new Timer();
$timer->start('longjohn', strtotime('-110 minutes'));
$this->assertCloseEnough(110 * 60, $timer->getElapsedTime('longjohn'));
}
public function testLongExecutionTimeThroughCommonFunc()
{
$timer = new Timer();
$timer->start('longjohn', strtotime('-11 minutes'));
$this->assertCloseEnough(11 * 60, $timer->getElapsedTime('longjohn'));
}
/**
* @timeLimit 1.5
*/
public function testCommonStartStop()
{
timer('test1');
sleep(1);
timer('test1');
$this->assertGreaterThanOrEqual(1.0, timer()->getElapsedTime('test1'));
}
public function testReturnsNullGettingElapsedTimeOfNonTimer()
{
$timer = new Timer();
$this->assertNull($timer->getElapsedTime('test1'));
}
}