Skip to content

A free open source IT asset/license management system

Fixed flappy test in the factory instead

snipe7fc3715b54a7
diff --git a/database/factories/MaintenanceFactory.php b/database/factories/MaintenanceFactory.php
index 925b13ebf7..c0129c780c 100644
--- a/database/factories/MaintenanceFactory.php
+++ b/database/factories/MaintenanceFactory.php
@@ -68,11 +68,21 @@ class MaintenanceFactory extends Factory
// Set location_id to rtd_location_id on the generated asset so
// seeded maintenance rows point at assets with a real location,
// matching what snipeit:sync-asset-locations would have set.
- 'asset_id' => Asset::factory()->laptopZenbook()->afterMaking(function (Asset $asset) {
+ //
+ // Use item_id + item_type (the polymorphic FK) rather than the
+ // legacy asset_id so callers can override the target with their
+ // own asset instance and skip the default factory-created one.
+ // The legacy asset_id path still routes through the model's
+ // setAssetIdAttribute mutator, but the default path here no
+ // longer spawns an asset when a caller passes item_id, which
+ // was leaking spurious asset rows into cross-cutting counts
+ // (assetsPastEol on the Needs Attention widget hit this).
+ 'item_id' => Asset::factory()->laptopZenbook()->afterMaking(function (Asset $asset) {
if ($asset->location_id === null) {
$asset->location_id = $asset->rtd_location_id;
}
}),
+ 'item_type' => Asset::class,
'supplier_id' => Supplier::factory(),
'maintenance_type_id' => $maintenanceType->id,
'asset_maintenance_type' => $maintenanceType->name,
diff --git a/tests/Feature/Livewire/NeedsAttentionTest.php b/tests/Feature/Livewire/NeedsAttentionTest.php
index ec70b9b3a1..ba36b9c38c 100644
--- a/tests/Feature/Livewire/NeedsAttentionTest.php
+++ b/tests/Feature/Livewire/NeedsAttentionTest.php
@@ -121,14 +121,7 @@ class NeedsAttentionTest extends TestCase implements TestsFullMultipleCompaniesS
$maintenanceAsset = Asset::factory()->create(['next_audit_date' => $farFuture]);
$maintenanceAsset->forceFill(['asset_eol_date' => $farFuture])->save();
- // Pin asset_id to the already-isolated $maintenanceAsset. Without
- // this override MaintenanceFactory's default definition resolves
- // asset_id via Asset::factory()->laptopZenbook(), spawning an
- // extra asset whose asset_eol_date lands in the past on the ~5%
- // chance leaked by AssetFactory's afterMaking hook. That was
- // the source of a flaky assetsPastEol == 5 in CI.
Maintenance::factory()->create([
- 'asset_id' => $maintenanceAsset->id,
'item_id' => $maintenanceAsset->id,
'item_type' => Asset::class,
'start_date' => now()->subDays(20),
@@ -153,12 +146,7 @@ class NeedsAttentionTest extends TestCase implements TestsFullMultipleCompaniesS
// should register.
$asset = Asset::factory()->create();
- // Pin asset_id on both maintenance rows for the same reason as
- // test_counts_reflect_seeded_data: MaintenanceFactory's default
- // asset_id resolver spawns an extra asset per call, which can
- // leak into cross-cutting counts on adjacent tests.
Maintenance::factory()->create([
- 'asset_id' => $asset->id,
'item_id' => $asset->id,
'item_type' => Asset::class,
'start_date' => now()->subDays(20),
@@ -166,7 +154,6 @@ class NeedsAttentionTest extends TestCase implements TestsFullMultipleCompaniesS
'completed_at' => now()->subDays(1),
]);
Maintenance::factory()->create([
- 'asset_id' => $asset->id,
'item_id' => $asset->id,
'item_type' => Asset::class,
'start_date' => now()->subDays(20),