adds an option to disable Auto assigned an actionlogs in factories

This commit is contained in:
Godfrey M
2025-09-24 15:27:16 -07:00
parent 7d0742054f
commit 6f990dd1de
2 changed files with 49 additions and 2 deletions

View File

@@ -23,23 +23,39 @@ class CheckoutAcceptanceFactory extends Factory
'assigned_to_id' => User::factory(),
];
}
protected static bool $skipAutoAssign = false;
public function withoutAutoAssign(): static
{
// turn off for this create() call
static::$skipAutoAssign = true;
// ensure it turns back on AFTER creating
return $this->afterCreating(function () {
static::$skipAutoAssign = false;
});
}
public function configure(): static
{
return $this->afterCreating(function (CheckoutAcceptance $acceptance) {
if (static::$skipAutoAssign) {
return; // short-circuit
}
if ($acceptance->checkoutable instanceof Asset) {
$this->createdAssociatedActionLogEntry($acceptance);
}
if ($acceptance->checkoutable instanceof Asset && $acceptance->assignedTo instanceof User) {
$acceptance->checkoutable->update([
'assigned_to' => $acceptance->assigned_to_id,
'assigned_type' => get_class($acceptance->assignedTo),
'assigned_to' => $acceptance->assigned_to_id,
'assigned_type'=> get_class($acceptance->assignedTo),
]);
}
});
}
public function forAccessory()
{
return $this->state([

View File

@@ -86,7 +86,38 @@ class AssetAcceptanceReminderTest extends TestCase
public function testReminderIsSentToUser()
{
<<<<<<< Updated upstream
$checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
=======
$checkedOutBy = User::factory()->canViewReports()->create();
$checkoutTypes = [
Asset::class => CheckoutAssetMail::class,
Accessory::class => CheckoutAccessoryMail::class,
LicenseSeat::class => CheckoutLicenseMail::class,
Consumable::class => CheckoutConsumableMail::class,
//for the future its setup for components, but we dont send reminders for components at the moment.
// Component::class => CheckoutComponentMail::class,
];
$assignee = User::factory()->create(['email' => 'test@example.com']);
foreach ($checkoutTypes as $modelClass => $mailable) {
$item = $modelClass::factory()->create();
$acceptance = CheckoutAcceptance::factory()->withoutAutoAssign()->pending()->create([
'checkoutable_id' => $item->id,
'checkoutable_type' => $modelClass,
'assigned_to_id' => $assignee->id,
]);
if ($modelClass === LicenseSeat::class) {
$logType = License::class;
$logId = $item->license->id;
} else {
$logType = $modelClass;
$logId = $item->id;
}
>>>>>>> Stashed changes
$this->actingAs(User::factory()->canViewReports()->create())
->post($this->routeFor($checkoutAcceptance))