'Full Administrator', 'guard_name' => 'admin']); $this->admin = User::factory()->create(); $this->admin->assignRole('Full Administrator'); $customer = Customer::factory()->create(); $order = Order::create([ 'order_num' => 'ORD-' . uniqid(), 'customer_id' => $customer->id, 'sub_total' => 100_000, 'total' => 100_000, 'status' => OrderStatus::PENDING->value, ]); $this->invoice = Invoice::create([ 'invoice_number' => 'INV-' . uniqid(), 'customer_id' => $customer->id, 'order_id' => $order->id, 'invoice_type' => 1, 'sub_total' => 100_000, 'tax' => 0, 'total' => 100_000, 'status' => InvoiceStatus::UNPAID->value, 'due_date' => now()->addDays(5)->toDateString(), ]); } private function sendMail(bool $mailEnabled) { Queue::fake(); Setting::updateOrCreate(['key' => 'email_settings'], ['value' => json_encode(['mail_enabled' => $mailEnabled])]); return $this->actingAs($this->admin, 'admin') ->postJson("/api/v1/administrator/invoices/{$this->invoice->id}/send-mail", ['type' => 'invoice', 'message' => 'Hi']); } public function test_send_mail_is_queued(): void { $this->sendMail(true)->assertOk(); Queue::assertPushed(SendInvoiceMailJob::class, fn ($job) => $job->invoiceId === $this->invoice->id && $job->sentBy === $this->admin->id && $job->html === null); } public function test_send_mail_is_rejected_when_mail_is_disabled(): void { $this->sendMail(false)->assertStatus(422); Queue::assertNothingPushed(); } public function test_job_sends_invoice_mail_and_logs_it(): void { $this->seed(\Database\Seeders\TemplateSeeder::class); Mail::fake(); dispatch_sync(new SendInvoiceMailJob($this->invoice->id, 'invoice', 'Hi', null, null, $this->admin->id)); Mail::assertSent(InvoiceNotificationMail::class); $this->assertDatabaseHas('mail', ['order_id' => $this->invoice->order_id, 'sent_by' => $this->admin->id]); } }