Skip to content

Commit

Permalink
test: add a test confirming terminable route middleware is called (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
countless-integers authored Jan 13, 2025
1 parent 10732a5 commit 54d9fef
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Http;

use App\Http\Middleware\ApiAfter;
use App\Http\Middleware\Authenticate;
use App\Http\Middleware\EncryptCookies;
use App\Http\Middleware\PreventRequestsDuringMaintenance;
Expand Down Expand Up @@ -50,6 +51,7 @@ final class Kernel extends HttpKernel
'api' => [
'throttle:api',
SubstituteBindings::class,
ApiAfter::class,
],
];

Expand Down
23 changes: 23 additions & 0 deletions app/Http/Middleware/ApiAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use App\Jobs\SampleJob;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

final class ApiAfter
{
public function handle(Request $request, Closure $next): Response
{
return $next($request);
}

public function terminate(Request $request, Response $response): void
{
dispatch(new SampleJob());
}
}
18 changes: 18 additions & 0 deletions app/Jobs/SampleJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SampleJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function handle()
{
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
fn(Request $request) => $request->user()
);

Route::get('/ping', fn(Request $request) => 'pong');
Route::post('/upload-files', [TestController::class, 'uploadFiles']);
19 changes: 19 additions & 0 deletions tests/Functional/MiddlewaresCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Functional;

use App\Jobs\SampleJob;
use Illuminate\Support\Facades\Queue;
use Tests\FunctionalTester;

class MiddlewaresCest
{
public function terminableRouteMiddlewareIsCalled(FunctionalTester $I): void
{
Queue::fake();

$I->amOnPage('/api/ping');

Queue::assertPushed(SampleJob::class);
}
}

0 comments on commit 54d9fef

Please sign in to comment.