From e80c698e85fa3f957f4d41b1d52791eaf96edef8 Mon Sep 17 00:00:00 2001 From: unclexo Date: Fri, 6 Jan 2023 23:12:33 +0600 Subject: [PATCH] Test mailable can have attachments --- app/Mail/OrderShipped.php | 6 +++++- tests/Feature/MailTest.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/Mail/OrderShipped.php b/app/Mail/OrderShipped.php index b6c6b31..5da5f7e 100644 --- a/app/Mail/OrderShipped.php +++ b/app/Mail/OrderShipped.php @@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; +use Illuminate\Mail\Mailables\Attachment; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; @@ -48,6 +49,9 @@ public function content() */ public function attachments() { - return []; + return [ + Attachment::fromPath(storage_path('app/public/some.pdf')), + Attachment::fromStorageDisk('public', 'other.pdf'), + ]; } } diff --git a/tests/Feature/MailTest.php b/tests/Feature/MailTest.php index 3abb70f..993049f 100644 --- a/tests/Feature/MailTest.php +++ b/tests/Feature/MailTest.php @@ -10,6 +10,7 @@ use Illuminate\Contracts\Mail\Mailable; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; +use Illuminate\Mail\Mailables\Attachment; use Tests\TestCase; class MailTest extends TestCase @@ -108,4 +109,17 @@ public function content_can_be_set_to_mailable_at_runtime() $mailable->assertSeeInHtml('Order Shipped'); } + + /** @test */ + public function mailable_can_have_attachments() + { + $this->actingAs($user = User::factory()->create()); + + $order = Order::factory()->create(['user_id' => $user->id]); + + $mailable = new OrderShipped($order); + + $mailable->assertHasAttachment(Attachment::fromPath(storage_path('app/public/some.pdf'))); + $mailable->assertHasAttachment(Attachment::fromStorageDisk('public', 'other.pdf')); + } }