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
<?php
/*
* This file is part of phpunit/php-timer.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Timer;
use PHPUnit\Framework\TestCase;
/**
* @covers \SebastianBergmann\Timer\Timer
*/
class TimerTest extends TestCase
{
public function testStartStop(): void
{
$this->assertInternalType('float', Timer::stop());
}
/**
* @dataProvider secondsProvider
*/
public function testSecondsToTimeString(string $string, string $seconds): void
{
$this->assertEquals(
$string,
Timer::secondsToTimeString($seconds)
);
}
public function testTimeSinceStartOfRequest(): void
{
$this->assertStringMatchesFormat(
'%f %s',
Timer::timeSinceStartOfRequest()
);
}
public function testTimeSinceStartOfRequest2(): void
{
if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
unset($_SERVER['REQUEST_TIME_FLOAT']);
}
$this->assertStringMatchesFormat(
'%f %s',
Timer::timeSinceStartOfRequest()
);
}
/**
* @backupGlobals enabled
*/
public function testTimeSinceStartOfRequest3(): void
{
if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
unset($_SERVER['REQUEST_TIME_FLOAT']);
}
if (isset($_SERVER['REQUEST_TIME'])) {
unset($_SERVER['REQUEST_TIME']);
}
$this->expectException(RuntimeException::class);
Timer::timeSinceStartOfRequest();
}
public function testResourceUsage(): void
{
$this->assertStringMatchesFormat(
'Time: %s, Memory: %fMB',
Timer::resourceUsage()
);
}
public function secondsProvider()
{
return [
['0 ms', 0],
['1 ms', .001],
['10 ms', .01],
['100 ms', .1],
['999 ms', .999],
['1 second', .9999],
['1 second', 1],
['2 seconds', 2],
['59.9 seconds', 59.9],
['59.99 seconds', 59.99],
['59.99 seconds', 59.999],
['1 minute', 59.9999],
['59 seconds', 59.001],
['59.01 seconds', 59.01],
['1 minute', 60],
['1.01 minutes', 61],
['2 minutes', 120],
['2.01 minutes', 121],
['59.99 minutes', 3599.9],
['59.99 minutes', 3599.99],
['59.99 minutes', 3599.999],
['1 hour', 3599.9999],
['59.98 minutes', 3599.001],
['59.98 minutes', 3599.01],
['1 hour', 3600],
['1 hour', 3601],
['1 hour', 3601.9],
['1 hour', 3601.99],
['1 hour', 3601.999],
['1 hour', 3601.9999],
['1.01 hours', 3659.9999],
['1.01 hours', 3659.001],
['1.01 hours', 3659.01],
['2 hours', 7199.9999],
];
}
}