actingAs(User::factory()->create()) ->post(route('accessories.checkout.store', Accessory::factory()->create())) ->assertForbidden(); } public function testValidation() { $accessory = Accessory::factory()->create(); $this->actingAs(User::factory()->checkoutAccessories()->create()) ->post(route('accessories.checkout.store', $accessory), [ // missing assigned_to ]) ->assertSessionHas('error'); } public function testAccessoryMustBeAvailableWhenCheckingOut() { $this->actingAs(User::factory()->checkoutAccessories()->create()) ->post(route('accessories.checkout.store', Accessory::factory()->withoutItemsRemaining()->create()), [ 'assigned_to' => User::factory()->create()->id, ]) ->assertSessionHas('error'); } public function testAccessoryCanBeCheckedOut() { $accessory = Accessory::factory()->create(); $user = User::factory()->create(); $this->actingAs(User::factory()->checkoutAccessories()->create()) ->post(route('accessories.checkout.store', $accessory), [ 'assigned_to' => $user->id, ]); $this->assertTrue($accessory->users->contains($user)); } public function testUserSentNotificationUponCheckout() { Notification::fake(); $accessory = Accessory::factory()->requiringAcceptance()->create(); $user = User::factory()->create(); $this->actingAs(User::factory()->checkoutAccessories()->create()) ->post(route('accessories.checkout.store', $accessory), [ 'assigned_to' => $user->id, ]); Notification::assertSentTo($user, CheckoutAccessoryNotification::class); } public function testActionLogCreatedUponCheckout() { $accessory = Accessory::factory()->create(); $actor = User::factory()->checkoutAccessories()->create(); $user = User::factory()->create(); $this->actingAs($actor) ->post(route('accessories.checkout.store', $accessory), [ 'assigned_to' => $user->id, 'note' => 'oh hi there', ]); $this->assertDatabaseHas('action_logs', [ 'action_type' => 'checkout', 'target_id' => $user->id, 'target_type' => User::class, 'item_id' => $accessory->id, 'item_type' => Accessory::class, 'user_id' => $actor->id, 'note' => 'oh hi there', ]); } }