From 8641725d1f460053377027dd4007a1a10c7c898e Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Tue, 25 Jul 2023 18:51:08 +0200 Subject: [PATCH] Add `ViewMatched` event --- src/Events/ViewMatched.php | 12 ++++++++++++ src/RequestHandler.php | 3 +++ tests/Feature/ManagerTest.php | 28 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 src/Events/ViewMatched.php diff --git a/src/Events/ViewMatched.php b/src/Events/ViewMatched.php new file mode 100644 index 0000000..0d842de --- /dev/null +++ b/src/Events/ViewMatched.php @@ -0,0 +1,12 @@ +mountPath->path ))->match($request, $uri) ?? abort(404); + app(Dispatcher::class)->dispatch(new Events\ViewMatched($matchedView)); + return (new Pipeline(app())) ->send($request) ->through($this->middleware($matchedView)) diff --git a/tests/Feature/ManagerTest.php b/tests/Feature/ManagerTest.php index e3c8a4f..f6cf83f 100644 --- a/tests/Feature/ManagerTest.php +++ b/tests/Feature/ManagerTest.php @@ -1,7 +1,9 @@ and($data)->toBe(['id' => 'Taylor']) ->and($mountPath)->toBe(__DIR__.'/resources/views/pages'); }); + +it('fires view matched event on route', function () { + Folio::route(__DIR__.'/resources/views/pages'); + + $events = Event::fake(ViewMatched::class); + + $response = $this->get('/users/Taylor'); + + $response->assertOk(); + + $events->assertDispatched(ViewMatched::class, function ($event) { + return $event->matched->path === __DIR__.'/resources/views/pages/users/[id].blade.php'; + }); +}); + +it('doesn\'t fire view matched event on 404', function () { + Folio::route(__DIR__.'/resources/views/pages'); + + $events = Event::fake(ViewMatched::class); + + $response = $this->get('/invalid-route'); + + $response->assertNotFound(); + + $events->assertNotDispatched(ViewMatched::class); +});