-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test notifying users after shipping orders
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace App\Notifications; | ||
|
||
use App\Models\Order; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Notifications\Notification; | ||
|
||
class OrderShipmentNotification extends Notification | ||
{ | ||
use Queueable; | ||
|
||
public Order $order; | ||
|
||
/** | ||
* Create a new notification instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Order $order) | ||
{ | ||
$this->order = $order; | ||
} | ||
|
||
/** | ||
* Get the notification's delivery channels. | ||
* | ||
* @param mixed $notifiable | ||
* @return array | ||
*/ | ||
public function via($notifiable) | ||
{ | ||
// $notifiable is $user in this case. | ||
|
||
return ['mail']; | ||
} | ||
|
||
/** | ||
* Get the mail representation of the notification. | ||
* | ||
* @param mixed $notifiable | ||
* @return \Illuminate\Notifications\Messages\MailMessage | ||
*/ | ||
public function toMail($notifiable) | ||
{ | ||
// $notifiable is $user in this case. | ||
|
||
$url = url('/orders/'.$this->order->id); | ||
|
||
return (new MailMessage) | ||
->greeting("Hello! {$notifiable->name}") | ||
->line('Your order has been shipped!') | ||
->action('View Order', $url) | ||
->line('Thank you for using our application!'); | ||
} | ||
|
||
/** | ||
* Get the array representation of the notification. | ||
* | ||
* @param mixed $notifiable | ||
* @return array | ||
*/ | ||
public function toArray($notifiable) | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
|
||
use App\Models\Order; | ||
use App\Models\User; | ||
use App\Notifications\OrderShipmentNotification; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Support\Facades\Notification; | ||
use Tests\TestCase; | ||
|
||
class NotificationTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** @test */ | ||
public function notifying_users_after_shipping_orders() | ||
{ | ||
$this->actingAs($user = User::factory()->create()); | ||
|
||
Notification::fake(); | ||
|
||
$this->patch(route('order.shipment.notification', $order = Order::factory()->create())); | ||
|
||
Notification::assertSentTo($user, OrderShipmentNotification::class, function ($notification) use ($user, $order) { | ||
return $notification->order->id === $order->id && | ||
in_array('mail', $notification->via($user)) && | ||
($notification->toMail($user) instanceof MailMessage); | ||
}); | ||
} | ||
} |