File: FixedMicrotimeFunction.php

package info (click to toggle)
php-mock 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 340 kB
  • sloc: php: 1,740; makefile: 18; xml: 17; sh: 7
file content (111 lines) | stat: -rw-r--r-- 2,928 bytes parent folder | download | duplicates (3)
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
<?php

namespace phpmock\functions;

use InvalidArgumentException;

/**
 * Mock function for microtime which returns always the same time.
 *
 * @author Markus Malkusch <markus@malkusch.de>
 * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
 * @license http://www.wtfpl.net/txt/copying/ WTFPL
 */
class FixedMicrotimeFunction implements FunctionProvider, Incrementable
{
    /**
     * @var string the timestamp in PHP's microtime() string format.
     */
    private $timestamp;

    /**
     * Set the timestamp.
     *
     * @param mixed $timestamp The timestamp, if ommited the current time.
     *
     * @SuppressWarnings(PHPMD)
     */
    public function __construct($timestamp = null)
    {
        if (is_null($timestamp)) {
            $this->setMicrotime(\microtime());
        } elseif (is_string($timestamp)) {
            $this->setMicrotime($timestamp);
        } elseif (is_numeric($timestamp)) {
            $this->setMicrotimeAsFloat($timestamp);
        } else {
            throw new InvalidArgumentException(
                "Timestamp parameter is invalid type."
            );
        }
    }

    /**
     * Returns this object as a callable for the mock function.
     *
     * @return callable The callable for this object.
     */
    public function getCallable()
    {
        return [$this, "getMicrotime"];
    }

    /**
     * Set the timestamp as string.
     *
     * @param string $timestamp The timestamp as string.
     */
    public function setMicrotime($timestamp)
    {
        if (empty($timestamp)) {
            throw new InvalidArgumentException('Timestamp should not be empty');
        }
        $this->timestamp = $timestamp;
    }

    /**
     * Set the timestamp as float.
     *
     * @param float $timestamp The timestamp as float.
     */
    public function setMicrotimeAsFloat($timestamp)
    {
        if (!is_numeric($timestamp)) {
            throw new InvalidArgumentException('Timestamp should be numeric');
        }
        $converter = new MicrotimeConverter();
        $this->timestamp = $converter->convertFloatToString($timestamp);
    }

    /**
     * Returns the microtime.
     *
     * @param bool $get_as_float If true returns timestamp as float, else string
     * @return mixed The value.
     * @SuppressWarnings(PHPMD)
     */
    public function getMicrotime($get_as_float = false)
    {
        if ($get_as_float) {
            $converter = new MicrotimeConverter();
            return $converter->convertStringToFloat($this->timestamp);
        } else {
            return $this->timestamp;
        }
    }

    /**
     * Returns the time without the microseconds.
     *
     * @return int The time.
     */
    public function getTime()
    {
        return (int) $this->getMicrotime(true);
    }

    public function increment($increment)
    {
        $this->setMicrotimeAsFloat($this->getMicrotime(true) + $increment);
    }
}