-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
ProjectionTest.php
157 lines (109 loc) · 3.7 KB
/
ProjectionTest.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace Spatie\EventSourcing\Tests;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertFalse;
use function PHPUnit\Framework\assertTrue;
use Spatie\EventSourcing\Projections\Exceptions\ReadonlyProjection;
use Spatie\EventSourcing\Tests\TestClasses\Models\ProjectionModel;
function createProjection(): ProjectionModel
{
ProjectionModel::new()->writeable()->create([
'uuid' => 'test-uuid',
'field' => 'original',
]);
return ProjectionModel::find('test-uuid');
}
function assertNothingChanged(): void
{
test()->assertDatabaseHas((new ProjectionModel())->getTable(), [
'uuid' => 'test-uuid',
'field' => 'original',
]);
}
beforeEach(function () {
Schema::dropIfExists('projection_models');
Schema::create('projection_models', function (Blueprint $table): void {
(new ProjectionModel())->getBlueprint($table);
});
});
it('can create', function () {
expect(function () {
ProjectionModel::create([
'uuid' => 'test-uuid',
'field' => 'test',
]);
})->toThrow(ReadonlyProjection::class);
$this->assertDatabaseCount((new ProjectionModel())->getTable(), 0);
$model = ProjectionModel::new()->writeable()->create([
'uuid' => 'test-uuid-2',
'field' => 'test',
]);
assertTrue($model->exists);
});
it('can save', function () {
$projection = createProjection();
expect(function () use ($projection) {
$projection->field = 'changed';
$projection->save();
})->toThrow(ReadonlyProjection::class);
assertNothingChanged();
$projection->field = 'changed';
$projection->writeable()->save();
assertEquals('changed', $projection->refresh()->field);
});
it('can update', function () {
$projection = createProjection();
expect(function () use ($projection) {
$projection->update([
'field' => 'changed',
]);
})->toThrow(ReadonlyProjection::class);
assertNothingChanged();
$projection->writeable()->update([
'field' => 'changed',
]);
assertEquals('changed', $projection->refresh()->field);
});
it('can delete', function () {
$projection = createProjection();
expect(fn () => $projection->delete())->toThrow(ReadonlyProjection::class);
assertNothingChanged();
$projection->writeable()->delete();
assertEquals(0, ProjectionModel::all()->count());
});
it('can force delete', function () {
$projection = createProjection();
expect(fn () => $projection->forceDelete())->toThrow(ReadonlyProjection::class);
assertNothingChanged();
$projection->writeable()->forceDelete();
assertEquals(0, ProjectionModel::all()->count());
});
it('can force fill', function () {
$projection = createProjection();
expect(function () use ($projection) {
$projection->forceFill([
'field' => 'changed',
])->save();
})->toThrow(ReadonlyProjection::class);
assertNothingChanged();
$projection->writeable()->forceFill([
'field' => 'changed',
])->save();
assertEquals('changed', $projection->refresh()->field);
});
it('should reset is writeable on refresh', function () {
$projection = createProjection();
$projection = $projection->writeable();
assertFalse($projection->refresh()->isWriteable());
});
it('should reset is writeable on fresh', function () {
$projection = createProjection();
$projection = $projection->writeable();
assertFalse($projection->fresh()->isWriteable());
});
it('can read', function () {
createProjection();
assertEquals(1, ProjectionModel::all()->count());
});