|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Unit\V2; |
| 6 | + |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use Tests\TestCase; |
| 9 | +use Workflow\V2\Models\WorkflowInstance; |
| 10 | +use Workflow\V2\Models\WorkflowRun; |
| 11 | +use Workflow\V2\Models\WorkflowTimelineEntry; |
| 12 | +use Workflow\V2\Support\StaleProjectionCleanup; |
| 13 | + |
| 14 | +final class StaleProjectionCleanupTest extends TestCase |
| 15 | +{ |
| 16 | + public function testRemovesOnlyPrunedRowsAndLeavesSeenRowsIntact(): void |
| 17 | + { |
| 18 | + $run = $this->seedRun(); |
| 19 | + |
| 20 | + $keepA = $this->seedTimelineEntry($run, 'keep-a'); |
| 21 | + $keepB = $this->seedTimelineEntry($run, 'keep-b'); |
| 22 | + $stale = $this->seedTimelineEntry($run, 'stale'); |
| 23 | + |
| 24 | + StaleProjectionCleanup::forRun(WorkflowTimelineEntry::class, $run->id, [$keepA->id, $keepB->id]); |
| 25 | + |
| 26 | + $this->assertDatabaseHas('workflow_run_timeline_entries', [ |
| 27 | + 'id' => $keepA->id, |
| 28 | + ]); |
| 29 | + $this->assertDatabaseHas('workflow_run_timeline_entries', [ |
| 30 | + 'id' => $keepB->id, |
| 31 | + ]); |
| 32 | + $this->assertDatabaseMissing('workflow_run_timeline_entries', [ |
| 33 | + 'id' => $stale->id, |
| 34 | + ]); |
| 35 | + } |
| 36 | + |
| 37 | + public function testRemovesAllRowsWhenSeenIsEmpty(): void |
| 38 | + { |
| 39 | + $run = $this->seedRun(); |
| 40 | + |
| 41 | + $this->seedTimelineEntry($run, 'doomed-a'); |
| 42 | + $this->seedTimelineEntry($run, 'doomed-b'); |
| 43 | + |
| 44 | + StaleProjectionCleanup::forRun(WorkflowTimelineEntry::class, $run->id, []); |
| 45 | + |
| 46 | + $this->assertSame(0, WorkflowTimelineEntry::query()->where('workflow_run_id', $run->id)->count()); |
| 47 | + } |
| 48 | + |
| 49 | + public function testNoopWhenNoRowsExistForRun(): void |
| 50 | + { |
| 51 | + $run = $this->seedRun(); |
| 52 | + |
| 53 | + StaleProjectionCleanup::forRun(WorkflowTimelineEntry::class, $run->id, ['unused-id']); |
| 54 | + |
| 55 | + $this->assertSame(0, WorkflowTimelineEntry::query()->where('workflow_run_id', $run->id)->count()); |
| 56 | + } |
| 57 | + |
| 58 | + public function testDoesNotAffectRowsForOtherRuns(): void |
| 59 | + { |
| 60 | + $runA = $this->seedRun(); |
| 61 | + $runB = $this->seedRun(); |
| 62 | + |
| 63 | + $runAEntry = $this->seedTimelineEntry($runA, 'run-a-entry'); |
| 64 | + $runBEntry = $this->seedTimelineEntry($runB, 'run-b-entry'); |
| 65 | + |
| 66 | + StaleProjectionCleanup::forRun(WorkflowTimelineEntry::class, $runA->id, []); |
| 67 | + |
| 68 | + $this->assertDatabaseMissing('workflow_run_timeline_entries', [ |
| 69 | + 'id' => $runAEntry->id, |
| 70 | + ]); |
| 71 | + $this->assertDatabaseHas('workflow_run_timeline_entries', [ |
| 72 | + 'id' => $runBEntry->id, |
| 73 | + ]); |
| 74 | + } |
| 75 | + |
| 76 | + private function seedRun(): WorkflowRun |
| 77 | + { |
| 78 | + $instance = WorkflowInstance::query()->create([ |
| 79 | + 'id' => (string) Str::ulid(), |
| 80 | + 'workflow_class' => 'App\\Fake\\Workflow', |
| 81 | + 'workflow_type' => 'App\\Fake\\Workflow', |
| 82 | + 'business_key' => null, |
| 83 | + 'namespace' => 'default', |
| 84 | + ]); |
| 85 | + |
| 86 | + return WorkflowRun::query()->create([ |
| 87 | + 'id' => (string) Str::ulid(), |
| 88 | + 'workflow_instance_id' => $instance->id, |
| 89 | + 'run_number' => 1, |
| 90 | + 'workflow_class' => $instance->workflow_class, |
| 91 | + 'workflow_type' => $instance->workflow_type, |
| 92 | + 'status' => 'pending', |
| 93 | + 'connection' => null, |
| 94 | + 'queue' => null, |
| 95 | + ]); |
| 96 | + } |
| 97 | + |
| 98 | + private function seedTimelineEntry(WorkflowRun $run, string $historyEventId): WorkflowTimelineEntry |
| 99 | + { |
| 100 | + return WorkflowTimelineEntry::query()->create([ |
| 101 | + 'id' => hash('sha256', $run->id . '|' . $historyEventId), |
| 102 | + 'workflow_run_id' => $run->id, |
| 103 | + 'workflow_instance_id' => $run->workflow_instance_id, |
| 104 | + 'history_event_id' => $historyEventId, |
| 105 | + 'sequence' => 0, |
| 106 | + 'type' => 'TestEvent', |
| 107 | + 'kind' => 'workflow', |
| 108 | + 'entry_kind' => 'point', |
| 109 | + 'source_kind' => null, |
| 110 | + 'source_id' => null, |
| 111 | + 'summary' => null, |
| 112 | + 'recorded_at' => now(), |
| 113 | + 'payload' => [], |
| 114 | + ]); |
| 115 | + } |
| 116 | +} |
0 commit comments