|
24 | 24 |
|
25 | 25 | // namespace Bottledcode\DurablePhp\Tests\Unit; |
26 | 26 |
|
| 27 | +use Bottledcode\DurablePhp\Contexts\AuthContext\SecurityException; |
27 | 28 | use Bottledcode\DurablePhp\Events\AwaitResult; |
28 | 29 | use Bottledcode\DurablePhp\Events\RaiseEvent; |
| 30 | +use Bottledcode\DurablePhp\Events\TaskCompleted; |
29 | 31 | use Bottledcode\DurablePhp\Events\WithEntity; |
30 | 32 | use Bottledcode\DurablePhp\Events\WithLock; |
31 | 33 | use Bottledcode\DurablePhp\State\EntityState; |
@@ -193,3 +195,69 @@ public function signal(): void |
193 | 195 | $finalResult = processEvent($secondResult[1], $otherEntity->applyRaiseEvent(...)); |
194 | 196 | expect($finalResult)->toBeEmpty(); |
195 | 197 | }); |
| 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