Skip to content

Commit

Permalink
[9.x] Add method to remove a middleware from a group (#44780)
Browse files Browse the repository at this point in the history
* Add removeMiddlewareFromGroup method

* Add tests for pushMiddlewareToGroup

* fix test name

* style ci

* Update Router.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
mateusjatenee and taylorotwell authored Oct 31, 2022
1 parent 4f7ee1d commit 779fe61
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
48 changes: 48 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 779fe61

Please sign in to comment.