Skip to content

Commit

Permalink
Add ViewMatched event (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive authored Jul 25, 2023
1 parent b41ec1e commit 7e27de8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Events/ViewMatched.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Laravel\Folio\Events;

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

class ViewMatched
{
/**
* Create a new view matched event instance.
*/
public function __construct(
public MatchedView $matchedView,
public 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 @@ -31,6 +32,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 7e27de8

Please sign in to comment.