forked from tursodatabase/libsql-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionCastingTest.php
68 lines (51 loc) · 1.98 KB
/
CollectionCastingTest.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
<?php
use Illuminate\Support\Facades\Schema;
beforeEach(function () {
Schema::create('collection_casting_table', function ($table) {
$table->id();
$table->json('data');
});
});
afterEach(function () {
Schema::dropIfExists('collection_casting_table');
});
class CollectionCastingModel extends \Illuminate\Database\Eloquent\Model
{
protected $table = 'collection_casting_table';
protected $guarded = false;
public $timestamps = false;
protected function casts(): array
{
return [
'data' => 'collection',
];
}
}
test('it can insert a new record using Eloquent ORM', function () {
$data = collect(['name' => 'John Doe', 'city' => 'New York']);
CollectionCastingModel::create([
'data' => $data,
]);
$result = CollectionCastingModel::first();
expect($result->id)->toBe(1)
->and(get_class($result->data))->toBe('Illuminate\Support\Collection')
->and($result->data->toArray())->toBe($data->toArray())
->and($result->data->get('name'))->toBe('John Doe')
->and($result->data->get('city'))->toBe('New York');
})->group('CollectionCastingTest', 'EloquentAttributeCastings', 'FeatureTest');
test('it can update an existing record using Eloquent ORM', function () {
$data = collect(['name' => 'John Doe', 'city' => 'New York']);
CollectionCastingModel::create([
'data' => $data,
]);
$newData = collect(['name' => 'Jane Doe', 'city' => 'Los Angeles']);
CollectionCastingModel::first()->update([
'data' => $newData,
]);
$result = CollectionCastingModel::first();
expect($result->id)->toBe(1)
->and(get_class($result->data))->toBe('Illuminate\Support\Collection')
->and($result->data->toArray())->toBe($newData->toArray())
->and($result->data->get('name'))->toBe('Jane Doe')
->and($result->data->get('city'))->toBe('Los Angeles');
})->group('CollectionCastingTest', 'EloquentAttributeCastings', 'FeatureTest');