Skip to content

Commit 98717b2

Browse files
committed
add tests
Signed-off-by: Robert Landers <landers.robert@gmail.com>
1 parent 4a9200b commit 98717b2

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

tests/Unit/EntityHistoryTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424

2525
// namespace Bottledcode\DurablePhp\Tests\Unit;
2626

27+
use Bottledcode\DurablePhp\Contexts\AuthContext\SecurityException;
2728
use Bottledcode\DurablePhp\Events\AwaitResult;
2829
use Bottledcode\DurablePhp\Events\RaiseEvent;
30+
use Bottledcode\DurablePhp\Events\TaskCompleted;
2931
use Bottledcode\DurablePhp\Events\WithEntity;
3032
use Bottledcode\DurablePhp\Events\WithLock;
3133
use Bottledcode\DurablePhp\State\EntityState;
@@ -193,3 +195,69 @@ public function signal(): void
193195
$finalResult = processEvent($secondResult[1], $otherEntity->applyRaiseEvent(...));
194196
expect($finalResult)->toBeEmpty();
195197
});
198+
199+
it('can get a property value using get signal', function (): void {
200+
// Create an entity state with a property
201+
$history = getEntityHistory(
202+
new class extends EntityState {
203+
public string $testProperty = 'test value';
204+
},
205+
);
206+
$history->from = StateId::fromInstance(OrchestrationInstance('test', 'test'));
207+
208+
// Create a signal to get the property value
209+
$event = RaiseEvent::forOperation('$testProperty::get', []);
210+
$event = AwaitResult::forEvent(StateId::fromInstance(OrchestrationInstance('test', 'test')), $event);
211+
212+
// Process the event
213+
$result = processEvent($event, $history->applyRaiseEvent(...));
214+
215+
// Verify the result contains a TaskCompleted event with the property value
216+
expect($result)->toHaveCount(1);
217+
expect($result[0]->getInnerEvent()->getInnerEvent())->toBeInstanceOf(TaskCompleted::class);
218+
expect($result[0]->getInnerEvent()->getInnerEvent()->result)->toBe(['value' => 'test value']);
219+
});
220+
221+
it('can set a property value using set signal', function (): void {
222+
// Create an entity state with a property
223+
$history = getEntityHistory(
224+
new class extends EntityState {
225+
public string $testProperty = 'initial value';
226+
},
227+
);
228+
$history->from = StateId::fromInstance(OrchestrationInstance('test', 'test'));
229+
230+
// Create a signal to set the property value
231+
$event = RaiseEvent::forOperation('$testProperty::set', ['new value']);
232+
$event = AwaitResult::forEvent(StateId::fromInstance(OrchestrationInstance('test', 'test')), $event);
233+
234+
// Process the event
235+
$result = processEvent($event, $history->applyRaiseEvent(...));
236+
237+
// Verify the property was updated
238+
expect($history->getState()->testProperty)->toBe('new value');
239+
240+
// Verify the result contains a TaskCompleted event
241+
expect($result)->toHaveCount(1);
242+
expect($result[0]->getInnerEvent()->getInnerEvent())->toBeInstanceOf(TaskCompleted::class);
243+
});
244+
245+
it('handles access control for property signals', function (): void {
246+
// Create a mock class with an AccessControl attribute on a property
247+
$from = StateId::fromInstance(OrchestrationInstance('test', 'test'));
248+
$mockClass = new class extends EntityState {
249+
#[Bottledcode\DurablePhp\State\Attributes\DenyAnyOperation(fromType: 'test')]
250+
public string $restrictedProperty = 'restricted value';
251+
252+
public string $publicProperty = 'public value';
253+
};
254+
255+
$history = getEntityHistory($mockClass);
256+
$history->from = $from;
257+
258+
// Try to access the restricted property
259+
$restrictedEvent = RaiseEvent::forOperation('$restrictedProperty::get', []);
260+
261+
// This should throw a SecurityException, which is caught in the execute method
262+
expect(fn() => processEvent($restrictedEvent, $history->applyRaiseEvent(...)))->toThrow(SecurityException::class);
263+
});

0 commit comments

Comments
 (0)