-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityHistoryTest.php
More file actions
263 lines (221 loc) · 9.5 KB
/
Copy pathEntityHistoryTest.php
File metadata and controls
263 lines (221 loc) · 9.5 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/*
* Copyright ©2024 Robert Landers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// namespace Bottledcode\DurablePhp\Tests\Unit;
use Bottledcode\DurablePhp\Contexts\AuthContext\SecurityException;
use Bottledcode\DurablePhp\Events\AwaitResult;
use Bottledcode\DurablePhp\Events\RaiseEvent;
use Bottledcode\DurablePhp\Events\TaskCompleted;
use Bottledcode\DurablePhp\Events\WithEntity;
use Bottledcode\DurablePhp\Events\WithLock;
use Bottledcode\DurablePhp\State\EntityState;
use Bottledcode\DurablePhp\State\Ids\StateId;
use function Bottledcode\DurablePhp\OrchestrationInstance;
it('knows if it has applied an event', function (): void {
$history = getEntityHistory();
processEvent($event = new RaiseEvent('test', 'test', []), $history->applyRaiseEvent(...));
expect($history->hasAppliedEvent($event))->toBeTrue();
});
test('acking an event removes it from history', function (): void {
$history = getEntityHistory();
processEvent($event = new RaiseEvent('test', 'test', []), $history->applyRaiseEvent(...));
$history->ackedEvent($event);
expect($history->hasAppliedEvent($event))->toBeFalse();
});
it('processes signals', function (): void {
$called = 0;
$outerCall = function () use (&$called): void {
$called++;
};
$history = getEntityHistory(
new class ($outerCall) extends EntityState {
public function __construct(public $outerCall) {}
public function signal(): void
{
($this->outerCall)();
}
},
);
$history->from = StateId::fromInstance(OrchestrationInstance('test', 'test'));
processEvent(
new RaiseEvent('id', '__signal', ['operation' => 'signal', 'input' => []]),
$history->applyRaiseEvent(...),
);
expect($called)->toBe(1);
});
it('only processes locked events', function (): void {
$called = 0;
$outerCall = function () use (&$called): void {
$called++;
};
$history = getEntityHistory(
new class ($outerCall) extends EntityState {
public function __construct(public $outerCall) {}
public function signal(): void
{
($this->outerCall)();
}
},
);
$history->from = StateId::fromInstance(OrchestrationInstance('test', 'test'));
$owner = StateId::fromInstance(OrchestrationInstance('owner', 'owner'));
$other = StateId::fromInstance(OrchestrationInstance('other', 'other'));
$lockResult = processEvent(
AwaitResult::forEvent(
$owner,
WithLock::onEntity($owner, RaiseEvent::forLockNotification($owner), $history->id),
),
$history->applyRaiseEvent(...),
);
expect($lockResult)->toHaveCount(2);
$result = processEvent(
WithLock::onEntity($owner, AwaitResult::forEvent($owner, RaiseEvent::forOperation('signal', [])), $history->id),
$history->applyRaiseEvent(...),
);
$otherResult = processEvent(
$waiting = AwaitResult::forEvent($other, RaiseEvent::forOperation('signal', [])),
$history->applyRaiseEvent(...),
);
expect($called)->toBe(1);
$unlockResult = processEvent(
WithLock::onEntity(
$owner,
AwaitResult::forEvent($owner, RaiseEvent::forUnlock($owner->id, null, null)),
$history->id,
),
$history->applyRaiseEvent(...),
);
expect($unlockResult)
->toContain($waiting)->and($called)->toBe(1);
});
it('properly locks in a chain', function (): void {
$called = 0;
$outerCall = function () use (&$called): void {
$called++;
};
$history = getEntityHistory(
new class ($outerCall) extends EntityState {
public function __construct(public $outerCall) {}
public function signal(): void
{
($this->outerCall)();
}
},
);
$history->from = StateId::fromInstance(OrchestrationInstance('test', 'test'));
$owner = StateId::fromInstance(OrchestrationInstance('owner', 'owner'));
$other = StateId::fromInstance(OrchestrationInstance('other', 'other'));
$otherEntity = getEntityHistory();
$firstLock = WithEntity::forInstance(
$history->id,
AwaitResult::forEvent(
$owner,
WithLock::onEntity(
$owner,
RaiseEvent::forLockNotification($owner),
$otherEntity->id,
$history->id,
),
),
);
$actualEvent = WithEntity::forInstance(
$history->id,
AwaitResult::forEvent(
$owner,
WithLock::onEntity(
$owner,
RaiseEvent::forOperation('signal', []),
$otherEntity->id,
$history->id,
),
),
);
// send the first lock notification in the chain
$firstResult = processEvent($firstLock, $otherEntity->applyRaiseEvent(...));
expect($firstResult)
->toHaveCount(3)->and($firstResult[0]->innerEvent->target->id)->toBe($history->id->id);
// send a signal to be run once the lock is complete
$locked = processEvent($actualEvent, $history->applyRaiseEvent(...));
expect($locked)->toHaveCount(1);
// complete the lock sequence
$secondResult = processEvent($firstResult[0], $history->applyRaiseEvent(...));
expect($secondResult)
->toHaveCount(3)->and($secondResult[0])->toBeInstanceOf(WithEntity::class);
// process the actual event earlier
$finalResult = processEvent($secondResult[0], $history->applyRaiseEvent(...));
expect($called)->toBe(1);
// process the final lock notification
$finalResult = processEvent($secondResult[1], $otherEntity->applyRaiseEvent(...));
expect($finalResult)->toBeEmpty();
});
it('can get a property value using get signal', function (): void {
// Create an entity state with a property
$history = getEntityHistory(
new class extends EntityState {
public string $testProperty = 'test value';
},
);
$history->from = StateId::fromInstance(OrchestrationInstance('test', 'test'));
// Create a signal to get the property value
$event = RaiseEvent::forOperation('$testProperty::get', []);
$event = AwaitResult::forEvent(StateId::fromInstance(OrchestrationInstance('test', 'test')), $event);
// Process the event
$result = processEvent($event, $history->applyRaiseEvent(...));
// Verify the result contains a TaskCompleted event with the property value
expect($result)->toHaveCount(1);
expect($result[0]->getInnerEvent()->getInnerEvent())->toBeInstanceOf(TaskCompleted::class);
expect($result[0]->getInnerEvent()->getInnerEvent()->result)->toBe(['value' => 'test value']);
});
it('can set a property value using set signal', function (): void {
// Create an entity state with a property
$history = getEntityHistory(
new class extends EntityState {
public string $testProperty = 'initial value';
},
);
$history->from = StateId::fromInstance(OrchestrationInstance('test', 'test'));
// Create a signal to set the property value
$event = RaiseEvent::forOperation('$testProperty::set', ['new value']);
$event = AwaitResult::forEvent(StateId::fromInstance(OrchestrationInstance('test', 'test')), $event);
// Process the event
$result = processEvent($event, $history->applyRaiseEvent(...));
// Verify the property was updated
expect($history->getState()->testProperty)->toBe('new value');
// Verify the result contains a TaskCompleted event
expect($result)->toHaveCount(1);
expect($result[0]->getInnerEvent()->getInnerEvent())->toBeInstanceOf(TaskCompleted::class);
});
it('handles access control for property signals', function (): void {
// Create a mock class with an AccessControl attribute on a property
$from = StateId::fromInstance(OrchestrationInstance('test', 'test'));
$mockClass = new class extends EntityState {
#[Bottledcode\DurablePhp\State\Attributes\DenyAnyOperation(fromType: 'test')]
public string $restrictedProperty = 'restricted value';
public string $publicProperty = 'public value';
};
$history = getEntityHistory($mockClass);
$history->from = $from;
// Try to access the restricted property
$restrictedEvent = RaiseEvent::forOperation('$restrictedProperty::get', []);
// This should throw a SecurityException, which is caught in the execute method
expect(fn() => processEvent($restrictedEvent, $history->applyRaiseEvent(...)))->toThrow(SecurityException::class);
});