forked from tursodatabase/libsql-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatetimeCastingTest.php
115 lines (84 loc) · 3.51 KB
/
DatetimeCastingTest.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
<?php
use Illuminate\Support\Facades\Schema;
beforeEach(function () {
Schema::create('datetime_casting_table', function ($table) {
$table->id();
$table->dateTime('started_at');
});
});
afterEach(function () {
Schema::dropIfExists('datetime_casting_table');
});
class DatetimeCastingModel extends \Illuminate\Database\Eloquent\Model
{
protected $table = 'datetime_casting_table';
protected $guarded = false;
public $timestamps = false;
protected function casts(): array
{
return [
'started_at' => 'datetime',
];
}
}
test('it can insert a new record using Eloquent ORM', function () {
$startedAt = '2021-01-01 12:00:00';
DatetimeCastingModel::create([
'started_at' => $startedAt,
]);
$result = DatetimeCastingModel::first();
expect($result->id)->toBe(1)
->and(get_class($result->started_at))->toBe('Illuminate\Support\Carbon')
->and($result->started_at->format('Y-m-d H:i:s'))->toBe($startedAt);
})->group('DatetimeCastingTest', 'EloquentAttributeCastings', 'FeatureTest');
test('it can update an existing record using Eloquent ORM', function () {
$startedAt = '2021-01-01 12:00:00';
DatetimeCastingModel::create([
'started_at' => $startedAt,
]);
$newStartedAt = '2021-01-01 13:00:00';
DatetimeCastingModel::first()->update([
'started_at' => $newStartedAt,
]);
$result = DatetimeCastingModel::first();
expect($result->id)->toBe(1)
->and(get_class($result->started_at))->toBe('Illuminate\Support\Carbon')
->and($result->started_at->format('Y-m-d H:i:s'))->toBe($newStartedAt);
})->group('DatetimeCastingTest', 'EloquentAttributeCastings', 'FeatureTest');
test('it can insert a new record using Eloquent ORM with Carbon instance', function () {
$startedAt = now();
DatetimeCastingModel::create([
'started_at' => $startedAt,
]);
$result = DatetimeCastingModel::first();
expect($result->id)->toBe(1)
->and(get_class($result->started_at))->toBe('Illuminate\Support\Carbon')
->and($result->started_at->format('Y-m-d H:i:s'))->toBe($startedAt->format('Y-m-d H:i:s'));
})->group('DatetimeCastingTest', 'EloquentAttributeCastings', 'FeatureTest');
test('it can update an existing record using Eloquent ORM with Carbon instance', function () {
$startedAt = now();
DatetimeCastingModel::create([
'started_at' => $startedAt,
]);
$newStartedAt = now()->addHour();
DatetimeCastingModel::first()->update([
'started_at' => $newStartedAt,
]);
$result = DatetimeCastingModel::first();
expect($result->id)->toBe(1)
->and(get_class($result->started_at))->toBe('Illuminate\Support\Carbon')
->and($result->started_at->format('Y-m-d H:i:s'))->toBe($newStartedAt->format('Y-m-d H:i:s'));
})->group('DatetimeCastingTest', 'EloquentAttributeCastings', 'FeatureTest');
test('it can find the saved record using Eloquent ORM', function () {
$startedAt = now();
DatetimeCastingModel::create([
'started_at' => now()->subHour(),
]);
DatetimeCastingModel::create([
'started_at' => $startedAt,
]);
$found = DatetimeCastingModel::where('started_at', $startedAt)->first();
expect($found->id)->toBe(2)
->and(get_class($found->started_at))->toBe('Illuminate\Support\Carbon')
->and($found->started_at->format('Y-m-d H:i:s'))->toBe($startedAt->format('Y-m-d H:i:s'));
})->group('DatetimeCastingTest', 'EloquentAttributeCastings', 'FeatureTest');