From f25df8761d8ad225a25b7223e1fe6c39a3f0e57b Mon Sep 17 00:00:00 2001 From: unclexo Date: Wed, 11 Jan 2023 21:48:31 +0600 Subject: [PATCH] Test triggering an event after deleting orders --- app/Http/Controllers/OrderController.php | 8 ++++++++ routes/web.php | 2 ++ tests/Feature/EventTest.php | 14 ++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/app/Http/Controllers/OrderController.php b/app/Http/Controllers/OrderController.php index cbc04ce..2b42d83 100644 --- a/app/Http/Controllers/OrderController.php +++ b/app/Http/Controllers/OrderController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Events\OrderCreatedEvent; +use App\Events\OrderDeletedEvent; use App\Events\OrderUpdatedEvent; use App\Models\Order; use Illuminate\Validation\Rule; @@ -35,4 +36,11 @@ public function update(Order $order) OrderUpdatedEvent::dispatch($order); } + + public function destroy(Order $order) + { + $order->delete(); + + OrderDeletedEvent::dispatch($order); + } } diff --git a/routes/web.php b/routes/web.php index 72e281d..88c689d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -75,5 +75,7 @@ Route::post('/', [OrderController::class, 'store'])->name('orders.store'); Route::patch('/{order}', [OrderController::class, 'update'])->name('orders.update'); + + Route::delete('/{order}', [OrderController::class, 'destroy'])->name('orders.delete'); }); }); diff --git a/tests/Feature/EventTest.php b/tests/Feature/EventTest.php index c329c7d..1cd59ff 100644 --- a/tests/Feature/EventTest.php +++ b/tests/Feature/EventTest.php @@ -74,4 +74,18 @@ public function triggering_an_event_after_updating_orders() Event::assertDispatched(OrderUpdatedEvent::class); } + + /** @test */ + public function triggering_an_event_after_deleting_orders() + { + $this->actingAs($user = User::factory()->create()); + + Event::fake(); + + Event::assertNotDispatched(OrderDeletedEvent::class); + + $this->delete(route('orders.delete', Order::factory()->create())); + + Event::assertDispatched(OrderDeletedEvent::class); + } }