-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not instantiate the controller just for fetching the middleware
- Loading branch information
1 parent
bb4035b
commit ce2412a
Showing
6 changed files
with
94 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
tests/Integration/Routing/Fixtures/ControllerWithStaticallyDefinedMiddleware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Routing\Fixtures; | ||
|
||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Routing\Controller; | ||
|
||
class ControllerWithStaticallyDefinedMiddleware extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
$_SERVER['controller_with_statically_defined_middleware_was_constructed'] = true; | ||
} | ||
|
||
public static function getMiddleware(): array | ||
{ | ||
static::middleware('auth'); | ||
|
||
static::middleware(function (Request $request): RedirectResponse { | ||
return new RedirectResponse('https://www.foo.com'); | ||
}); | ||
|
||
return parent::getMiddleware(); | ||
} | ||
|
||
public function __invoke(): Response | ||
{ | ||
return new Response('foobar'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Routing; | ||
|
||
use Illuminate\Auth\GenericUser; | ||
use Illuminate\Contracts\Routing\Registrar; | ||
use Illuminate\Tests\Integration\Routing\Fixtures\ControllerWithStaticallyDefinedMiddleware; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
final class RoutingMiddlewareTest extends TestCase | ||
{ | ||
protected function tearDown(): void | ||
{ | ||
unset($_SERVER['controller_with_statically_defined_middleware_was_constructed']); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testControllerIsNotInstantiatedWhenStaticallyDefiningMiddlewareOnIt() | ||
{ | ||
$this->withoutExceptionHandling(); | ||
$this->actingAs(new GenericUser([])); | ||
|
||
$_SERVER['controller_with_statically_defined_middleware_was_constructed'] = false; | ||
|
||
/** @var Registrar $router */ | ||
$router = $this->app->make(Registrar::class); | ||
|
||
$router->get('test-route', ControllerWithStaticallyDefinedMiddleware::class); | ||
|
||
$response = $this->get('test-route'); | ||
|
||
$response->assertRedirect('https://www.foo.com'); | ||
|
||
$this->assertFalse($_SERVER['controller_with_statically_defined_middleware_was_constructed']); | ||
} | ||
} |