Skip to content

Commit 01b6eef

Browse files
Make migration tests discoverable and align with v2 StartResult API
Tests previously used @test annotations which PHPUnit 10 does not discover by default. Rename to testXxx methods so they run without annotation configuration. Simplify setUp using Schema::dropAllTables and migrate:install rather than manual table iteration. Use the new StartResult return value for runId()/instanceId(). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ae0aa8a commit 01b6eef

1 file changed

Lines changed: 107 additions & 77 deletions

File tree

tests/Feature/MigrationTest.php

Lines changed: 107 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use Illuminate\Support\Facades\DB;
88
use Illuminate\Support\Facades\Schema;
99
use Tests\TestCase;
10-
use Workflow\V2\WorkflowStub;
10+
use Workflow\V2\Activity;
1111
use Workflow\V2\StartOptions;
1212
use Workflow\V2\Workflow;
13-
use Workflow\V2\Activity;
13+
use Workflow\V2\WorkflowStub;
1414

1515
/**
1616
* Tests v1→v2 migration path to ensure:
@@ -26,23 +26,13 @@ protected function setUp(): void
2626
{
2727
parent::setUp();
2828

29-
// Fresh database for each test
30-
DB::statement('SET FOREIGN_KEY_CHECKS=0');
31-
$tables = DB::select('SHOW TABLES');
32-
foreach ($tables as $table) {
33-
$tableName = array_values((array) $table)[0];
34-
if ($tableName !== 'migrations') {
35-
Schema::dropIfExists($tableName);
36-
}
37-
}
38-
DB::statement('SET FOREIGN_KEY_CHECKS=1');
39-
DB::table('migrations')->truncate();
29+
Schema::dropAllTables();
30+
31+
$this->artisan('migrate:install');
4032
}
4133

42-
/**
43-
* @test
44-
*/
45-
public function it_runs_v1_migrations_without_errors()
34+
35+
public function testItRunsV1MigrationsWithoutErrors()
4636
{
4737
$this->runV1Migrations();
4838

@@ -56,14 +46,12 @@ public function it_runs_v1_migrations_without_errors()
5646

5747
// Assert v1 table structure
5848
$this->assertTrue(Schema::hasColumns('workflows', [
59-
'id', 'class', 'arguments', 'output', 'status', 'created_at', 'updated_at'
49+
'id', 'class', 'arguments', 'output', 'status', 'created_at', 'updated_at',
6050
]));
6151
}
6252

63-
/**
64-
* @test
65-
*/
66-
public function it_preserves_v1_workflow_data_after_v2_migration()
53+
54+
public function testItPreservesV1WorkflowDataAfterV2Migration()
6755
{
6856
// Set up v1 schema and data
6957
$this->runV1Migrations();
@@ -96,14 +84,18 @@ public function it_preserves_v1_workflow_data_after_v2_migration()
9684
$this->assertEquals($v1Workflow->arguments, $v1WorkflowAfter->arguments);
9785

9886
// Assert v1 related data is unchanged
99-
$this->assertEquals($v1LogCount, DB::table('workflow_logs')->where('stored_workflow_id', $v1WorkflowId)->count());
100-
$this->assertEquals($v1SignalCount, DB::table('workflow_signals')->where('stored_workflow_id', $v1WorkflowId)->count());
87+
$this->assertEquals(
88+
$v1LogCount,
89+
DB::table('workflow_logs')->where('stored_workflow_id', $v1WorkflowId)->count()
90+
);
91+
$this->assertEquals(
92+
$v1SignalCount,
93+
DB::table('workflow_signals')->where('stored_workflow_id', $v1WorkflowId)->count()
94+
);
10195
}
10296

103-
/**
104-
* @test
105-
*/
106-
public function it_allows_v2_workflows_after_migration()
97+
98+
public function testItAllowsV2WorkflowsAfterMigration()
10799
{
108100
// Set up v1 schema and data
109101
$this->runV1Migrations();
@@ -114,30 +106,34 @@ public function it_allows_v2_workflows_after_migration()
114106

115107
// Start a v2 workflow
116108
$workflow = WorkflowStub::make(TestMigrationWorkflow::class, 'migration-test-v2-1');
117-
$runId = $workflow->start(['test' => true], StartOptions::rejectDuplicate());
109+
$result = $workflow->start([
110+
'test' => true,
111+
], StartOptions::rejectDuplicate());
112+
$runId = $result->runId();
118113

119114
// Assert v2 data was created
120-
$this->assertNotNull($runId);
115+
$this->assertIsString($runId);
116+
$this->assertSame('migration-test-v2-1', $result->instanceId());
121117

122118
$instance = DB::table('workflow_instances')
123-
->where('instance_id', 'migration-test-v2-1')
119+
->where('id', 'migration-test-v2-1')
124120
->first();
125121
$this->assertNotNull($instance);
122+
$this->assertEquals($runId, $instance->current_run_id);
126123

127124
$run = DB::table('workflow_runs')
128-
->where('run_id', $runId)
125+
->where('id', $runId)
129126
->first();
130127
$this->assertNotNull($run);
128+
$this->assertEquals('migration-test-v2-1', $run->workflow_instance_id);
131129

132130
// Assert v1 data is still intact
133131
$v1Count = DB::table('workflows')->count();
134132
$this->assertGreaterThan(0, $v1Count, 'v1 workflows should still exist');
135133
}
136134

