Skip to content

Commit

Permalink
Fixes controller computed middleware (#44454)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Oct 4, 2022
1 parent 51abec6 commit 0012549
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ protected function parseControllerCallback()
*/
public function flushController()
{
$this->computedMiddleware = null;
$this->controller = null;
}

Expand Down
31 changes: 24 additions & 7 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1772,14 +1772,17 @@ public function testRouteFlushController()
$request = Request::create('count', 'GET');

$response = $router->dispatch($request);
$this->assertSame(1, (int) $response->getContent());
$this->assertSame(1, $response->original['invokedCount']);
$this->assertSame(1, $response->original['middlewareInvokedCount']);

$response = $router->dispatch($request);
$this->assertSame(2, (int) $response->getContent());
$this->assertSame(2, $response->original['invokedCount']);
$this->assertSame(2, $response->original['middlewareInvokedCount']);

$request->route()->flushController();
$response = $router->dispatch($request);
$this->assertSame(1, (int) $response->getContent());
$this->assertSame(1, $response->original['invokedCount']);
$this->assertSame(1, $response->original['middlewareInvokedCount']);
}

public function testJsonResponseIsReturned()
Expand Down Expand Up @@ -2323,15 +2326,29 @@ public function __invoke()
}
}

class ActionCountStub
class ActionCountStub extends Controller
{
protected $count = 0;
protected $middlewareInvokedCount = 0;

protected $invokedCount = 0;

public function __construct()
{
$this->middleware(function ($request, $next) {
$this->middlewareInvokedCount++;

return $next($request);
});
}

public function __invoke()
{
$this->count++;
$this->invokedCount++;

return $this->count;
return [
'invokedCount' => $this->invokedCount,
'middlewareInvokedCount' => $this->middlewareInvokedCount,
];
}
}

Expand Down

0 comments on commit 0012549

Please sign in to comment.