forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupportCarbonTest.php
More file actions
111 lines (87 loc) · 3.08 KB
/
SupportCarbonTest.php
File metadata and controls
111 lines (87 loc) · 3.08 KB
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 Illuminate\Tests\Support;
use BadMethodCallException;
use Carbon\Carbon as BaseCarbon;
use DateTime;
use DateTimeInterface;
use Illuminate\Support\Carbon;
use PHPUnit\Framework\TestCase;
class SupportCarbonTest extends TestCase
{
/**
* @var \Illuminate\Support\Carbon
*/
protected $now;
protected function setUp(): void
{
parent::setUp();
Carbon::setTestNow($this->now = Carbon::create(2017, 6, 27, 13, 14, 15, 'UTC'));
}
protected function tearDown(): void
{
Carbon::setTestNow();
Carbon::serializeUsing(null);
parent::tearDown();
}
public function testInstance()
{
$this->assertInstanceOf(DateTime::class, $this->now);
$this->assertInstanceOf(DateTimeInterface::class, $this->now);
$this->assertInstanceOf(BaseCarbon::class, $this->now);
$this->assertInstanceOf(Carbon::class, $this->now);
}
public function testCarbonIsMacroableWhenNotCalledStatically()
{
Carbon::macro('diffInDecades', function (Carbon $dt = null, $abs = true) {
return (int) ($this->diffInYears($dt, $abs) / 10);
});
$this->assertSame(2, $this->now->diffInDecades(Carbon::now()->addYears(25)));
}
public function testCarbonIsMacroableWhenCalledStatically()
{
Carbon::macro('twoDaysAgoAtNoon', function () {
return Carbon::now()->subDays(2)->setTime(12, 0, 0);
});
$this->assertSame('2017-06-25 12:00:00', Carbon::twoDaysAgoAtNoon()->toDateTimeString());
}
public function testCarbonRaisesExceptionWhenStaticMacroIsNotFound()
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('nonExistingStaticMacro does not exist.');
Carbon::nonExistingStaticMacro();
}
public function testCarbonRaisesExceptionWhenMacroIsNotFound()
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('nonExistingMacro does not exist.');
Carbon::now()->nonExistingMacro();
}
public function testCarbonAllowsCustomSerializer()
{
Carbon::serializeUsing(function (Carbon $carbon) {
return $carbon->getTimestamp();
});
$result = json_decode(json_encode($this->now), true);
$this->assertSame(1498569255, $result);
}
public function testCarbonCanSerializeToJson()
{
$this->assertSame('2017-06-27T13:14:15.000000Z', $this->now->jsonSerialize());
}
public function testSetStateReturnsCorrectType()
{
$carbon = Carbon::__set_state([
'date' => '2017-06-27 13:14:15.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
]);
$this->assertInstanceOf(Carbon::class, $carbon);
}
public function testDeserializationOccursCorrectly()
{
$carbon = new Carbon('2017-06-27 13:14:15.000000');
$serialized = 'return '.var_export($carbon, true).';';
$deserialized = eval($serialized);
$this->assertInstanceOf(Carbon::class, $deserialized);
}
}