Skip to content

Commit

Permalink
Add ViewMatched event
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed Jul 25, 2023
1 parent 51836cc commit 2031897
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Events/ViewMatched.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Laravel\Folio\Events;

use Laravel\Folio\MountPath;
use Laravel\Folio\Pipeline\MatchedView;

class ViewMatched
{
public function __construct(
public readonly MatchedView $matchedView,
public readonly MountPath $mountPath,
) {}
}
3 changes: 3 additions & 0 deletions src/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Folio;

use Closure;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Pipeline;
Expand Down Expand Up @@ -30,6 +31,8 @@ public function __invoke(Request $request, string $uri): mixed
$this->mountPath->path
))->match($request, $uri) ?? abort(404);

app(Dispatcher::class)->dispatch(new Events\ViewMatched($matchedView, $this->mountPath));

return (new Pipeline(app()))
->send($request)
->through($this->middleware($matchedView))
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/ManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Laravel\Folio\Events\ViewMatched;
use Laravel\Folio\Folio;
use Laravel\Folio\Pipeline\MatchedView;

Expand Down Expand Up @@ -52,3 +54,29 @@
->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->matchedView->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);
});

0 comments on commit 2031897

Please sign in to comment.