137-
/**
138-
* @test
139-
*/
140-
public function it_tracks_v1_workflows_separately_from_v2()
135+
136+
public function testItTracksV1WorkflowsSeparatelyFromV2()
141137
{
142138
// Set up v1 schema and data
143139
$this->runV1Migrations();
@@ -148,15 +144,18 @@ public function it_tracks_v1_workflows_separately_from_v2()
148144

149145
// Start a v2 workflow
150146
$workflow = WorkflowStub::make(TestMigrationWorkflow::class, 'migration-test-coexist');
151-
$runId = $workflow->start(['test' => true], StartOptions::rejectDuplicate());
147+
$result = $workflow->start([
148+
'test' => true,
149+
], StartOptions::rejectDuplicate());
150+
$this->assertIsString($result->runId());
152151

153152
// v1 workflow should be in workflows table
154153
$v1Exists = DB::table('workflows')->where('id', $v1Id)->exists();
155154
$this->assertTrue($v1Exists, 'v1 workflow should exist in workflows table');
156155

157156
// v2 workflow should be in workflow_instances table
158157
$v2Exists = DB::table('workflow_instances')
159-
->where('instance_id', 'migration-test-coexist')
158+
->where('id', 'migration-test-coexist')
160159
->exists();
161160
$this->assertTrue($v2Exists, 'v2 workflow should exist in workflow_instances table');
162161

@@ -168,22 +167,22 @@ public function it_tracks_v1_workflows_separately_from_v2()
168167

169168
// v1 workflow should NOT be in workflow_instances table
170169
$v1InV2Table = DB::table('workflow_instances')
171-
->where('instance_id', 'test-v1-workflow')
170+
->where('id', 'test-v1-workflow')
172171
->exists();
173172
$this->assertFalse($v1InV2Table, 'v1 workflow should not appear in v2 workflow_instances table');
174173
}
175174

