Skip to content

Commit

Permalink
Added test cases for laravel middleware method discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
LWlook authored and Lukas Weilguny committed Dec 23, 2024
1 parent e5d2be2 commit 9585631
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
29 changes: 27 additions & 2 deletions tests/RouteDiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Domain\DomainController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverController\DoNotDiscoverThisMethodController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverMethod\DoNotDiscoverMethodController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverMiddleware\DiscoverMiddlewareIfNotLaravelMethodController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverMiddleware\DoNotDiscoverMiddlewareController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Invokable\InvokableController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Middleware\MiddlewareOnControllerController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Middleware\MiddlewareOnMethodController;
Expand Down Expand Up @@ -271,7 +273,6 @@
);
});


it('can override the http method', function () {
$this
->routeRegistrar
Expand Down Expand Up @@ -358,6 +359,30 @@
);
});

it('can avoid discovering the laravel middleware method', function () {
$this
->routeRegistrar
->registerDirectory(controllersPath('DoNotDiscoverMiddleware'));

$this
->assertRegisteredRoutesCount(3)
->assertRouteRegistered(
DoNotDiscoverMiddlewareController::class,
controllerMethod: 'method',
uri: 'do-not-discover-middleware/method',
)
->assertRouteRegistered(
DiscoverMiddlewareIfNotLaravelMethodController::class,
controllerMethod: 'method',
uri: 'discover-middleware-if-not-laravel-method/method',
)
->assertRouteRegistered(
DiscoverMiddlewareIfNotLaravelMethodController::class,
controllerMethod: 'middleware',
uri: 'discover-middleware-if-not-laravel-method/middleware',
);
});

it('can avoid discovering a controller', function () {
$this
->routeRegistrar
Expand Down Expand Up @@ -461,7 +486,7 @@
$this->assertRegisteredRoutesCount(3);

$registeredUris = collect(app()->router->getRoutes())
->map(fn (Route $route) => $route->uri)
->map(fn(Route $route) => $route->uri)
->toArray();

expect($registeredUris)->toEqual([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverMiddleware;

class DiscoverMiddlewareIfNotLaravelMethodController
{
public static function middleware(): array {
return [];
}

public function method()
{
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverMiddleware;

use Illuminate\Routing\Controllers\HasMiddleware;

class DoNotDiscoverMiddlewareController implements HasMiddleware
{
public static function middleware(): array {
return [];
}

public function method()
{
}

}

0 comments on commit 9585631

Please sign in to comment.