Skip to content

Commit

Permalink
Test sending email via queue
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Jan 9, 2023
1 parent e7bee04 commit 9544752
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/Http/Controllers/OrderShipmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ public function shipOrderBasic(Order $order)

return ['message' => 'Your order has been shipped.'];
}

public function shipOrderAdvanced(Order $order)
{
$order->status = 'shipped';
$order->save();

$mailable = (new OrderShipped($order))
->onConnection('redis')
->onQueue('order_shipment');

Mail::to(auth()->user())->queue($mailable);

return ['message' => 'Your order has been shipped.'];
}
}
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@

Route::post('orders/{order}/shipped/basic', [OrderShipmentController::class, 'shipOrderBasic'])
->name('order.shipped.basic');

Route::post('orders/{order}/shipped/advanced', [OrderShipmentController::class, 'shipOrderAdvanced'])
->name('order.shipped.advanced');
});
});
17 changes: 17 additions & 0 deletions tests/Feature/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,21 @@ public function email_api_can_be_instructed_to_send_a_mailable()

Mail::assertSent(OrderShipped::class);
}

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

Mail::fake();

Mail::assertNotQueued(OrderShipped::class);

$this->post(route(
'order.shipped.advanced',
Order::factory()->create(['user_id' => $user->id])
));

Mail::assertQueued(OrderShipped::class);
}
}

0 comments on commit 9544752

Please sign in to comment.