Skip to content

Commit

Permalink
Test previewing email template
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Jan 6, 2023
1 parent fecdf9d commit 13e5edd
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
16 changes: 16 additions & 0 deletions app/Http/Controllers/PostPublishController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers;

use App\Mail\PostPublished;
use App\Models\Post;

class PostPublishController extends Controller
{
public function previewMailTemplate(int $id)
{
$post = Post::find($id);

return (new PostPublished($post))->render();
}
}
10 changes: 8 additions & 2 deletions app/Mail/PostPublished.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Mail;

use App\Models\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
Expand All @@ -13,14 +14,16 @@ class PostPublished extends Mailable
{
use Queueable, SerializesModels;

public Post $post;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
public function __construct(Post $post)
{
//
$this->post = $post;
}

/**
Expand All @@ -44,6 +47,9 @@ public function content()
{
return new Content(
markdown: 'emails.posts.published',
with: [
'url' => url("/posts/{$this->post->id}"),
],
);
}

Expand Down
8 changes: 4 additions & 4 deletions resources/views/emails/posts/published.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<x-mail::message>
# Introduction
# Post Published

The body of your message.
Your post has been published on Medium

<x-mail::button :url="''">
Button Text
<x-mail::button :url="$url">
View Post
</x-mail::button>

Thanks,<br>
Expand Down
6 changes: 6 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use App\Http\Controllers\HomeController;
use App\Http\Controllers\MediaUploaderController;
use App\Http\Controllers\PostController;
use App\Http\Controllers\PostPublishController;
use Illuminate\Support\Facades\Route;

/*
Expand Down Expand Up @@ -56,4 +57,9 @@

Route::get('/upload/download/{filename}', 'download')->name('upload.download');
});

Route::prefix('mailable')->group(function() {
Route::get('template/{id}', [PostPublishController::class, 'previewMailTemplate'])
->name('mailable.preview');
});
});
21 changes: 20 additions & 1 deletion tests/Feature/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,39 @@

use App\Mail\OrderShipped;
use App\Mail\PostPublished;
use App\Models\Post;
use App\Models\User;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class MailTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function mailables_are_available()
{
$postPublished = new PostPublished();
$this->actingAs($user = User::factory()->create());

$post = Post::factory()->create(['user_id' => $user->id]);

$postPublished = new PostPublished($post);
$orderShipped = new OrderShipped();

$this->assertInstanceOf(Mailable::class, $postPublished);
$this->assertInstanceOf(Mailable::class, $orderShipped);
}

/** @test */
public function mailable_can_be_previewed()
{
$this->actingAs($user = User::factory()->create());

$post = Post::factory()->create(['user_id' => $user->id]);

$this->get(route('mailable.preview', $post->id))
->assertStatus(200);
}
}

0 comments on commit 13e5edd

Please sign in to comment.