71 lines
2.9 KiB
PHP
71 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\Mail;
|
|
|
|
use App\Jobs\Mail\Customer\HostingAccountCreatedJob;
|
|
use App\Mail\Customer\HostingAccountCreated;
|
|
use App\Models\Customer;
|
|
use App\Models\Invoice;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Tests\TestCase;
|
|
|
|
class HostingAccountCreatedInvoiceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private function payload(array $extra = []): array
|
|
{
|
|
return $extra + ['customer_email' => 'khach@example.test', 'customer_name' => 'Khach', 'domain_name' => 'baodev111.net'];
|
|
}
|
|
|
|
public function test_hosting_ready_email_attaches_the_order_invoice(): void
|
|
{
|
|
Mail::fake();
|
|
$invoice = Invoice::create([
|
|
'invoice_number' => 'INV-HOST-1', 'customer_id' => Customer::factory()->create()->id,
|
|
'invoice_type' => 1, 'sub_total' => 100_000, 'tax' => 0, 'total' => 100_000, 'status' => 2,
|
|
]);
|
|
|
|
(new HostingAccountCreatedJob($this->payload(['invoice_id' => $invoice->id])))->handle();
|
|
|
|
Mail::assertSent(HostingAccountCreated::class, fn (HostingAccountCreated $mail) => $mail->invoicePdfFileName === 'Hoa-don-INV-HOST-1.pdf'
|
|
&& str_starts_with((string) $mail->invoicePdf, '%PDF')
|
|
&& count($mail->attachments()) === 1);
|
|
}
|
|
|
|
public function test_email_still_sent_without_invoice(): void
|
|
{
|
|
Mail::fake();
|
|
|
|
(new HostingAccountCreatedJob($this->payload()))->handle();
|
|
|
|
Mail::assertSent(HostingAccountCreated::class, fn (HostingAccountCreated $mail) => $mail->attachments() === []);
|
|
}
|
|
|
|
public function test_domain_order_received_email_attaches_the_invoice(): void
|
|
{
|
|
$invoice = Invoice::create([
|
|
'invoice_number' => 'INV-DOM-1', 'customer_id' => Customer::factory()->create()->id,
|
|
'invoice_type' => 1, 'sub_total' => 100_000, 'tax' => 0, 'total' => 100_000, 'status' => 2,
|
|
]);
|
|
|
|
$attachments = (new \App\Mail\Customer\DomainOrderReceivedNotification(['invoice_id' => $invoice->id]))->attachments();
|
|
|
|
$this->assertCount(1, $attachments);
|
|
$this->assertCount(0, (new \App\Mail\Customer\DomainOrderReceivedNotification([]))->attachments());
|
|
}
|
|
|
|
public function test_subject_uses_company_server_and_customer_name_with_fallbacks(): void
|
|
{
|
|
$this->seed(\Database\Seeders\TemplateSeeder::class);
|
|
$subject = fn (array $data) => (new HostingAccountCreated($data + ['server_name' => 'HOST-01']))->envelope()->subject;
|
|
|
|
$this->assertSame('V/v: '.\App\Support\MailGlobals::values()['company_name'].' gửi thông tin truy cập - HOST-01 - Nguyen Van A', $subject(['customer_name' => 'Nguyen Van A']));
|
|
$this->assertStringEndsWith(' - Cong ty ABC', $subject(['customer_name' => ' ', 'customer_company' => 'Cong ty ABC']));
|
|
$this->assertStringEndsWith(' - Công ty', $subject([]));
|
|
}
|
|
}
|