From 779fe61f4268ab3daba7e8c5d7edab94ed07f3ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Guimar=C3=A3es?= Date: Mon, 31 Oct 2022 10:50:19 -0300 Subject: [PATCH] [9.x] Add method to remove a middleware from a group (#44780) * Add removeMiddlewareFromGroup method * Add tests for pushMiddlewareToGroup * fix test name * style ci * Update Router.php Co-authored-by: Taylor Otwell --- src/Illuminate/Routing/Router.php | 26 +++++++++++++++ tests/Routing/RouteRegistrarTest.php | 48 ++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/src/Illuminate/Routing/Router.php b/src/Illuminate/Routing/Router.php index ababf351ada0..d5ab01928351 100644 --- a/src/Illuminate/Routing/Router.php +++ b/src/Illuminate/Routing/Router.php @@ -999,6 +999,32 @@ public function pushMiddlewareToGroup($group, $middleware) return $this; } + /** + * Remove the given middleware from the specified group. + * + * @param string $group + * @param string $middleware + * @return $this + */ + public function removeMiddlewareFromGroup($group, $middleware) + { + if (! $this->hasMiddlewareGroup($group)) { + return $this; + } + + $reversedMiddlewaresArray = array_flip($this->middlewareGroups[$group]); + + if (! array_key_exists($middleware, $reversedMiddlewaresArray)) { + return $this; + } + + $middlewareKey = $reversedMiddlewaresArray[$middleware]; + + unset($this->middlewareGroups[$group][$middlewareKey]); + + return $this; + } + /** * Flush the router's middleware groups. * diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index e585e77a1a0c..5449bcaf5fc4 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -1020,6 +1020,54 @@ public function testCanSetRouteNameUsingNameAlias() $this->assertSame('users.index', $this->getRoute()->getName()); } + public function testPushMiddlewareToGroup() + { + $this->router->middlewareGroup('web', []); + $this->router->pushMiddlewareToGroup('web', 'test-middleware'); + + $this->assertEquals(['test-middleware'], $this->router->getMiddlewareGroups()['web']); + } + + public function testPushMiddlewareToGroupUnregisteredGroup() + { + $this->router->pushMiddlewareToGroup('web', 'test-middleware'); + + $this->assertEquals(['test-middleware'], $this->router->getMiddlewareGroups()['web']); + } + + public function testPushMiddlewareToGroupDuplicatedMiddleware() + { + $this->router->pushMiddlewareToGroup('web', 'test-middleware'); + $this->router->pushMiddlewareToGroup('web', 'test-middleware'); + + $this->assertEquals(['test-middleware'], $this->router->getMiddlewareGroups()['web']); + } + + public function testCanRemoveMiddlewareFromGroup() + { + $this->router->pushMiddlewareToGroup('web', 'test-middleware'); + + $this->router->removeMiddlewareFromGroup('web', 'test-middleware'); + + $this->assertEquals([], $this->router->getMiddlewareGroups()['web']); + } + + public function testCanRemoveMiddlewareFromGroupNotUnregisteredMiddleware() + { + $this->router->middlewareGroup('web', []); + + $this->router->removeMiddlewareFromGroup('web', 'different-test-middleware'); + + $this->assertEquals([], $this->router->getMiddlewareGroups()['web']); + } + + public function testCanRemoveMiddlewareFromGroupUnregisteredGroup() + { + $this->router->removeMiddlewareFromGroup('web', ['test-middleware']); + + $this->assertEquals([], $this->router->getMiddlewareGroups()); + } + /** * Get the last route registered with the router. *