From 0bc06e1bb1bf3f0b0f241827ef6bd7704650b9fc Mon Sep 17 00:00:00 2001 From: Matt Kilgore Date: Fri, 20 Mar 2020 17:59:57 -0400 Subject: [PATCH] fix insertAfter and insertBefore middleware extender functions (#2063) --- src/Extend/Middleware.php | 8 ++-- .../integration/extenders/MiddlewareTest.php | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Extend/Middleware.php b/src/Extend/Middleware.php index fac40bd56..2dad16a4e 100644 --- a/src/Extend/Middleware.php +++ b/src/Extend/Middleware.php @@ -49,14 +49,14 @@ class Middleware implements ExtenderInterface public function insertBefore($originalMiddleware, $newMiddleware) { - $this->replaceMiddlewares[$originalMiddleware] = $newMiddleware; + $this->insertBeforeMiddlewares[$originalMiddleware] = $newMiddleware; return $this; } public function insertAfter($originalMiddleware, $newMiddleware) { - $this->replaceMiddlewares[$originalMiddleware] = $newMiddleware; + $this->insertAfterMiddlewares[$originalMiddleware] = $newMiddleware; return $this; } @@ -81,7 +81,7 @@ class Middleware implements ExtenderInterface foreach ($this->insertBeforeMiddlewares as $originalMiddleware => $newMiddleware) { array_splice( $existingMiddleware, - array_search($originalMiddleware, $existingMiddleware), + array_search($originalMiddleware, $existingMiddleware) + 1, 0, $newMiddleware ); @@ -90,7 +90,7 @@ class Middleware implements ExtenderInterface foreach ($this->insertAfterMiddlewares as $originalMiddleware => $newMiddleware) { array_splice( $existingMiddleware, - array_search($originalMiddleware, $existingMiddleware) + 1, + array_search($originalMiddleware, $existingMiddleware), 0, $newMiddleware ); diff --git a/tests/integration/extenders/MiddlewareTest.php b/tests/integration/extenders/MiddlewareTest.php index 2519b4a58..dca895be6 100644 --- a/tests/integration/extenders/MiddlewareTest.php +++ b/tests/integration/extenders/MiddlewareTest.php @@ -84,6 +84,44 @@ class MiddlewareTest extends TestCase $this->assertEquals(200, $response->getStatusCode()); $this->assertArrayNotHasKey('X-First-Test-Middleware', $response->getHeaders()); } + + /** + * @test + */ + public function can_insert_before_middleware() + { + $this->add_first_middleware(); + $this->extend( + (new Extend\Middleware('forum'))->insertBefore(FirstTestMiddleware::class, SecondTestMiddleware::class) + ); + + $response = $this->send($this->request('GET', '/')); + $headers = $response->getHeaders(); + $newMiddlewarePosition = array_search('X-Second-Test-Middleware', array_keys($headers)); + $originalMiddlewarePosition = array_search('X-First-Test-Middleware', array_keys($headers)); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertLessThan($originalMiddlewarePosition, $newMiddlewarePosition); + } + + /** + * @test + */ + public function can_insert_after_middleware() + { + $this->add_first_middleware(); + $this->extend( + (new Extend\Middleware('forum'))->insertAfter(FirstTestMiddleware::class, SecondTestMiddleware::class) + ); + + $response = $this->send($this->request('GET', '/')); + $headers = $response->getHeaders(); + $newMiddlewarePosition = array_search('X-Second-Test-Middleware', array_keys($headers)); + $originalMiddlewarePosition = array_search('X-First-Test-Middleware', array_keys($headers)); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertGreaterThan($originalMiddlewarePosition, $newMiddlewarePosition); + } } class FirstTestMiddleware implements MiddlewareInterface