|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\TestTime; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @mixin \Carbon\Carbon |
|
10
|
|
|
* |
|
11
|
|
|
* @method static self create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) Create a new Carbon instance from a specific date and time. |
|
12
|
|
|
* @method static self createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now. |
|
13
|
|
|
* @method static self createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today. |
|
14
|
|
|
* @method static self createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today. |
|
15
|
|
|
* @method static self createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format. |
|
16
|
|
|
* @method static self years() Set current instance year to the given value. |
|
17
|
|
|
* @method static self year(int $value) Set current instance year to the given value. |
|
18
|
|
|
* @method static self setYears() Set current instance year to the given value. |
|
19
|
|
|
* @method static self setYear(int $value) Set current instance year to the given value. |
|
20
|
|
|
* @method static self months(int $value) Set current instance month to the given value. |
|
21
|
|
|
* @method static self month(int $value) Set current instance month to the given value. |
|
22
|
|
|
* @method static self setMonths(int $value) Set current instance month to the given value. |
|
23
|
|
|
* @method static self setMonth(int $value) Set current instance month to the given value. |
|
24
|
|
|
* @method static self days(int $value) Set current instance day to the given value. |
|
25
|
|
|
* @method static self day(int $value) Set current instance day to the given value. |
|
26
|
|
|
* @method static self setDays(int $value) Set current instance day to the given value. |
|
27
|
|
|
* @method static self setDay(int $value) Set current instance day to the given value. |
|
28
|
|
|
* @method static self hours(int $value) Set current instance hour to the given value. |
|
29
|
|
|
* @method static self hour(int $value) Set current instance hour to the given value. |
|
30
|
|
|
* @method static self setHours(int $value) Set current instance hour to the given value. |
|
31
|
|
|
* @method static self setHour(int $value) Set current instance hour to the given value. |
|
32
|
|
|
* @method static self minutes(int $value) Set current instance minute to the given value. |
|
33
|
|
|
* @method static self minute(int $value) Set current instance minute to the given value. |
|
34
|
|
|
* @method static self setMinutes(int $value) Set current instance minute to the given value. |
|
35
|
|
|
* @method static self setMinute(int $value) Set current instance minute to the given value. |
|
36
|
|
|
* @method static self seconds(int $value) Set current instance second to the given value. |
|
37
|
|
|
* @method static self second(int $value) Set current instance second to the given value. |
|
38
|
|
|
* @method static self setSeconds(int $value) Set current instance second to the given value. |
|
39
|
|
|
* @method static self setSecond(int $value) Set current instance second to the given value. |
|
40
|
|
|
* @method static self millis(int $value) Set current instance millisecond to the given value. |
|
41
|
|
|
* @method static self milli(int $value) Set current instance millisecond to the given value. |
|
42
|
|
|
* @method static self setMillis(int $value) Set current instance millisecond to the given value. |
|
43
|
|
|
* @method static self setMilli(int $value) Set current instance millisecond to the given value. |
|
44
|
|
|
* @method static self milliseconds(int $value) Set current instance millisecond to the given value. |
|
45
|
|
|
* @method static self millisecond(int $value) Set current instance millisecond to the given value. |
|
46
|
|
|
* @method static self setMilliseconds(int $value) Set current instance millisecond to the given value. |
|
47
|
|
|
* @method static self setMillisecond(int $value) Set current instance millisecond to the given value. |
|
48
|
|
|
* @method static self micros() Set current instance microsecond to the given value. |
|
49
|
|
|
* @method static self micro(int $value) Set current instance microsecond to the given value. |
|
50
|
|
|
* @method static self setMicros() Set current instance microsecond to the given value. |
|
51
|
|
|
* @method static self setMicro(int $value) Set current instance microsecond to the given value. |
|
52
|
|
|
* @method static self microseconds() Set current instance microsecond to the given value. |
|
53
|
|
|
* @method static self microsecond(int $value) Set current instance microsecond to the given value. |
|
54
|
|
|
* @method static self setMicroseconds() Set current instance microsecond to the given value. |
|
55
|
|
|
* @method static self setMicrosecond(int $value) Set current instance microsecond to the given value. |
|
56
|
|
|
* @method static self addDays(int $value = 1) Add days (the $value count passed in) to the instance (using date interval). |
|
57
|
|
|
* @method static self addDay() Add one day to the instance (using date interval). |
|
58
|
|
|
* @method static self subDays(int $value = 1) Sub days (the $value count passed in) to the instance (using date interval). |
|
59
|
|
|
* @method static self subDay() Sub one day to the instance (using date interval). |
|
60
|
|
|
* @method static self addHours(int $value = 1) Add hours (the $value count passed in) to the instance (using date interval). |
|
61
|
|
|
* @method static self addHour() Add one hour to the instance (using date interval). |
|
62
|
|
|
* @method static self subHours(int $value = 1) Sub hours (the $value count passed in) to the instance (using date interval). |
|
63
|
|
|
* @method static self subHour() Sub one hour to the instance (using date interval). |
|
64
|
|
|
* @method static self addMinutes(int $value = 1) Add minutes (the $value count passed in) to the instance (using date interval). |
|
65
|
|
|
* @method static self addMinute() Add one minute to the instance (using date interval). |
|
66
|
|
|
* @method static self subMinutes(int $value = 1) Sub minutes (the $value count passed in) to the instance (using date interval). |
|
67
|
|
|
* @method static self subMinute() Sub one minute to the instance (using date interval). |
|
68
|
|
|
* @method static self addSeconds(int $value = 1) Add seconds (the $value count passed in) to the instance (using date interval). |
|
69
|
|
|
* @method static self addSecond() Add one second to the instance (using date interval). |
|
70
|
|
|
* @method static self subSeconds(int $value = 1) Sub seconds (the $value count passed in) to the instance (using date interval). |
|
71
|
|
|
* @method static self subSecond() Sub one second to the instance (using date interval). |
|
72
|
|
|
* @method static self addMillis(int $value = 1) Add milliseconds (the $value count passed in) to the instance (using date interval). |
|
73
|
|
|
* @method static self addMilli() Add one millisecond to the instance (using date interval). |
|
74
|
|
|
* @method static self subMillis(int $value = 1) Sub milliseconds (the $value count passed in) to the instance (using date interval). |
|
75
|
|
|
* @method static self subMilli() Sub one millisecond to the instance (using date interval). |
|
76
|
|
|
* @method static self addMilliseconds(int $value = 1) Add milliseconds (the $value count passed in) to the instance (using date interval). |
|
77
|
|
|
* @method static self addMillisecond() Add one millisecond to the instance (using date interval). |
|
78
|
|
|
* @method static self subMilliseconds(int $value = 1) Sub milliseconds (the $value count passed in) to the instance (using date interval). |
|
79
|
|
|
* @method static self subMillisecond() Sub one millisecond to the instance (using date interval). |
|
80
|
|
|
* @method static self addMicros(int $value = 1) Add microseconds (the $value count passed in) to the instance (using date interval). |
|
81
|
|
|
* @method static self addMicro() Add one microsecond to the instance (using date interval). |
|
82
|
|
|
* @method static self subMicros(int $value = 1) Sub microseconds (the $value count passed in) to the instance (using date interval). |
|
83
|
|
|
* @method static self subMicro() Sub one microsecond to the instance (using date interval). |
|
84
|
|
|
* @method static self addMicroseconds(int $value = 1) Add microseconds (the $value count passed in) to the instance (using date interval). |
|
85
|
|
|
* @method static self addMicrosecond() Add one microsecond to the instance (using date interval). |
|
86
|
|
|
* @method static self subMicroseconds(int $value = 1) Sub microseconds (the $value count passed in) to the instance (using date interval). |
|
87
|
|
|
* @method static self subMicrosecond() Sub one microsecond to the instance (using date interval). |
|
88
|
|
|
* @method static self addMonths(int $value = 1) Add months (the $value count passed in) to the instance (using date interval). |
|
89
|
|
|
* @method static self addMonth() Add one month to the instance (using date interval). |
|
90
|
|
|
* @method static self subMonths(int $value = 1) Sub months (the $value count passed in) to the instance (using date interval). |
|
91
|
|
|
* @method static self subMonth() Sub one month to the instance (using date interval). |
|
92
|
|
|
* @method static self addYears(int $value = 1) Add years (the $value count passed in) to the instance (using date interval). |
|
93
|
|
|
* @method static self addYear() Add one year to the instance (using date interval). |
|
94
|
|
|
* @method static self subYears(int $value = 1) Sub years (the $value count passed in) to the instance (using date interval). |
|
95
|
|
|
* @method static self subYear() Sub one year to the instance (using date interval). |
|
96
|
|
|
*/ |
|
97
|
|
|
class TestTime |
|
98
|
|
|
{ |
|
99
|
|
|
public static function freeze(): Carbon |
|
100
|
|
|
{ |
|
101
|
|
|
$frozenTime = static::getCarbon(func_get_args()); |
|
102
|
|
|
|
|
103
|
|
|
Carbon::setTestNow($frozenTime); |
|
104
|
|
|
|
|
105
|
|
|
return $frozenTime; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public static function freezeAtSecond(): Carbon |
|
109
|
|
|
{ |
|
110
|
|
|
$frozenTime = static::getCarbon(func_get_args())->startOfSecond(); |
|
111
|
|
|
|
|
112
|
|
|
return static::freeze($frozenTime); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function __call($name, $arguments) |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->__callStatic($name, $arguments); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public static function __callStatic($name, $arguments) |
|
121
|
|
|
{ |
|
122
|
|
|
$result = (new Carbon)->$name(...$arguments); |
|
123
|
|
|
|
|
124
|
|
|
if (! $result instanceof Carbon) { |
|
125
|
|
|
return $result; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
Carbon::setTestNow($result); |
|
129
|
|
|
|
|
130
|
|
|
return new TestTime(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
protected static function getCarbon(array $args): Carbon |
|
134
|
|
|
{ |
|
135
|
|
|
if (count($args) === 0) { |
|
136
|
|
|
return Carbon::now(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if (count($args) === 1) { |
|
140
|
|
|
if (! $args[0] instanceof Carbon) { |
|
141
|
|
|
throw new InvalidArgumentException('You must pass a Carbon instance to `freeze`'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $args[0]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if (count($args) === 2) { |
|
148
|
|
|
return Carbon::createFromFormat($args[0], $args[1]); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
throw new InvalidArgumentException('You can only pass a maximum of two arguments to `freeze`'); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|