/
Wormhole.php
144 lines (123 loc) · 3.04 KB
/
Wormhole.php
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
<?php
namespace Illuminate\Foundation\Testing;
use Illuminate\Support\Carbon;
class Wormhole
{
/**
* The amount of time to travel.
*
* @var int
*/
public $value;
/**
* Create a new wormhole instance.
*
* @param int $value
* @return void
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* Travel forward the given number of milliseconds.
*
* @param callable|null $callback
* @return mixed
*/
public function milliseconds($callback = null)
{
Carbon::setTestNow(Carbon::now()->addMilliseconds($this->value));
return $this->handleCallback($callback);
}
/**
* Travel forward the given number of seconds.
*
* @param callable|null $callback
* @return mixed
*/
public function seconds($callback = null)
{
Carbon::setTestNow(Carbon::now()->addSeconds($this->value));
return $this->handleCallback($callback);
}
/**
* Travel forward the given number of minutes.
*
* @param callable|null $callback
* @return mixed
*/
public function minutes($callback = null)
{
Carbon::setTestNow(Carbon::now()->addMinutes($this->value));
return $this->handleCallback($callback);
}
/**
* Travel forward the given number of hours.
*
* @param callable|null $callback
* @return mixed
*/
public function hours($callback = null)
{
Carbon::setTestNow(Carbon::now()->addHours($this->value));
return $this->handleCallback($callback);
}
/**
* Travel forward the given number of days.
*
* @param callable|null $callback
* @return mixed
*/
public function days($callback = null)
{
Carbon::setTestNow(Carbon::now()->addDays($this->value));
return $this->handleCallback($callback);
}
/**
* Travel forward the given number of weeks.
*
* @param callable|null $callback
* @return mixed
*/
public function weeks($callback = null)
{
Carbon::setTestNow(Carbon::now()->addWeeks($this->value));
return $this->handleCallback($callback);
}
/**
* Travel forward the given number of years.
*
* @param callable|null $callback
* @return mixed
*/
public function years($callback = null)
{
Carbon::setTestNow(Carbon::now()->addYears($this->value));
return $this->handleCallback($callback);
}
/**
* Travel back to the current time.
*
* @return \DateTimeInterface
*/
public static function back()
{
Carbon::setTestNow();
return Carbon::now();
}
/**
* Handle the given optional execution callback.
*
* @param callable|null $callback
* @return mixed
*/
protected function handleCallback($callback)
{
if ($callback) {
return tap($callback(), function () {
Carbon::setTestNow();
});
}
}
}