Skip to content

Commit

Permalink
[8.x] Create getter for the middleware priority (#37271)
Browse files Browse the repository at this point in the history
* Create getter for the middleware priority

This getter will help to create tests to ensure that middlewares are
registered in the right priority.

My use case for this is a middleware that I want to ensure gets
registered after the `StartSession` but right before the
`AuthenticatesRequests` one. With this getter I can use their indexes to
write a test for it.

* Update Kernel.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
roberto-aguilar and taylorotwell authored May 5, 2021
1 parent c11ecea commit fe6e25e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Illuminate/Foundation/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ protected function syncMiddlewareToRouter()
}
}

/**
* Get the priority-sorted list of middleware.
*
* @return array
*/
public function getMiddlewarePriority()
{
return $this->middlewarePriority;
}

/**
* Get the bootstrap classes for the application.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Foundation/Http/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ public function testGetRouteMiddleware()
$this->assertEquals([], $kernel->getRouteMiddleware());
}

public function testGetMiddlewarePriority()
{
$kernel = new Kernel($this->getApplication(), $this->getRouter());

$this->assertEquals([
\Illuminate\Cookie\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
], $kernel->getMiddlewarePriority());
}

/**
* @return \Illuminate\Contracts\Foundation\Application
*/
Expand Down

0 comments on commit fe6e25e

Please sign in to comment.