|
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 addDays(int $value = 1) Add days (the $value count passed in) to the instance (using date interval). |
|
12
|
|
|
* @method static self addDay() Add one day to the instance (using date interval). |
|
13
|
|
|
* @method static self subDays(int $value = 1) Sub days (the $value count passed in) to the instance (using date interval). |
|
14
|
|
|
* @method static self subDay() Sub one day to the instance (using date interval). |
|
15
|
|
|
* @method static self addHours(int $value = 1) Add hours (the $value count passed in) to the instance (using date interval). |
|
16
|
|
|
* @method static self addHour() Add one hour to the instance (using date interval). |
|
17
|
|
|
* @method static self subHours(int $value = 1) Sub hours (the $value count passed in) to the instance (using date interval). |
|
18
|
|
|
* @method static self subHour() Sub one hour to the instance (using date interval). |
|
19
|
|
|
* @method static self addMinutes(int $value = 1) Add minutes (the $value count passed in) to the instance (using date interval). |
|
20
|
|
|
* @method static self addMinute() Add one minute to the instance (using date interval). |
|
21
|
|
|
* @method static self subMinutes(int $value = 1) Sub minutes (the $value count passed in) to the instance (using date interval). |
|
22
|
|
|
* @method static self subMinute() Sub one minute to the instance (using date interval). |
|
23
|
|
|
* @method static self addSeconds(int $value = 1) Add seconds (the $value count passed in) to the instance (using date interval). |
|
24
|
|
|
* @method static self addSecond() Add one second to the instance (using date interval). |
|
25
|
|
|
* @method static self subSeconds(int $value = 1) Sub seconds (the $value count passed in) to the instance (using date interval). |
|
26
|
|
|
* @method static self subSecond() Sub one second to the instance (using date interval). |
|
27
|
|
|
* @method static self addMillis(int $value = 1) Add milliseconds (the $value count passed in) to the instance (using date interval). |
|
28
|
|
|
* @method static self addMilli() Add one millisecond to the instance (using date interval). |
|
29
|
|
|
* @method static self subMillis(int $value = 1) Sub milliseconds (the $value count passed in) to the instance (using date interval). |
|
30
|
|
|
* @method static self subMilli() Sub one millisecond to the instance (using date interval). |
|
31
|
|
|
* @method static self addMilliseconds(int $value = 1) Add milliseconds (the $value count passed in) to the instance (using date interval). |
|
32
|
|
|
* @method static self addMillisecond() Add one millisecond to the instance (using date interval). |
|
33
|
|
|
* @method static self subMilliseconds(int $value = 1) Sub milliseconds (the $value count passed in) to the instance (using date interval). |
|
34
|
|
|
* @method static self subMillisecond() Sub one millisecond to the instance (using date interval). |
|
35
|
|
|
* @method static self addMicros(int $value = 1) Add microseconds (the $value count passed in) to the instance (using date interval). |
|
36
|
|
|
* @method static self addMicro() Add one microsecond to the instance (using date interval). |
|
37
|
|
|
* @method static self subMicros(int $value = 1) Sub microseconds (the $value count passed in) to the instance (using date interval). |
|
38
|
|
|
* @method static self subMicro() Sub one microsecond to the instance (using date interval). |
|
39
|
|
|
* @method static self addMicroseconds(int $value = 1) Add microseconds (the $value count passed in) to the instance (using date interval). |
|
40
|
|
|
* @method static self addMicrosecond() Add one microsecond to the instance (using date interval). |
|
41
|
|
|
* @method static self subMicroseconds(int $value = 1) Sub microseconds (the $value count passed in) to the instance (using date interval). |
|
42
|
|
|
* @method static self subMicrosecond() Sub one microsecond to the instance (using date interval). |
|
43
|
|
|
* @method static self addYears(int $value = 1) Add years (the $value count passed in) to the instance (using date interval). |
|
44
|
|
|
* @method static self addYear() Add one year to the instance (using date interval). |
|
45
|
|
|
* @method static self subYears(int $value = 1) Sub years (the $value count passed in) to the instance (using date interval). |
|
46
|
|
|
* @method static self subYear() Sub one year to the instance (using date interval). |
|
47
|
|
|
*/ |
|
48
|
|
|
class TestTime |
|
49
|
|
|
{ |
|
50
|
|
|
public static function freeze(): Carbon |
|
51
|
|
|
{ |
|
52
|
|
|
$frozenTime = static::getCarbon(func_get_args()); |
|
53
|
|
|
|
|
54
|
|
|
Carbon::setTestNow($frozenTime); |
|
55
|
|
|
|
|
56
|
|
|
return $frozenTime; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
public function __call($name, $arguments) |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->__callStatic($name, $arguments); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function __callStatic($name, $arguments) |
|
67
|
|
|
{ |
|
68
|
|
|
$result = (new Carbon)->$name(...$arguments); |
|
69
|
|
|
|
|
70
|
|
|
if (! $result instanceof Carbon) { |
|
71
|
|
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
Carbon::setTestNow($result); |
|
75
|
|
|
|
|
76
|
|
|
return new TestTime(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected static function getCarbon(array $args): Carbon |
|
80
|
|
|
{ |
|
81
|
|
|
if (count($args) === 0) { |
|
82
|
|
|
return Carbon::now(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (count($args) === 1) { |
|
86
|
|
|
if (! $args[0] instanceof Carbon) { |
|
87
|
|
|
throw new InvalidArgumentException('You must pass a Carbon instance to `freeze`'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $args[0]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (count($args) === 2) { |
|
94
|
|
|
return Carbon::createFromFormat($args[0], $args[1]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
throw new InvalidArgumentException('You can only pass a maximum of two arguments to `freeze`'); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|