Skip to content

Commit

Permalink
Fix Middleware Classifier for Laravel 11 (#223)
Browse files Browse the repository at this point in the history
* Rename variables

* Remove unused getEnvironmentSetup

* Use Middleware from Router in Middleware Classifier

* Update Tests

* Support Rector v1
  • Loading branch information
stefanzweifel authored Feb 13, 2024
1 parent 2a23809 commit 718cd70
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"orchestra/testbench": "^7.0 | ^8.0 | ^9.0",
"pestphp/pest": "^1 || ^2",
"phpunit/phpunit": "^9.0 | ^10.0 | ^11.0",
"rector/rector": "^0.15",
"rector/rector": "^0.15|^1.0",
"vimeo/psalm": "^5.0"
},
"autoload": {
Expand Down
25 changes: 21 additions & 4 deletions src/Classifiers/MiddlewareClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wnx\LaravelStats\Classifiers;

use Illuminate\Routing\Route;
use ReflectionProperty;
use Illuminate\Contracts\Http\Kernel;
use Wnx\LaravelStats\ReflectionClass;
Expand All @@ -21,19 +22,21 @@ public function satisfies(ReflectionClass $class): bool
{
$this->httpKernel = $this->getHttpKernelInstance();

$middleware = $this->getMiddleware();
$middlewares = $this->getMiddlewares();

if (in_array($class->getName(), $middleware)) {
if (in_array($class->getName(), $middlewares)) {
return true;
}

return collect($middleware)
return collect($middlewares)
->merge($this->getMiddlewareGroupsFromKernel())
->merge($this->getRouteMiddlewares())
->flatten()
->unique()
->contains($class->getName());
}

protected function getMiddleware(): array
protected function getMiddlewares(): array
{
$reflection = new ReflectionProperty($this->httpKernel, 'middleware');
$reflection->setAccessible(true);
Expand Down Expand Up @@ -77,4 +80,18 @@ public function countsTowardsTests(): bool
{
return false;
}

private function getRouteMiddlewares(): array
{
$reflection = new ReflectionProperty($this->httpKernel, 'router');
$reflection->setAccessible(true);

$router = $reflection->getValue($this->httpKernel);

return collect($router->getRoutes()->getRoutes())
->map(fn (Route $route) => $route->middleware())
->flatten()
->unique()
->toArray();
}
}
22 changes: 17 additions & 5 deletions tests/Classifiers/MiddlewareClassifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Wnx\LaravelStats\Tests\Classifiers;

use Orchestra\Testbench\Attributes\DefineRoute;
use PHPUnit\Framework\Attributes\Test;
use Wnx\LaravelStats\Tests\TestCase;
use Wnx\LaravelStats\ReflectionClass;
use Illuminate\Routing\Middleware\ThrottleRequests;
Expand All @@ -10,13 +12,23 @@

class MiddlewareClassifierTest extends TestCase
{
/** @test */
public function it_returns_true_if_given_class_is_a_middleware()
/**
* Define routes setup.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
protected function usesMiddlewareRoutes($router)
{
if ($this->getLaravelVersion() === 11.0) {
$this->markTestSkipped("Middleware classification currently broken in Laravel 11.");
}
$router->get('/demo', function () {
return 'Hello World';
})->middleware(DemoMiddleware::class);
}

#[Test]
#[DefineRoute('usesMiddlewareRoutes')]
public function it_returns_true_if_given_class_is_a_middleware(): void
{
$this->assertTrue(
(new MiddlewareClassifier())->satisfies(
new ReflectionClass(DemoMiddleware::class)
Expand Down
7 changes: 0 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ protected function getPackageProviders($app)
];
}

/**
* @param \Illuminate\Foundation\Application $app
*/
protected function getEnvironmentSetUp($app)
{
}

/**
* Resolve application HTTP Kernel implementation.
*
Expand Down

0 comments on commit 718cd70

Please sign in to comment.