Scaffold tests

This commit is contained in:
Marcus Moore 2025-01-29 15:21:10 -08:00
parent fdcb891cbb
commit d254a40e0a
No known key found for this signature in database
2 changed files with 109 additions and 0 deletions

View file

@ -27,6 +27,17 @@ class CheckoutAcceptanceFactory extends Factory
public function configure(): static public function configure(): static
{ {
return $this->afterCreating(function (CheckoutAcceptance $acceptance) { return $this->afterCreating(function (CheckoutAcceptance $acceptance) {
// @todo: add comment
if ($acceptance->checkoutable instanceof Asset) {
$acceptance->checkoutable->assetlog()->create([
'action_type' => 'checkout',
'target_id' => $acceptance->assigned_to_id,
'target_type' => get_class($acceptance->assignedTo),
'item_id' => $acceptance->checkoutable_id,
'item_type' => $acceptance->checkoutable_type,
]);
}
if ($acceptance->checkoutable instanceof Asset && $acceptance->assignedTo instanceof User) { if ($acceptance->checkoutable instanceof Asset && $acceptance->assignedTo instanceof User) {
$acceptance->checkoutable->update([ $acceptance->checkoutable->update([
'assigned_to' => $acceptance->assigned_to_id, 'assigned_to' => $acceptance->assigned_to_id,
@ -51,4 +62,12 @@ class CheckoutAcceptanceFactory extends Factory
'declined_at' => null, 'declined_at' => null,
]); ]);
} }
public function accepted()
{
return $this->state([
'accepted_at' => now()->subDay(),
'declined_at' => null,
]);
}
} }

View file

@ -0,0 +1,90 @@
<?php
namespace Tests\Feature\Notifications\Email;
use App\Mail\CheckoutAssetMail;
use App\Models\CheckoutAcceptance;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class AssetAcceptanceReminderTest extends TestCase
{
private CheckoutAcceptance $checkoutAcceptance;
private User $actor;
protected function setUp(): void
{
parent::setUp();
Mail::fake();
$this->actor = User::factory()->canViewReports()->create();
$this->checkoutAcceptance = CheckoutAcceptance::factory()->pending()->create();
}
public function testMustHavePermissionToSendReminder()
{
$userWithoutPermission = User::factory()->create();
$this->actingAs($userWithoutPermission)
->post($this->routeFor($this->checkoutAcceptance))
->assertForbidden();
}
public function testReminderNotSentIfAcceptanceDoesNotExist()
{
$this->actingAs($this->actor)
->post(route('reports/unaccepted_assets_sent_reminder', [
'acceptance_id' => 999999,
]));
Mail::assertNotSent(CheckoutAssetMail::class);
}
public function testReminderNotSentIfAcceptanceAlreadyAccepted()
{
$checkoutAcceptanceAlreadyAccepted = CheckoutAcceptance::factory()->accepted()->create();
$this->actingAs($this->actor)
->post($this->routeFor($checkoutAcceptanceAlreadyAccepted));
Mail::assertNotSent(CheckoutAssetMail::class);
}
public function testUserWithoutEmailAddressHandledGracefully()
{
$userWithoutEmailAddress = User::factory()->create(['email' => null]);
$this->checkoutAcceptance->assigned_to_id = $userWithoutEmailAddress->id;
$this->checkoutAcceptance->save();
$this->actingAs($this->actor)
->post($this->routeFor($this->checkoutAcceptance))
// check we didn't crash...
->assertRedirect();
Mail::assertNotSent(CheckoutAssetMail::class);
}
public function testReminderIsSentToUser()
{
$this->actingAs($this->actor)
->post($this->routeFor($this->checkoutAcceptance))
->assertRedirect(route('reports/unaccepted_assets'));
Mail::assertSent(CheckoutAssetMail::class, 1);
Mail::assertSent(CheckoutAssetMail::class, function (CheckoutAssetMail $mail) {
return $mail->hasTo($this->checkoutAcceptance->assignedTo->email)
// @todo:
&& $mail->hasSubject('Reminder: ' . trans('mail.Asset_Checkout_Notification'));
});
}
private function routeFor(CheckoutAcceptance $checkoutAcceptance): string
{
return route('reports/unaccepted_assets_sent_reminder', [
'acceptance_id' => $checkoutAcceptance->id,
]);
}
}