Skip to content

Commit 7fc3715

Browse files
committed
Fixed flappy test in the factory instead
1 parent b4ec5be commit 7fc3715

2 files changed

Lines changed: 11 additions & 14 deletions

File tree

database/factories/MaintenanceFactory.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,21 @@ public function definition()
6868
// Set location_id to rtd_location_id on the generated asset so
6969
// seeded maintenance rows point at assets with a real location,
7070
// matching what snipeit:sync-asset-locations would have set.
71-
'asset_id' => Asset::factory()->laptopZenbook()->afterMaking(function (Asset $asset) {
71+
//
72+
// Use item_id + item_type (the polymorphic FK) rather than the
73+
// legacy asset_id so callers can override the target with their
74+
// own asset instance and skip the default factory-created one.
75+
// The legacy asset_id path still routes through the model's
76+
// setAssetIdAttribute mutator, but the default path here no
77+
// longer spawns an asset when a caller passes item_id, which
78+
// was leaking spurious asset rows into cross-cutting counts
79+
// (assetsPastEol on the Needs Attention widget hit this).
80+
'item_id' => Asset::factory()->laptopZenbook()->afterMaking(function (Asset $asset) {
7281
if ($asset->location_id === null) {
7382
$asset->location_id = $asset->rtd_location_id;
7483
}
7584
}),
85+
'item_type' => Asset::class,
7686
'supplier_id' => Supplier::factory(),
7787
'maintenance_type_id' => $maintenanceType->id,
7888
'asset_maintenance_type' => $maintenanceType->name,

tests/Feature/Livewire/NeedsAttentionTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,7 @@ public function test_counts_reflect_seeded_data(): void
121121
$maintenanceAsset = Asset::factory()->create(['next_audit_date' => $farFuture]);
122122
$maintenanceAsset->forceFill(['asset_eol_date' => $farFuture])->save();
123123

124-
// Pin asset_id to the already-isolated $maintenanceAsset. Without
125-
// this override MaintenanceFactory's default definition resolves
126-
// asset_id via Asset::factory()->laptopZenbook(), spawning an
127-
// extra asset whose asset_eol_date lands in the past on the ~5%
128-
// chance leaked by AssetFactory's afterMaking hook. That was
129-
// the source of a flaky assetsPastEol == 5 in CI.
130124
Maintenance::factory()->create([
131-
'asset_id' => $maintenanceAsset->id,
132125
'item_id' => $maintenanceAsset->id,
133126
'item_type' => Asset::class,
134127
'start_date' => now()->subDays(20),
@@ -153,20 +146,14 @@ public function test_counts_ignore_completed_maintenances(): void
153146
// should register.
154147
$asset = Asset::factory()->create();
155148

156-
// Pin asset_id on both maintenance rows for the same reason as
157-
// test_counts_reflect_seeded_data: MaintenanceFactory's default
158-
// asset_id resolver spawns an extra asset per call, which can
159-
// leak into cross-cutting counts on adjacent tests.
160149
Maintenance::factory()->create([
161-
'asset_id' => $asset->id,
162150
'item_id' => $asset->id,
163151
'item_type' => Asset::class,
164152
'start_date' => now()->subDays(20),
165153
'expected_completion_date' => now()->subDays(5),
166154
'completed_at' => now()->subDays(1),
167155
]);
168156
Maintenance::factory()->create([
169-
'asset_id' => $asset->id,
170157
'item_id' => $asset->id,
171158
'item_type' => Asset::class,
172159
'start_date' => now()->subDays(20),

0 commit comments

Comments
 (0)