|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Unit\V2; |
| 6 | + |
| 7 | +use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Tests\Fixtures\V2\TestChildGreetingWorkflow; |
| 10 | +use Tests\Fixtures\V2\TestMixedEntryActivity; |
| 11 | +use Workflow\V2\Enums\HistoryEventType; |
| 12 | +use Workflow\V2\Exceptions\HistoryEventShapeMismatchException; |
| 13 | +use Workflow\V2\Models\WorkflowHistoryEvent; |
| 14 | +use Workflow\V2\Models\WorkflowRun; |
| 15 | +use Workflow\V2\Support\WorkflowStepHistory; |
| 16 | + |
| 17 | +final class WorkflowStepHistoryTest extends TestCase |
| 18 | +{ |
| 19 | + public function testActivityTypeDetailAcceptsCanonicalTypeAliasForYieldedClass(): void |
| 20 | + { |
| 21 | + $run = $this->runWithHistoryEvents([ |
| 22 | + $this->historyEvent(HistoryEventType::ActivityScheduled, [ |
| 23 | + 'sequence' => 1, |
| 24 | + 'activity_type' => 'test-mixed-entry-activity', |
| 25 | + 'activity_class' => TestMixedEntryActivity::class, |
| 26 | + ]), |
| 27 | + ]); |
| 28 | + |
| 29 | + WorkflowStepHistory::assertCompatible($run, 1, WorkflowStepHistory::ACTIVITY, [ |
| 30 | + 'activity_type' => TestMixedEntryActivity::class, |
| 31 | + ]); |
| 32 | + |
| 33 | + $this->addToAssertionCount(1); |
| 34 | + } |
| 35 | + |
| 36 | + public function testActivityTypeDetailRejectsMutatedTypeEvenWhenClassFallbackMatches(): void |
| 37 | + { |
| 38 | + $run = $this->runWithHistoryEvents([ |
| 39 | + $this->historyEvent(HistoryEventType::ActivityScheduled, [ |
| 40 | + 'sequence' => 1, |
| 41 | + 'activity_type' => 'changed-activity-type', |
| 42 | + 'activity_class' => TestMixedEntryActivity::class, |
| 43 | + ]), |
| 44 | + ]); |
| 45 | + |
| 46 | + $this->expectException(HistoryEventShapeMismatchException::class); |
| 47 | + $this->expectExceptionMessage('Recorded activity_type [changed-activity-type]'); |
| 48 | + |
| 49 | + WorkflowStepHistory::assertCompatible($run, 1, WorkflowStepHistory::ACTIVITY, [ |
| 50 | + 'activity_type' => TestMixedEntryActivity::class, |
| 51 | + ]); |
| 52 | + } |
| 53 | + |
| 54 | + public function testChildWorkflowTypeDetailAcceptsCanonicalTypeAliasForYieldedClass(): void |
| 55 | + { |
| 56 | + $run = $this->runWithHistoryEvents([ |
| 57 | + $this->historyEvent(HistoryEventType::ChildWorkflowScheduled, [ |
| 58 | + 'sequence' => 2, |
| 59 | + 'child_workflow_type' => 'test-child-greeting-workflow', |
| 60 | + 'child_workflow_class' => TestChildGreetingWorkflow::class, |
| 61 | + ]), |
| 62 | + ]); |
| 63 | + |
| 64 | + WorkflowStepHistory::assertCompatible($run, 2, WorkflowStepHistory::CHILD_WORKFLOW, [ |
| 65 | + 'child_workflow_type' => TestChildGreetingWorkflow::class, |
| 66 | + ]); |
| 67 | + |
| 68 | + $this->addToAssertionCount(1); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param list<WorkflowHistoryEvent> $events |
| 73 | + */ |
| 74 | + private function runWithHistoryEvents(array $events): WorkflowRun |
| 75 | + { |
| 76 | + $run = new WorkflowRun(); |
| 77 | + $run->setRelation('historyEvents', new EloquentCollection($events)); |
| 78 | + |
| 79 | + return $run; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param array<string, mixed> $payload |
| 84 | + */ |
| 85 | + private function historyEvent(HistoryEventType $type, array $payload): WorkflowHistoryEvent |
| 86 | + { |
| 87 | + $event = new WorkflowHistoryEvent(); |
| 88 | + $event->forceFill([ |
| 89 | + 'sequence' => $payload['sequence'] ?? 1, |
| 90 | + 'event_type' => $type->value, |
| 91 | + 'payload' => $payload, |
| 92 | + ]); |
| 93 | + |
| 94 | + return $event; |
| 95 | + } |
| 96 | +} |
0 commit comments