fix insertAfter and insertBefore middleware extender functions (#2063)

This commit is contained in:
Matt Kilgore 2020-03-20 17:59:57 -04:00 committed by GitHub
parent b10a17529d
commit 0bc06e1bb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 4 deletions

View File

@ -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
);

View File

@ -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