Skip to content

Commit 7e48526

Browse files
committed
Test setting content to mailable at runtime
1 parent 9693f2c commit 7e48526

File tree

7 files changed

+140
-20
lines changed

7 files changed

+140
-20
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
class OrderController extends Controller
8+
{
9+
//
10+
}

app/Mail/OrderShipped.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Mail;
44

5+
use App\Models\Order;
56
use Illuminate\Bus\Queueable;
67
use Illuminate\Contracts\Queue\ShouldQueue;
78
use Illuminate\Mail\Mailable;
@@ -13,26 +14,16 @@ class OrderShipped extends Mailable
1314
{
1415
use Queueable, SerializesModels;
1516

17+
public Order $order;
18+
1619
/**
1720
* Create a new message instance.
1821
*
1922
* @return void
2023
*/
21-
public function __construct()
22-
{
23-
//
24-
}
25-
26-
/**
27-
* Get the message envelope.
28-
*
29-
* @return \Illuminate\Mail\Mailables\Envelope
30-
*/
31-
public function envelope()
24+
public function __construct(Order $order)
3225
{
33-
return new Envelope(
34-
subject: 'Order Shipped',
35-
);
26+
$this->order = $order;
3627
}
3728

3829
/**
@@ -44,6 +35,9 @@ public function content()
4435
{
4536
return new Content(
4637
markdown: 'emails.orders.shipped',
38+
with: [
39+
'url' => url("/orders/{$this->order->id}"),
40+
],
4741
);
4842
}
4943

app/Models/Order.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Order extends Model
9+
{
10+
use HasFactory;
11+
}

database/factories/OrderFactory.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use App\Models\User;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
/**
9+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Order>
10+
*/
11+
class OrderFactory extends Factory
12+
{
13+
/**
14+
* Define the model's default state.
15+
*
16+
* @return array<string, mixed>
17+
*/
18+
public function definition()
19+
{
20+
return [
21+
'user_id' => User::factory()->create(),
22+
'shipper_id' => $this->faker->numberBetween(1, 10),
23+
'name' => $this->faker->userName,
24+
'price' => $this->faker->numberBetween(100, 10000),
25+
];
26+
}
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('orders', function (Blueprint $table) {
17+
$table->id();
18+
$table->unsignedBigInteger('user_id');
19+
$table->unsignedBigInteger('shipper_id');
20+
$table->string('name');
21+
$table->unsignedDecimal('price');
22+
$table->timestamps();
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::dropIfExists('orders');
34+
}
35+
};

resources/views/emails/orders/shipped.blade.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<x-mail::message>
2-
# Introduction
2+
# Order Shipped
33

4-
The body of your message.
4+
Your order has been shipped. The order summary:
55

6-
<x-mail::button :url="''">
7-
Button Text
6+
Order ID: {{ $order->id }}
7+
Name: {{ $order->name }}
8+
Price: {{ $order->price }}
9+
10+
<x-mail::button :url="$url">
11+
View Order
812
</x-mail::button>
913

1014
Thanks,<br>

tests/Feature/MailTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Mail\OrderShipped;
66
use App\Mail\PostPublished;
7+
use App\Models\Order;
78
use App\Models\Post;
89
use App\Models\User;
910
use Illuminate\Contracts\Mail\Mailable;
@@ -21,9 +22,10 @@ public function mailables_are_available()
2122
$this->actingAs($user = User::factory()->create());
2223

2324
$post = Post::factory()->create(['user_id' => $user->id]);
24-
2525
$postPublished = new PostPublished($post);
26-
$orderShipped = new OrderShipped();
26+
27+
$order = Order::factory()->create(['user_id' => $user->id]);
28+
$orderShipped = new OrderShipped($order);
2729

2830
$this->assertInstanceOf(Mailable::class, $postPublished);
2931
$this->assertInstanceOf(Mailable::class, $orderShipped);
@@ -69,4 +71,41 @@ public function mailable_has_valid_content()
6971

7072
$mailable->assertSeeInOrderInText(['View Post', 'Thanks']);
7173
}
74+
75+
/** @test */
76+
public function content_can_be_set_to_mailable_at_runtime()
77+
{
78+
$this->actingAs($user = User::factory()->create());
79+
80+
$order = Order::factory()->create(['user_id' => $user->id]);
81+
82+
$mailable = new OrderShipped($order);
83+
84+
$mailable
85+
->from('[email protected]', 'Amazon Books')
86+
->to('[email protected]', 'Abu Jobaer')
87+
->bcc('[email protected]', 'Ria Jobaer')
88+
->replyTo('[email protected]', 'Taylor Otwell')
89+
->subject('Order Shipped')
90+
->tag('shipment')
91+
->tag('fast-delivery')
92+
->metadata('order_id', $order->id);
93+
94+
95+
$mailable->assertFrom('[email protected]');
96+
97+
$mailable->assertTo('[email protected]');
98+
99+
$mailable->assertHasBcc('[email protected]');
100+
101+
$mailable->assertHasReplyTo('[email protected]');
102+
103+
$mailable->assertHasSubject('Order Shipped');
104+
105+
$mailable->assertHasTag('shipment');
106+
107+
$mailable->assertHasMetadata('order_id', $order->id);
108+
109+
$mailable->assertSeeInHtml('Order Shipped');
110+
}
72111
}

0 commit comments

Comments
 (0)