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
224 lines (177 loc) · 6.17 KB
/
Copy pathTimerTest.php
File metadata and controls
224 lines (177 loc) · 6.17 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?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 ArgumentCountError;
use CodeIgniter\Test\CIUnitTestCase;
use ErrorException;
use RuntimeException;
/**
* @internal
*
* @group Others
*/
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');
}
/**
* This test might fail if your timezone has Daylight Saving Time.
* See https://github.com/codeigniter4/CodeIgniter4/issues/6823
*/
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'));
}
public function testRecordFunctionNoReturn()
{
$timer = new Timer();
$returnValue = $timer->record('longjohn', static function () { usleep(100000); });
$this->assertGreaterThanOrEqual(0.1, $timer->getElapsedTime('longjohn'));
$this->assertNull($returnValue);
}
public function testRecordFunctionWithReturn()
{
$timer = new Timer();
$returnValue = $timer->record('longjohn', static function () {
usleep(100000);
return 'test';
});
$this->assertGreaterThanOrEqual(0.1, $timer->getElapsedTime('longjohn'));
$this->assertSame('test', $returnValue);
}
public function testRecordArrowFunction()
{
$timer = new Timer();
$returnValue = $timer->record('longjohn', static fn () => strlen('CI4'));
$this->assertLessThan(0.1, $timer->getElapsedTime('longjohn'));
$this->assertSame(3, $returnValue);
}
public function testRecordThrowsException()
{
$this->expectException(RuntimeException::class);
$timer = new Timer();
$timer->record('ex', static function () { throw new RuntimeException(); });
}
public function testRecordThrowsErrorOnCallableWithParams()
{
if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
$this->expectException(ArgumentCountError::class);
} else {
$this->expectException(ErrorException::class);
}
$timer = new Timer();
$timer->record('error', 'strlen');
}
public function testCommonNoNameExpectTimer()
{
$returnValue = timer();
$this->assertInstanceOf(Timer::class, $returnValue);
}
public function testCommonWithNameExpectTimer()
{
$returnValue = timer('test');
$this->assertInstanceOf(Timer::class, $returnValue);
$this->assertTrue($returnValue->has('test'));
}
public function testCommonNoNameCallableExpectTimer()
{
$returnValue = timer(null, static fn () => strlen('CI4'));
$this->assertInstanceOf(Timer::class, $returnValue);
}
public function testCommonCallableExpectNoReturn()
{
$returnValue = timer('common', static function () { usleep(100000); });
$this->assertNotInstanceOf(Timer::class, $returnValue);
$this->assertNull($returnValue);
$this->assertGreaterThanOrEqual(0.1, timer()->getElapsedTime('common'));
}
public function testCommonCallableExpectWithReturn()
{
$returnValue = timer('common', static fn () => strlen('CI4'));
$this->assertNotInstanceOf(Timer::class, $returnValue);
$this->assertSame(3, $returnValue);
$this->assertLessThanOrEqual(0.1, timer()->getElapsedTime('common'));
}
}