176-
/**
177-
* @test
178-
*/
179-
public function it_preserves_v1_workflow_with_timer()
175+
176+
public function testItPreservesV1WorkflowWithTimer()
180177
{
181178
$this->runV1Migrations();
182179

183180
// Create v1 workflow with timer
184181
$workflowId = DB::table('workflows')->insertGetId([
185182
'class' => 'App\\TestWorkflow',
186-
'arguments' => json_encode(['test' => true]),
183+
'arguments' => json_encode([
184+
'test' => true,
185+
]),
187186
'status' => 'running',
188187
'created_at' => now(),
189188
'updated_at' => now(),
@@ -192,7 +191,8 @@ public function it_preserves_v1_workflow_with_timer()
192191
DB::table('workflow_timers')->insert([
193192
'stored_workflow_id' => $workflowId,
194193
'index' => 1,
195-
'stop_at' => now()->addHours(2),
194+
'stop_at' => now()
195+
->addHours(2),
196196
'created_at' => now(),
197197
]);
198198

@@ -207,17 +207,17 @@ public function it_preserves_v1_workflow_with_timer()
207207
$this->assertEquals(1, $timer->index);
208208
}
209209

210-
/**
211-
* @test
212-
*/
213-
public function it_preserves_v1_workflow_with_signals()
210+
211+
public function testItPreservesV1WorkflowWithSignals()
214212
{
215213
$this->runV1Migrations();
216214

217215
// Create v1 workflow with signals
218216
$workflowId = DB::table('workflows')->insertGetId([
219217
'class' => 'App\\OrderWorkflow',
220-
'arguments' => json_encode(['order_id' => 123]),
218+
'arguments' => json_encode([
219+
'order_id' => 123,
220+
]),
221221
'status' => 'running',
222222
'created_at' => now(),
223223
'updated_at' => now(),
@@ -226,15 +226,20 @@ public function it_preserves_v1_workflow_with_signals()
226226
DB::table('workflow_signals')->insert([
227227
'stored_workflow_id' => $workflowId,
228228
'method' => 'approve',
229-
'arguments' => json_encode(['approved_by' => 'admin']),
229+
'arguments' => json_encode([
230+
'approved_by' => 'admin',
231+
]),
230232
'created_at' => now(),
231233
]);
232234

233235
DB::table('workflow_signals')->insert([
234236
'stored_workflow_id' => $workflowId,
235237
'method' => 'payment_received',
236-
'arguments' => json_encode(['amount' => 99.99]),
237-
'created_at' => now()->addMinutes(5),
238+
'arguments' => json_encode([
239+
'amount' => 99.99,
240+
]),
241+
'created_at' => now()
242+
->addMinutes(5),
238243
]);
239244

240245
// Run v2 migrations
@@ -250,17 +255,17 @@ public function it_preserves_v1_workflow_with_signals()
250255
$this->assertEquals('payment_received', $signals[1]->method);
251256
}
252257

253-
/**
254-
* @test
255-
*/
256-
public function it_preserves_v1_workflow_with_exception()
258+
259+
public function testItPreservesV1WorkflowWithException()
257260
{
258261
$this->runV1Migrations();
259262

260263
// Create v1 workflow with exception
261264
$workflowId = DB::table('workflows')->insertGetId([
262265
'class' => 'App\\FailedWorkflow',
263-
'arguments' => json_encode(['test' => true]),
266+
'arguments' => json_encode([
267+
'test' => true,
268+
]),
264269
'status' => 'failed',
265270
'created_at' => now(),
266271
'updated_at' => now(),
@@ -305,8 +310,8 @@ private function runV1Migrations(): void
305310

306311
foreach ($migrations as $migrationFile) {
307312
$path = __DIR__ . '/../../src/migrations/' . $migrationFile;
308-
if (!file_exists($path)) {
309-
throw new \RuntimeException("Migration not found: $path");
313+
if (! file_exists($path)) {
314+
throw new \RuntimeException("Migration not found: {$path}");
310315
}
311316

312317
$migration = include $path;
@@ -354,45 +359,66 @@ private function seedV1WorkflowData(): int
354359
// Create a completed v1 workflow
355360
$completedId = DB::table('workflows')->insertGetId([
356361
'class' => 'App\\SimpleWorkflow',
357-
'arguments' => json_encode(['name' => 'test']),
358-
'output' => json_encode(['result' => 'success']),
362+
'arguments' => json_encode([
363+
'name' => 'test',
364+
]),
365+
'output' => json_encode([
366+
'result' => 'success',
367+
]),
359368
'status' => 'completed',
360-
'created_at' => now()->subHours(2),
361-
'updated_at' => now()->subHours(1),
369+
'created_at' => now()
370+
->subHours(2),
371+
'updated_at' => now()
372+
->subHours(1),
362373
]);
363374

364375
DB::table('workflow_logs')->insert([
365376
'stored_workflow_id' => $completedId,
366377
'index' => 1,
367-
'now' => now()->subHours(2),
378+
'now' => now()
379+
->subHours(2),
368380
'class' => 'App\\TestActivity',
369-
'result' => json_encode(['done' => true]),
370-
'created_at' => now()->subHours(2),
381+
'result' => json_encode([
382+
'done' => true,
383+
]),
384+
'created_at' => now()
385+
->subHours(2),
371386
]);
372387

373388
// Create a running v1 workflow with signal
374389
$runningId = DB::table('workflows')->insertGetId([
375390
'class' => 'App\\OrderWorkflow',
376-
'arguments' => json_encode(['order_id' => 123]),
391+
'arguments' => json_encode([
392+
'order_id' => 123,
393+
]),
377394
'status' => 'running',
378-
'created_at' => now()->subMinutes(30),
379-
'updated_at' => now()->subMinutes(10),
395+
'created_at' => now()
396+
->subMinutes(30),
397+
'updated_at' => now()
398+
->subMinutes(10),
380399
]);
381400

382401
DB::table('workflow_signals')->insert([
383402
'stored_workflow_id' => $runningId,
384403
'method' => 'approve',
385-
'arguments' => json_encode(['approved_by' => 'admin']),
386-
'created_at' => now()->subMinutes(20),
404+
'arguments' => json_encode([
405+
'approved_by' => 'admin',
406+
]),
407+
'created_at' => now()
408+
->subMinutes(20),
387409
]);
388410

389411
// Create a pending v1 workflow
390412
$pendingId = DB::table('workflows')->insertGetId([
391413
'class' => 'App\\InvoiceWorkflow',
392-
'arguments' => json_encode(['invoice_id' => 456]),
414+
'arguments' => json_encode([
415+
'invoice_id' => 456,
416+
]),
393417
'status' => 'pending',
394-
'created_at' => now()->subMinutes(5),
395-
'updated_at' => now()->subMinutes(5),
418+
'created_at' => now()
419+
->subMinutes(5),
420+
'updated_at' => now()
421+
->subMinutes(5),
396422
]);
397423

398424
return $completedId; // Return one for verification
@@ -406,7 +432,9 @@ class TestMigrationWorkflow extends Workflow
406432
{
407433
public function handle(array $input)
408434
{
409-
return ['migration_test' => 'passed'];
435+
return [
436+
'migration_test' => 'passed',
437+
];
410438
}
411439
}
412440

@@ -417,6 +445,8 @@ class TestMigrationActivity extends Activity
417445
{
418446
public function handle(array $input)
419447
{
420-
return ['activity_result' => 'ok'];
448+
return [
449+
'activity_result' => 'ok',
450+
];
421451
}
422452
}

0 commit comments

Comments
 (0)