File: SleepEnvironmentBuilder.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 (134 lines) | stat: -rw-r--r-- 3,899 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

namespace phpmock\environment;

use phpmock\MockBuilder;
use phpmock\functions\FixedDateFunction;
use phpmock\functions\FixedMicrotimeFunction;
use phpmock\functions\SleepFunction;
use phpmock\functions\UsleepFunction;

/**
 * Builds a sleep(), usleep(), date(), time() and microtime() mock environment.
 *
 * In this environment sleep() and usleep() don't sleep for real. Instead
 * they return immediatly and increase the amount of time in the mocks for
 * date(), time() and microtime().
 *
 * Example:
 * <code>
 * namespace foo;
 *
 * use phpmock\environment\SleepEnvironmentBuilder;
 *
 * $builder = new SleepEnvironmentBuilder();
 * $builder->addNamespace(__NAMESPACE__)
 *         ->setTimestamp(1417011228);
 *
 * $environment = $builder->build();
 * $environment->enable();
 *
 * // This won't delay the test for 10 seconds, but increase time().
 * sleep(10);
 * assert(1417011228 + 10 == time());
 *
 * // Now revert the effect so that sleep() and time() are not mocked anymore.
 * $environment->disable();
 * </code>
 *
 * @author Markus Malkusch <markus@malkusch.de>
 * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
 * @license http://www.wtfpl.net/txt/copying/ WTFPL
 */
class SleepEnvironmentBuilder
{
    /**
     * @var array The namespaces for the mock environment.
     */
    private $namespaces;

    /**
     * @var mixed the timestamp.
     */
    private $timestamp;

    /**
     * Add a namespace for the mock environment.
     *
     * @param string $namespace A namespace for the mock environment.
     * @return SleepEnvironmentBuilder
     */
    public function addNamespace($namespace)
    {
        $this->namespaces[] = $namespace;
        return $this;
    }

    /**
     * Sets the mocked timestamp.
     *
     * If not set the mock will use the current time at creation time.
     * The timestamp can be an int, a float with microseconds or a string
     * in the microtime() format.
     *
     * @param mixed $timestamp The timestamp.
     * @return SleepEnvironmentBuilder
     */
    public function setTimestamp($timestamp)
    {
        $this->timestamp = $timestamp;
        return $this;
    }

    /**
     * Builds a sleep(), usleep(), date(), time() and microtime() mock environment.
     *
     * @return MockEnvironment
     */
    public function build()
    {
        $environment = new MockEnvironment();
        $builder = new MockBuilder();

        $incrementables = [];
        foreach ($this->namespaces as $namespace) {
            $builder->setNamespace($namespace);

            // microtime() mock
            $microtime = new FixedMicrotimeFunction($this->timestamp);
            $builder->setName("microtime")
                ->setFunctionProvider($microtime);
            $environment->addMock($builder->build());

            // time() mock
            $builder->setName("time")
                ->setFunction([$microtime, "getTime"]);
            $environment->addMock($builder->build());

            // date() mock
            $date = new FixedDateFunction($this->timestamp);
            $builder->setName("date")
                    ->setFunctionProvider($date);
            $environment->addMock($builder->build());

            $incrementables[] = $microtime;
            $incrementables[] = $date;
        }

        // Need a complete list of $incrementables.
        foreach ($this->namespaces as $namespace) {
            $builder->setNamespace($namespace);
            // sleep() mock
            $builder->setName("sleep")
                ->setFunctionProvider(new SleepFunction($incrementables));
            $environment->addMock($builder->build());

            // usleep() mock
            $builder->setName("usleep")
                ->setFunctionProvider(new UsleepFunction($incrementables));
            $environment->addMock($builder->build());
        }

        return $environment;
    }
}