Skip to content

Commit

Permalink
Test notifying users after shipping orders
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Jan 19, 2023
1 parent dead386 commit 5c9418d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/Http/Controllers/OrderShipmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use App\Mail\OrderShipped;
use App\Models\Order;
use App\Notifications\OrderShipmentNotification;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;

class OrderShipmentController extends Controller
{
Expand Down Expand Up @@ -37,4 +39,14 @@ public function shipOrderAdvanced(Order $order)

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

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

Notification::send(auth()->user(), new OrderShipmentNotification($order));

return ['message' => 'Your order has been shipped.'];
}
}
71 changes: 71 additions & 0 deletions app/Notifications/OrderShipmentNotification.php
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 [
//
];
}
}
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@

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

Route::patch('orders/{order}/shipment/notification', [OrderShipmentController::class, 'notifyForOrderShipment'])
->name('order.shipment.notification');
});

Route::prefix('orders')->group(function() {
Expand Down
34 changes: 34 additions & 0 deletions tests/Feature/NotificationTest.php
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);
});
}
}

0 comments on commit 5c9418d

Please sign in to comment.