1<?php
2/*
3 * This file is part of the PHP_Timer package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11use PHPUnit\Framework\TestCase;
12
13class PHP_TimerTest extends TestCase
14{
15    /**
16     * @covers PHP_Timer::start
17     * @covers PHP_Timer::stop
18     */
19    public function testStartStop()
20    {
21        $this->assertInternalType('float', PHP_Timer::stop());
22    }
23
24    /**
25     * @covers       PHP_Timer::secondsToTimeString
26     * @dataProvider secondsProvider
27     */
28    public function testSecondsToTimeString($string, $seconds)
29    {
30        $this->assertEquals(
31            $string,
32            PHP_Timer::secondsToTimeString($seconds)
33        );
34    }
35
36    /**
37     * @covers PHP_Timer::timeSinceStartOfRequest
38     */
39    public function testTimeSinceStartOfRequest()
40    {
41        $this->assertStringMatchesFormat(
42            '%f %s',
43            PHP_Timer::timeSinceStartOfRequest()
44        );
45    }
46
47
48    /**
49     * @covers PHP_Timer::resourceUsage
50     */
51    public function testResourceUsage()
52    {
53        $this->assertStringMatchesFormat(
54            'Time: %s, Memory: %fMB',
55            PHP_Timer::resourceUsage()
56        );
57    }
58
59    public function secondsProvider()
60    {
61        return array(
62          array('0 ms', 0),
63          array('1 ms', .001),
64          array('10 ms', .01),
65          array('100 ms', .1),
66          array('999 ms', .999),
67          array('1 second', .9999),
68          array('1 second', 1),
69          array('2 seconds', 2),
70          array('59.9 seconds', 59.9),
71          array('59.99 seconds', 59.99),
72          array('59.99 seconds', 59.999),
73          array('1 minute', 59.9999),
74          array('59 seconds', 59.001),
75          array('59.01 seconds', 59.01),
76          array('1 minute', 60),
77          array('1.01 minutes', 61),
78          array('2 minutes', 120),
79          array('2.01 minutes', 121),
80          array('59.99 minutes', 3599.9),
81          array('59.99 minutes', 3599.99),
82          array('59.99 minutes', 3599.999),
83          array('1 hour', 3599.9999),
84          array('59.98 minutes', 3599.001),
85          array('59.98 minutes', 3599.01),
86          array('1 hour', 3600),
87          array('1 hour', 3601),
88          array('1 hour', 3601.9),
89          array('1 hour', 3601.99),
90          array('1 hour', 3601.999),
91          array('1 hour', 3601.9999),
92          array('1.01 hours', 3659.9999),
93          array('1.01 hours', 3659.001),
94          array('1.01 hours', 3659.01),
95          array('2 hours', 7199.9999),
96        );
97    }
98}
99